blob: ebdaa62f20af91032de99039136489164e04e49a [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'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000173/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000174};
175
176static int inverse_for_options[NUMBER_OF_OPT] =
177{
178/* -n */ 0,
179/* -s */ IP6T_INV_SRCIP,
180/* -d */ IP6T_INV_DSTIP,
181/* -p */ IP6T_INV_PROTO,
182/* -j */ 0,
183/* -v */ 0,
184/* -x */ 0,
185/* -i */ IP6T_INV_VIA_IN,
186/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000187/*--line*/ 0,
188/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189};
190
191const char *program_version;
192const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000193char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000194
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +0000195/* the path to command to load kernel module */
196const char *modprobe = NULL;
197
Rusty Russell5eed48a2000-06-02 20:12:24 +0000198/* Keeping track of external matches and targets: linked lists. */
199struct ip6tables_match *ip6tables_matches = NULL;
200struct ip6tables_target *ip6tables_targets = NULL;
201
202/* Extra debugging from libiptc */
203extern void dump_entries6(const ip6tc_handle_t handle);
204
205/* A few hardcoded protocols for 'all' and in case the user has no
206 /etc/protocols */
207struct pprot {
208 char *name;
209 u_int8_t num;
210};
211
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000212/* Primitive headers... */
213/* defined in netinet/in.h */
214#if 0
215#ifndef IPPROTO_ESP
216#define IPPROTO_ESP 50
217#endif
218#ifndef IPPROTO_AH
219#define IPPROTO_AH 51
220#endif
221#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000222#ifndef IPPROTO_MH
223#define IPPROTO_MH 135
224#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000225
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226static const struct pprot chain_protos[] = {
227 { "tcp", IPPROTO_TCP },
228 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000229 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000230 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000231 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000232 { "esp", IPPROTO_ESP },
233 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000234 { "ipv6-mh", IPPROTO_MH },
235 { "mh", IPPROTO_MH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000236};
237
238static char *
239proto_to_name(u_int8_t proto, int nolookup)
240{
241 unsigned int i;
242
243 if (proto && !nolookup) {
244 struct protoent *pent = getprotobynumber(proto);
245 if (pent)
246 return pent->p_name;
247 }
248
249 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
250 if (chain_protos[i].num == proto)
251 return chain_protos[i].name;
252
253 return NULL;
254}
255
Phil Oester58179b12006-07-20 17:00:19 +0000256int
257service_to_port(const char *name, const char *proto)
258{
259 struct servent *service;
260
261 if ((service = getservbyname(name, proto)) != NULL)
262 return ntohs((unsigned short) service->s_port);
263
264 return -1;
265}
266
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000267u_int16_t
268parse_port(const char *port, const char *proto)
269{
270 unsigned int portnum;
271
272 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
273 (portnum = service_to_port(port, proto)) != -1)
274 return (u_int16_t)portnum;
275
276 exit_error(PARAMETER_PROBLEM,
277 "invalid port/service `%s' specified", port);
278}
279
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280static void
281in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
282{
283 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000284 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285}
286
Pablo Neiradfdcd642005-05-29 19:05:23 +0000287static void free_opts(int reset_offset)
288{
289 if (opts != original_opts) {
290 free(opts);
291 opts = original_opts;
292 if (reset_offset)
293 global_option_offset = 0;
294 }
295}
296
Rusty Russell5eed48a2000-06-02 20:12:24 +0000297void
298exit_error(enum exittype status, char *msg, ...)
299{
300 va_list args;
301
302 va_start(args, msg);
303 fprintf(stderr, "%s v%s: ", program_name, program_version);
304 vfprintf(stderr, msg, args);
305 va_end(args);
306 fprintf(stderr, "\n");
307 if (status == PARAMETER_PROBLEM)
308 exit_tryhelp(status);
309 if (status == VERSION_PROBLEM)
310 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000311 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000312 /* On error paths, make sure that we don't leak memory */
313 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314 exit(status);
315}
316
317void
318exit_tryhelp(int status)
319{
Harald Weltea8658ca2003-03-05 07:46:15 +0000320 if (line != -1)
321 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
323 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000324 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000325 exit(status);
326}
327
328void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000329exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000330{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000331 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332 struct ip6tables_target *t = NULL;
333
334 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000335"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000336" %s -[RI] chain rulenum rule-specification [options]\n"
337" %s -D chain rulenum [options]\n"
338" %s -[LFZ] [chain] [options]\n"
339" %s -[NX] chain\n"
340" %s -E old-chain-name new-chain-name\n"
341" %s -P chain target [options]\n"
342" %s -h (print this help information)\n\n",
343 program_name, program_version, program_name, program_name,
344 program_name, program_name, program_name, program_name,
345 program_name, program_name);
346
347 printf(
348"Commands:\n"
349"Either long or short options are allowed.\n"
350" --append -A chain Append to chain\n"
351" --delete -D chain Delete matching rule from chain\n"
352" --delete -D chain rulenum\n"
353" Delete rule rulenum (1 = first) from chain\n"
354" --insert -I chain [rulenum]\n"
355" Insert in chain as rulenum (default 1=first)\n"
356" --replace -R chain rulenum\n"
357" Replace rule rulenum (1 = first) in chain\n"
358" --list -L [chain] List the rules in a chain or all chains\n"
359" --flush -F [chain] Delete all rules in chain or all chains\n"
360" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361" --new -N chain Create a new user-defined chain\n"
362" --delete-chain\n"
363" -X [chain] Delete a user-defined chain\n"
364" --policy -P chain target\n"
365" Change policy on chain to target\n"
366" --rename-chain\n"
367" -E old-chain new-chain\n"
368" Change chain name, (moving any references)\n"
369
370"Options:\n"
371" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
372" --source -s [!] address[/mask]\n"
373" source specification\n"
374" --destination -d [!] address[/mask]\n"
375" destination specification\n"
376" --in-interface -i [!] input name[+]\n"
377" network interface name ([+] for wildcard)\n"
378" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000379" target for rule (may load target extension)\n"
380" --match -m match\n"
381" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000382" --numeric -n numeric output of addresses and ports\n"
383" --out-interface -o [!] output name[+]\n"
384" network interface name ([+] for wildcard)\n"
385" --table -t table table to manipulate (default: `filter')\n"
386" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000387" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000388" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000389/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000390" --modprobe=<command> try to insert modules using this command\n"
391" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000392"[!] --version -V print package version.\n");
393
394 /* Print out any special helps. A user might like to be able to add a --help
395 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000396 specified matches & targets */
397 for (t = ip6tables_targets; t; t = t->next) {
398 if (t->used) {
399 printf("\n");
400 t->help();
401 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000402 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000403 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000404 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000405 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000406 }
407 exit(0);
408}
409
410static void
411generic_opt_check(int command, int options)
412{
413 int i, j, legal = 0;
414
415 /* Check that commands are valid with options. Complicated by the
416 * fact that if an option is legal with *any* command given, it is
417 * legal overall (ie. -z and -l).
418 */
419 for (i = 0; i < NUMBER_OF_OPT; i++) {
420 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
421
422 for (j = 0; j < NUMBER_OF_CMD; j++) {
423 if (!(command & (1<<j)))
424 continue;
425
426 if (!(options & (1<<i))) {
427 if (commands_v_options[j][i] == '+')
428 exit_error(PARAMETER_PROBLEM,
429 "You need to supply the `-%c' "
430 "option for this command\n",
431 optflags[i]);
432 } else {
433 if (commands_v_options[j][i] != 'x')
434 legal = 1;
435 else if (legal == 0)
436 legal = -1;
437 }
438 }
439 if (legal == -1)
440 exit_error(PARAMETER_PROBLEM,
441 "Illegal option `-%c' with this command\n",
442 optflags[i]);
443 }
444}
445
446static char
447opt2char(int option)
448{
449 const char *ptr;
450 for (ptr = optflags; option > 1; option >>= 1, ptr++);
451
452 return *ptr;
453}
454
455static char
456cmd2char(int option)
457{
458 const char *ptr;
459 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
460
461 return *ptr;
462}
463
464static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000465add_command(unsigned int *cmd, const int newcmd, const int othercmds,
466 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467{
468 if (invert)
469 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
470 if (*cmd & (~othercmds))
471 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
472 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
473 *cmd |= newcmd;
474}
475
476int
Harald Welteb77f1da2002-03-14 11:35:58 +0000477check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478{
479 if (option && strcmp(option, "!") == 0) {
480 if (*invert)
481 exit_error(PARAMETER_PROBLEM,
482 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000484 if (optind) {
485 *optind = *optind+1;
486 if (argc && *optind > argc)
487 exit_error(PARAMETER_PROBLEM,
488 "no argument following `!'");
489 }
490
Rusty Russell5eed48a2000-06-02 20:12:24 +0000491 return TRUE;
492 }
493 return FALSE;
494}
495
496static void *
497fw_calloc(size_t count, size_t size)
498{
499 void *p;
500
501 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000502 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000503 exit(1);
504 }
505 return p;
506}
507
508static void *
509fw_malloc(size_t size)
510{
511 void *p;
512
513 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000514 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515 exit(1);
516 }
517 return p;
518}
519
Rusty Russell5eed48a2000-06-02 20:12:24 +0000520static char *
521addr_to_numeric(const struct in6_addr *addrp)
522{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000523 /* 0000:0000:0000:0000:0000:000.000.000.000
524 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000525 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000526 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000527}
528
529static struct in6_addr *
530numeric_to_addr(const char *num)
531{
532 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000533 int err;
534 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000535 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000536#ifdef DEBUG
537 fprintf(stderr, "\nnumeric2addr: %d\n", err);
538#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000539 return (struct in6_addr *)NULL;
540}
541
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000542
543static struct in6_addr *
544host_to_addr(const char *name, unsigned int *naddr)
545{
546 struct addrinfo hints;
547 struct addrinfo *res;
548 static struct in6_addr *addr;
549 int err;
550
551 memset(&hints, 0, sizeof(hints));
552 hints.ai_flags=AI_CANONNAME;
553 hints.ai_family=AF_INET6;
554 hints.ai_socktype=SOCK_RAW;
555 hints.ai_protocol=41;
556 hints.ai_next=NULL;
557
558 *naddr = 0;
559 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
560#ifdef DEBUG
561 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
562#endif
563 return (struct in6_addr *) NULL;
564 } else {
565 if (res->ai_family != AF_INET6 ||
566 res->ai_addrlen != sizeof(struct sockaddr_in6))
567 return (struct in6_addr *) NULL;
568
569#ifdef DEBUG
570 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
571 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
572#endif
573 /* Get the first element of the address-chain */
574 addr = fw_calloc(1, sizeof(struct in6_addr));
575 in6addrcpy(addr, (struct in6_addr *)
576 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
577 freeaddrinfo(res);
578 *naddr = 1;
579 return addr;
580 }
581
582 return (struct in6_addr *) NULL;
583}
584
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000585static char *
586addr_to_host(const struct in6_addr *addr)
587{
588 struct sockaddr_in6 saddr;
589 int err;
590 static char hostname[NI_MAXHOST];
591
592 memset(&saddr, 0, sizeof(struct sockaddr_in6));
593 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000594 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000595
596 if ( (err=getnameinfo((struct sockaddr *)&saddr,
597 sizeof(struct sockaddr_in6),
598 hostname, sizeof(hostname)-1,
599 NULL, 0, 0)) != 0 ){
600#ifdef DEBUG
601 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
602#endif
603 return (char *) NULL;
604 } else {
605#ifdef DEBUG
606 fprintf (stderr, "\naddr2host: %s\n", hostname);
607#endif
608
609 return hostname;
610 }
611
612 return (char *) NULL;
613}
614
Rusty Russell5eed48a2000-06-02 20:12:24 +0000615static char *
616mask_to_numeric(const struct in6_addr *addrp)
617{
Harald Welte8a50ab82003-06-24 18:15:59 +0000618 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000619 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000620 if (l == -1) {
621 strcpy(buf, "/");
622 strcat(buf, addr_to_numeric(addrp));
623 return buf;
624 }
Harald Welte43ac1912002-03-03 09:43:07 +0000625 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000626 return buf;
627}
628
629static struct in6_addr *
630network_to_addr(const char *name)
631{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000632 /* abort();*/
633 /* TODO: not implemented yet, but the exception breaks the
634 * name resolvation */
635 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000636}
637
638static char *
639addr_to_anyname(const struct in6_addr *addr)
640{
641 char *name;
642
643 if ((name = addr_to_host(addr)) != NULL)
644 return name;
645
646 return addr_to_numeric(addr);
647}
648
649/*
650 * All functions starting with "parse" should succeed, otherwise
651 * the program fails.
652 * Most routines return pointers to static data that may change
653 * between calls to the same or other routines with a few exceptions:
654 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
655 * return global static data.
656*/
657
658static struct in6_addr *
659parse_hostnetwork(const char *name, unsigned int *naddrs)
660{
661 struct in6_addr *addrp, *addrptmp;
662
663 if ((addrptmp = numeric_to_addr(name)) != NULL ||
664 (addrptmp = network_to_addr(name)) != NULL) {
665 addrp = fw_malloc(sizeof(struct in6_addr));
666 in6addrcpy(addrp, addrptmp);
667 *naddrs = 1;
668 return addrp;
669 }
670 if ((addrp = host_to_addr(name, naddrs)) != NULL)
671 return addrp;
672
673 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
674}
675
676static struct in6_addr *
677parse_mask(char *mask)
678{
679 static struct in6_addr maskaddr;
680 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000681 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000682
683 if (mask == NULL) {
684 /* no mask at all defaults to 128 bits */
685 memset(&maskaddr, 0xff, sizeof maskaddr);
686 return &maskaddr;
687 }
688 if ((addrp = numeric_to_addr(mask)) != NULL)
689 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000690 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000691 exit_error(PARAMETER_PROBLEM,
692 "invalid mask `%s' specified", mask);
693 if (bits != 0) {
694 char *p = (char *)&maskaddr;
695 memset(p, 0xff, bits / 8);
696 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
697 p[bits / 8] = 0xff << (8 - (bits & 7));
698 return &maskaddr;
699 }
700
701 memset(&maskaddr, 0, sizeof maskaddr);
702 return &maskaddr;
703}
704
Harald Welte43ac1912002-03-03 09:43:07 +0000705void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000706parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
707 struct in6_addr *maskp, unsigned int *naddrs)
708{
709 struct in6_addr *addrp;
710 char buf[256];
711 char *p;
712 int i, j, n;
713
714 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000715 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716 if ((p = strrchr(buf, '/')) != NULL) {
717 *p = '\0';
718 addrp = parse_mask(p + 1);
719 } else
720 addrp = parse_mask(NULL);
721 in6addrcpy(maskp, addrp);
722
723 /* if a null mask is given, the name is ignored, like in "any/0" */
724 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
725 strcpy(buf, "::");
726
727 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
728 n = *naddrs;
729 for (i = 0, j = 0; i < n; i++) {
730 int k;
731 for (k = 0; k < 4; k++)
732 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
733 j++;
734 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000735 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 (*naddrs)--;
737 j--;
738 break;
739 }
740 }
741 }
742}
743
744struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000745find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746{
747 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000748 const char *icmp6 = "icmp6";
749 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000750
751 /* This is ugly as hell. Nonetheless, there is no way of changing
752 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000753 if ( (strcmp(match_name,"icmpv6") == 0) ||
754 (strcmp(match_name,"ipv6-icmp") == 0) ||
755 (strcmp(match_name,"icmp6") == 0) )
756 name = icmp6;
757 else
758 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000759
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000760 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
761 if (strcmp(name, ptr->name) == 0) {
762 struct ip6tables_match *clone;
763
764 /* First match of this type: */
765 if (ptr->m == NULL)
766 break;
767
768 /* Second and subsequent clones */
769 clone = fw_malloc(sizeof(struct ip6tables_match));
770 memcpy(clone, ptr, sizeof(struct ip6tables_match));
771 clone->mflags = 0;
772 /* This is a clone: */
773 clone->next = clone;
774
775 ptr = clone;
776 break;
777 }
778 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000779
Harald Welte3efb6ea2001-08-06 18:50:21 +0000780#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000781 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000782 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000784 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000785 if (dlopen(path, RTLD_NOW)) {
786 /* Found library. If it didn't register itself,
787 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000788 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789
790 if (!ptr)
791 exit_error(PARAMETER_PROBLEM,
792 "Couldn't load match `%s'\n",
793 name);
794 } else if (tryload == LOAD_MUST_SUCCEED)
795 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000796 "Couldn't load match `%s':%s\n",
797 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000799#else
800 if (ptr && !ptr->loaded) {
801 if (tryload != DONT_LOAD)
802 ptr->loaded = 1;
803 else
804 ptr = NULL;
805 }
Marc Boucher067477b2002-03-24 15:09:31 +0000806 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
807 exit_error(PARAMETER_PROBLEM,
808 "Couldn't find match `%s'\n", name);
809 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000810#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000812 if (ptr && matches) {
813 struct ip6tables_rule_match **i;
814 struct ip6tables_rule_match *newentry;
815
816 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
817
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000818 for (i = matches; *i; i = &(*i)->next) {
819 if (strcmp(name, (*i)->match->name) == 0)
820 (*i)->completed = 1;
821 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000822 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000823 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000824 newentry->next = NULL;
825 *i = newentry;
826 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000827
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828 return ptr;
829}
830
831/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
832static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000833find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000834{
Harald Welteed498492001-07-23 01:24:22 +0000835 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836
Harald Welte43ac1912002-03-03 09:43:07 +0000837 if (string_to_number(pname, 0, 255, &proto) != -1) {
838 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000839
Harald Welte43ac1912002-03-03 09:43:07 +0000840 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000841 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000842 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000843 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000844
845 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846}
847
Harald Welte43ac1912002-03-03 09:43:07 +0000848u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849parse_protocol(const char *s)
850{
Harald Welteed498492001-07-23 01:24:22 +0000851 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000852
Harald Welteed498492001-07-23 01:24:22 +0000853 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000854 struct protoent *pent;
855
Harald Weltecbe1ec72006-02-11 09:50:11 +0000856 /* first deal with the special case of 'all' to prevent
857 * people from being able to redefine 'all' in nsswitch
858 * and/or provoke expensive [not working] ldap/nis/...
859 * lookups */
860 if (!strcmp(s, "all"))
861 return 0;
862
Rusty Russell5eed48a2000-06-02 20:12:24 +0000863 if ((pent = getprotobyname(s)))
864 proto = pent->p_proto;
865 else {
866 unsigned int i;
867 for (i = 0;
868 i < sizeof(chain_protos)/sizeof(struct pprot);
869 i++) {
870 if (strcmp(s, chain_protos[i].name) == 0) {
871 proto = chain_protos[i].num;
872 break;
873 }
874 }
875 if (i == sizeof(chain_protos)/sizeof(struct pprot))
876 exit_error(PARAMETER_PROBLEM,
877 "unknown protocol `%s' specified",
878 s);
879 }
880 }
881
882 return (u_int16_t)proto;
883}
884
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000885/* proto means IPv6 extension header ? */
886static int is_exthdr(u_int16_t proto)
887{
888 return (proto == IPPROTO_HOPOPTS ||
889 proto == IPPROTO_ROUTING ||
890 proto == IPPROTO_FRAGMENT ||
891 proto == IPPROTO_ESP ||
892 proto == IPPROTO_AH ||
893 proto == IPPROTO_DSTOPTS);
894}
895
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000896void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897{
898 int vialen = strlen(arg);
899 unsigned int i;
900
901 memset(mask, 0, IFNAMSIZ);
902 memset(vianame, 0, IFNAMSIZ);
903
904 if (vialen + 1 > IFNAMSIZ)
905 exit_error(PARAMETER_PROBLEM,
906 "interface name `%s' must be shorter than IFNAMSIZ"
907 " (%i)", arg, IFNAMSIZ-1);
908
909 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000910 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000911 memset(mask, 0, IFNAMSIZ);
912 else if (vianame[vialen - 1] == '+') {
913 memset(mask, 0xFF, vialen - 1);
914 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000915 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916 } else {
917 /* Include nul-terminator in match */
918 memset(mask, 0xFF, vialen + 1);
919 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000920 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000921 if (vianame[i] == ':' ||
922 vianame[i] == '!' ||
923 vianame[i] == '*') {
924 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000925 " `%s' (No aliases, :, ! or *).\n",
926 vianame);
927 break;
928 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000929 }
930 }
931}
932
933/* Can't be zero. */
934static int
935parse_rulenumber(const char *rule)
936{
Harald Welte43ac1912002-03-03 09:43:07 +0000937 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938
Harald Welte43ac1912002-03-03 09:43:07 +0000939 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000940 exit_error(PARAMETER_PROBLEM,
941 "Invalid rule number `%s'", rule);
942
943 return rulenum;
944}
945
946static const char *
947parse_target(const char *targetname)
948{
949 const char *ptr;
950
951 if (strlen(targetname) < 1)
952 exit_error(PARAMETER_PROBLEM,
953 "Invalid target name (too short)");
954
955 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
956 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000957 "Invalid target name `%s' (%u chars max)",
958 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000959
960 for (ptr = targetname; *ptr; ptr++)
961 if (isspace(*ptr))
962 exit_error(PARAMETER_PROBLEM,
963 "Invalid target name `%s'", targetname);
964 return targetname;
965}
966
967int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000968string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
969 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000970{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000971 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000972 char *end;
973
974 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000975 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000976 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977 if (*end == '\0' && end != s) {
978 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000979 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000980 *ret = number;
981 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000982 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 }
984 return -1;
985}
986
Martin Josefssonb105bc92004-05-26 15:54:49 +0000987int
988string_to_number_l(const char *s, unsigned long min, unsigned long max,
989 unsigned long *ret)
990{
991 int result;
992 unsigned long long number;
993
994 result = string_to_number_ll(s, min, max, &number);
995 *ret = (unsigned long)number;
996
997 return result;
998}
999
1000int string_to_number(const char *s, unsigned int min, unsigned int max,
1001 unsigned int *ret)
1002{
1003 int result;
1004 unsigned long number;
1005
1006 result = string_to_number_l(s, min, max, &number);
1007 *ret = (unsigned int)number;
1008
1009 return result;
1010}
1011
Rusty Russell5eed48a2000-06-02 20:12:24 +00001012static void
1013set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1014 int invert)
1015{
1016 if (*options & option)
1017 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1018 opt2char(option));
1019 *options |= option;
1020
1021 if (invert) {
1022 unsigned int i;
1023 for (i = 0; 1 << i != option; i++);
1024
1025 if (!inverse_for_options[i])
1026 exit_error(PARAMETER_PROBLEM,
1027 "cannot have ! before -%c",
1028 opt2char(option));
1029 *invflg |= inverse_for_options[i];
1030 }
1031}
1032
1033struct ip6tables_target *
1034find_target(const char *name, enum ip6t_tryload tryload)
1035{
1036 struct ip6tables_target *ptr;
1037
1038 /* Standard target? */
1039 if (strcmp(name, "") == 0
1040 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1041 || strcmp(name, IP6TC_LABEL_DROP) == 0
1042 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1043 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1044 name = "standard";
1045
1046 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1047 if (strcmp(name, ptr->name) == 0)
1048 break;
1049 }
1050
Harald Welte3efb6ea2001-08-06 18:50:21 +00001051#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001052 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001053 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001054 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001055 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001056 if (dlopen(path, RTLD_NOW)) {
1057 /* Found library. If it didn't register itself,
1058 maybe they specified match as a target. */
1059 ptr = find_target(name, DONT_LOAD);
1060 if (!ptr)
1061 exit_error(PARAMETER_PROBLEM,
1062 "Couldn't load target `%s'\n",
1063 name);
1064 } else if (tryload == LOAD_MUST_SUCCEED)
1065 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001066 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001067 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001068 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001069#else
1070 if (ptr && !ptr->loaded) {
1071 if (tryload != DONT_LOAD)
1072 ptr->loaded = 1;
1073 else
1074 ptr = NULL;
1075 }
Marc Boucher067477b2002-03-24 15:09:31 +00001076 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1077 exit_error(PARAMETER_PROBLEM,
1078 "Couldn't find target `%s'\n", name);
1079 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001080#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001081
Harald Welte43ac1912002-03-03 09:43:07 +00001082 if (ptr)
1083 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001084
Rusty Russell5eed48a2000-06-02 20:12:24 +00001085 return ptr;
1086}
1087
1088static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001089merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001090 unsigned int *option_offset)
1091{
1092 unsigned int num_old, num_new, i;
1093 struct option *merge;
1094
1095 for (num_old = 0; oldopts[num_old].name; num_old++);
1096 for (num_new = 0; newopts[num_new].name; num_new++);
1097
1098 global_option_offset += OPTION_OFFSET;
1099 *option_offset = global_option_offset;
1100
1101 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1102 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001103 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001104 for (i = 0; i < num_new; i++) {
1105 merge[num_old + i] = newopts[i];
1106 merge[num_old + i].val += *option_offset;
1107 }
1108 memset(merge + num_old + num_new, 0, sizeof(struct option));
1109
1110 return merge;
1111}
1112
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001113static int compatible_revision(const char *name, u_int8_t revision, int opt)
1114{
1115 struct ip6t_get_revision rev;
1116 socklen_t s = sizeof(rev);
1117 int max_rev, sockfd;
1118
1119 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1120 if (sockfd < 0) {
1121 fprintf(stderr, "Could not open socket to kernel: %s\n",
1122 strerror(errno));
1123 exit(1);
1124 }
1125
1126 strcpy(rev.name, name);
1127 rev.revision = revision;
1128
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001129 load_ip6tables_ko(modprobe);
1130
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001131 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1132 if (max_rev < 0) {
1133 /* Definitely don't support this? */
1134 if (errno == EPROTONOSUPPORT) {
1135 close(sockfd);
1136 return 0;
1137 } else if (errno == ENOPROTOOPT) {
1138 close(sockfd);
1139 /* Assume only revision 0 support (old kernel) */
1140 return (revision == 0);
1141 } else {
1142 fprintf(stderr, "getsockopt failed strangely: %s\n",
1143 strerror(errno));
1144 exit(1);
1145 }
1146 }
1147 close(sockfd);
1148 return 1;
1149}
1150
1151static int compatible_match_revision(const char *name, u_int8_t revision)
1152{
1153 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1154}
1155
Rusty Russell5eed48a2000-06-02 20:12:24 +00001156void
1157register_match6(struct ip6tables_match *me)
1158{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001159 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001160
Rusty Russell5eed48a2000-06-02 20:12:24 +00001161 if (strcmp(me->version, program_version) != 0) {
1162 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1163 program_name, me->name, me->version, program_version);
1164 exit(1);
1165 }
1166
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001167 /* Revision field stole a char from name. */
1168 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1169 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001170 program_name, me->name);
1171 exit(1);
1172 }
1173
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001174 old = find_match(me->name, DURING_LOAD, NULL);
1175 if (old) {
1176 if (old->revision == me->revision) {
1177 fprintf(stderr,
1178 "%s: match `%s' already registered.\n",
1179 program_name, me->name);
1180 exit(1);
1181 }
1182
1183 /* Now we have two (or more) options, check compatibility. */
1184 if (compatible_match_revision(old->name, old->revision)
1185 && old->revision > me->revision)
1186 return;
1187
1188 /* Replace if compatible. */
1189 if (!compatible_match_revision(me->name, me->revision))
1190 return;
1191
1192 /* Delete old one. */
1193 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1194 *i = old->next;
1195 }
1196
Harald Welte43ac1912002-03-03 09:43:07 +00001197 if (me->size != IP6T_ALIGN(me->size)) {
1198 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001199 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001200 exit(1);
1201 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001202
Harald Welte43ac1912002-03-03 09:43:07 +00001203 /* Append to list. */
1204 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1205 me->next = NULL;
1206 *i = me;
1207
Rusty Russell5eed48a2000-06-02 20:12:24 +00001208 me->m = NULL;
1209 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001210}
1211
1212void
1213register_target6(struct ip6tables_target *me)
1214{
1215 if (strcmp(me->version, program_version) != 0) {
1216 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1217 program_name, me->name, me->version, program_version);
1218 exit(1);
1219 }
1220
Jones Desougif5b86e62005-12-22 03:33:50 +00001221 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001222 fprintf(stderr, "%s: target `%s' already registered.\n",
1223 program_name, me->name);
1224 exit(1);
1225 }
1226
Harald Welte43ac1912002-03-03 09:43:07 +00001227 if (me->size != IP6T_ALIGN(me->size)) {
1228 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001229 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001230 exit(1);
1231 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001232
Rusty Russell5eed48a2000-06-02 20:12:24 +00001233 /* Prepend to list. */
1234 me->next = ip6tables_targets;
1235 ip6tables_targets = me;
1236 me->t = NULL;
1237 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001238}
1239
1240static void
1241print_num(u_int64_t number, unsigned int format)
1242{
Harald Welte43ac1912002-03-03 09:43:07 +00001243 if (format & FMT_KILOMEGAGIGA) {
1244 if (number > 99999) {
1245 number = (number + 500) / 1000;
1246 if (number > 9999) {
1247 number = (number + 500) / 1000;
1248 if (number > 9999) {
1249 number = (number + 500) / 1000;
1250 if (number > 9999) {
1251 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001252 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001253 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001254 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001255 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001256 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001257 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001258 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001259 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001260 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001261 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001262 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001263}
1264
Harald Welte43ac1912002-03-03 09:43:07 +00001265
Rusty Russell5eed48a2000-06-02 20:12:24 +00001266static void
1267print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1268{
1269 struct ip6t_counters counters;
1270 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1271 printf("Chain %s", chain);
1272 if (pol) {
1273 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001274 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001275 fputc(' ', stdout);
1276 print_num(counters.pcnt, (format|FMT_NOTABLE));
1277 fputs("packets, ", stdout);
1278 print_num(counters.bcnt, (format|FMT_NOTABLE));
1279 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001280 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001281 printf(")\n");
1282 } else {
1283 unsigned int refs;
1284 if (!ip6tc_get_references(&refs, chain, handle))
1285 printf(" (ERROR obtaining refs)\n");
1286 else
1287 printf(" (%u references)\n", refs);
1288 }
1289
1290 if (format & FMT_LINENUMBERS)
1291 printf(FMT("%-4s ", "%s "), "num");
1292 if (!(format & FMT_NOCOUNTS)) {
1293 if (format & FMT_KILOMEGAGIGA) {
1294 printf(FMT("%5s ","%s "), "pkts");
1295 printf(FMT("%5s ","%s "), "bytes");
1296 } else {
1297 printf(FMT("%8s ","%s "), "pkts");
1298 printf(FMT("%10s ","%s "), "bytes");
1299 }
1300 }
1301 if (!(format & FMT_NOTARGET))
1302 printf(FMT("%-9s ","%s "), "target");
1303 fputs(" prot ", stdout);
1304 if (format & FMT_OPTIONS)
1305 fputs("opt", stdout);
1306 if (format & FMT_VIA) {
1307 printf(FMT(" %-6s ","%s "), "in");
1308 printf(FMT("%-6s ","%s "), "out");
1309 }
1310 printf(FMT(" %-19s ","%s "), "source");
1311 printf(FMT(" %-19s "," %s "), "destination");
1312 printf("\n");
1313}
1314
Harald Welte43ac1912002-03-03 09:43:07 +00001315
Rusty Russell5eed48a2000-06-02 20:12:24 +00001316static int
1317print_match(const struct ip6t_entry_match *m,
1318 const struct ip6t_ip6 *ip,
1319 int numeric)
1320{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001321 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001322
1323 if (match) {
1324 if (match->print)
1325 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001326 else
1327 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001328 } else {
1329 if (m->u.user.name[0])
1330 printf("UNKNOWN match `%s' ", m->u.user.name);
1331 }
1332 /* Don't stop iterating. */
1333 return 0;
1334}
1335
1336/* e is called `fw' here for hysterical raisins */
1337static void
1338print_firewall(const struct ip6t_entry *fw,
1339 const char *targname,
1340 unsigned int num,
1341 unsigned int format,
1342 const ip6tc_handle_t handle)
1343{
1344 struct ip6tables_target *target = NULL;
1345 const struct ip6t_entry_target *t;
1346 u_int8_t flags;
1347 char buf[BUFSIZ];
1348
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349 if (!ip6tc_is_chain(targname, handle))
1350 target = find_target(targname, TRY_LOAD);
1351 else
1352 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1353
1354 t = ip6t_get_target((struct ip6t_entry *)fw);
1355 flags = fw->ipv6.flags;
1356
1357 if (format & FMT_LINENUMBERS)
1358 printf(FMT("%-4u ", "%u "), num+1);
1359
1360 if (!(format & FMT_NOCOUNTS)) {
1361 print_num(fw->counters.pcnt, format);
1362 print_num(fw->counters.bcnt, format);
1363 }
1364
1365 if (!(format & FMT_NOTARGET))
1366 printf(FMT("%-9s ", "%s "), targname);
1367
1368 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1369 {
Harald Welte43ac1912002-03-03 09:43:07 +00001370 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001371 if (pname)
1372 printf(FMT("%-5s", "%s "), pname);
1373 else
1374 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1375 }
1376
1377 if (format & FMT_OPTIONS) {
1378 if (format & FMT_NOTABLE)
1379 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001380 fputc(' ', stdout); /* Invert flag of FRAG */
1381 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001382 fputc(' ', stdout);
1383 }
1384
1385 if (format & FMT_VIA) {
1386 char iface[IFNAMSIZ+2];
1387
1388 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1389 iface[0] = '!';
1390 iface[1] = '\0';
1391 }
1392 else iface[0] = '\0';
1393
1394 if (fw->ipv6.iniface[0] != '\0') {
1395 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396 }
1397 else if (format & FMT_NUMERIC) strcat(iface, "*");
1398 else strcat(iface, "any");
1399 printf(FMT(" %-6s ","in %s "), iface);
1400
1401 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1402 iface[0] = '!';
1403 iface[1] = '\0';
1404 }
1405 else iface[0] = '\0';
1406
1407 if (fw->ipv6.outiface[0] != '\0') {
1408 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001409 }
1410 else if (format & FMT_NUMERIC) strcat(iface, "*");
1411 else strcat(iface, "any");
1412 printf(FMT("%-6s ","out %s "), iface);
1413 }
1414
1415 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1416 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1417 && !(format & FMT_NUMERIC))
1418 printf(FMT("%-19s ","%s "), "anywhere");
1419 else {
1420 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001421 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 else
Harald Welte43ac1912002-03-03 09:43:07 +00001423 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1425 printf(FMT("%-19s ","%s "), buf);
1426 }
1427
1428 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1429 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1430 && !(format & FMT_NUMERIC))
1431 printf(FMT("%-19s","-> %s"), "anywhere");
1432 else {
1433 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001434 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001435 else
Harald Welte43ac1912002-03-03 09:43:07 +00001436 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001437 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1438 printf(FMT("%-19s","-> %s"), buf);
1439 }
1440
1441 if (format & FMT_NOTABLE)
1442 fputs(" ", stdout);
1443
1444 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1445
1446 if (target) {
1447 if (target->print)
1448 /* Print the target information. */
1449 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1450 } else if (t->u.target_size != sizeof(*t))
1451 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001452 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001453
1454 if (!(format & FMT_NONEWLINE))
1455 fputc('\n', stdout);
1456}
1457
1458static void
1459print_firewall_line(const struct ip6t_entry *fw,
1460 const ip6tc_handle_t h)
1461{
1462 struct ip6t_entry_target *t;
1463
1464 t = ip6t_get_target((struct ip6t_entry *)fw);
1465 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1466}
1467
1468static int
1469append_entry(const ip6t_chainlabel chain,
1470 struct ip6t_entry *fw,
1471 unsigned int nsaddrs,
1472 const struct in6_addr saddrs[],
1473 unsigned int ndaddrs,
1474 const struct in6_addr daddrs[],
1475 int verbose,
1476 ip6tc_handle_t *handle)
1477{
1478 unsigned int i, j;
1479 int ret = 1;
1480
1481 for (i = 0; i < nsaddrs; i++) {
1482 fw->ipv6.src = saddrs[i];
1483 for (j = 0; j < ndaddrs; j++) {
1484 fw->ipv6.dst = daddrs[j];
1485 if (verbose)
1486 print_firewall_line(fw, *handle);
1487 ret &= ip6tc_append_entry(chain, fw, handle);
1488 }
1489 }
1490
1491 return ret;
1492}
1493
1494static int
1495replace_entry(const ip6t_chainlabel chain,
1496 struct ip6t_entry *fw,
1497 unsigned int rulenum,
1498 const struct in6_addr *saddr,
1499 const struct in6_addr *daddr,
1500 int verbose,
1501 ip6tc_handle_t *handle)
1502{
1503 fw->ipv6.src = *saddr;
1504 fw->ipv6.dst = *daddr;
1505
1506 if (verbose)
1507 print_firewall_line(fw, *handle);
1508 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1509}
1510
1511static int
1512insert_entry(const ip6t_chainlabel chain,
1513 struct ip6t_entry *fw,
1514 unsigned int rulenum,
1515 unsigned int nsaddrs,
1516 const struct in6_addr saddrs[],
1517 unsigned int ndaddrs,
1518 const struct in6_addr daddrs[],
1519 int verbose,
1520 ip6tc_handle_t *handle)
1521{
1522 unsigned int i, j;
1523 int ret = 1;
1524
1525 for (i = 0; i < nsaddrs; i++) {
1526 fw->ipv6.src = saddrs[i];
1527 for (j = 0; j < ndaddrs; j++) {
1528 fw->ipv6.dst = daddrs[j];
1529 if (verbose)
1530 print_firewall_line(fw, *handle);
1531 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1532 }
1533 }
1534
1535 return ret;
1536}
1537
1538static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001539make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001540{
1541 /* Establish mask for comparison */
1542 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001543 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544 unsigned char *mask, *mptr;
1545
1546 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001547 for (matchp = matches; matchp; matchp = matchp->next)
1548 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549
1550 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001551 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001552 + ip6tables_targets->size);
1553
1554 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1555 mptr = mask + sizeof(struct ip6t_entry);
1556
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001557 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001558 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001559 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001560 + matchp->match->userspacesize);
1561 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001562 }
1563
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001564 memset(mptr, 0xFF,
1565 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1566 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567
1568 return mask;
1569}
1570
1571static int
1572delete_entry(const ip6t_chainlabel chain,
1573 struct ip6t_entry *fw,
1574 unsigned int nsaddrs,
1575 const struct in6_addr saddrs[],
1576 unsigned int ndaddrs,
1577 const struct in6_addr daddrs[],
1578 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001579 ip6tc_handle_t *handle,
1580 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581{
1582 unsigned int i, j;
1583 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001584 unsigned char *mask;
1585
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001586 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001588 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001589 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001590 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001591 if (verbose)
1592 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001593 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001594 }
1595 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001596 free(mask);
1597
Rusty Russell5eed48a2000-06-02 20:12:24 +00001598 return ret;
1599}
1600
András Kis-Szabó764316a2001-02-26 17:31:20 +00001601int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001602for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1603 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1604{
1605 int ret = 1;
1606 const char *chain;
1607 char *chains;
1608 unsigned int i, chaincount = 0;
1609
1610 chain = ip6tc_first_chain(handle);
1611 while (chain) {
1612 chaincount++;
1613 chain = ip6tc_next_chain(handle);
1614 }
1615
1616 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1617 i = 0;
1618 chain = ip6tc_first_chain(handle);
1619 while (chain) {
1620 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1621 i++;
1622 chain = ip6tc_next_chain(handle);
1623 }
1624
1625 for (i = 0; i < chaincount; i++) {
1626 if (!builtinstoo
1627 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001628 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 continue;
1630 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1631 }
1632
1633 free(chains);
1634 return ret;
1635}
1636
András Kis-Szabó764316a2001-02-26 17:31:20 +00001637int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001638flush_entries(const ip6t_chainlabel chain, int verbose,
1639 ip6tc_handle_t *handle)
1640{
1641 if (!chain)
1642 return for_each_chain(flush_entries, verbose, 1, handle);
1643
1644 if (verbose)
1645 fprintf(stdout, "Flushing chain `%s'\n", chain);
1646 return ip6tc_flush_entries(chain, handle);
1647}
1648
1649static int
1650zero_entries(const ip6t_chainlabel chain, int verbose,
1651 ip6tc_handle_t *handle)
1652{
1653 if (!chain)
1654 return for_each_chain(zero_entries, verbose, 1, handle);
1655
1656 if (verbose)
1657 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1658 return ip6tc_zero_entries(chain, handle);
1659}
1660
András Kis-Szabó764316a2001-02-26 17:31:20 +00001661int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001662delete_chain(const ip6t_chainlabel chain, int verbose,
1663 ip6tc_handle_t *handle)
1664{
1665 if (!chain)
1666 return for_each_chain(delete_chain, verbose, 0, handle);
1667
1668 if (verbose)
1669 fprintf(stdout, "Deleting chain `%s'\n", chain);
1670 return ip6tc_delete_chain(chain, handle);
1671}
1672
1673static int
1674list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1675 int expanded, int linenumbers, ip6tc_handle_t *handle)
1676{
1677 int found = 0;
1678 unsigned int format;
1679 const char *this;
1680
1681 format = FMT_OPTIONS;
1682 if (!verbose)
1683 format |= FMT_NOCOUNTS;
1684 else
1685 format |= FMT_VIA;
1686
1687 if (numeric)
1688 format |= FMT_NUMERIC;
1689
1690 if (!expanded)
1691 format |= FMT_KILOMEGAGIGA;
1692
1693 if (linenumbers)
1694 format |= FMT_LINENUMBERS;
1695
1696 for (this = ip6tc_first_chain(handle);
1697 this;
1698 this = ip6tc_next_chain(handle)) {
1699 const struct ip6t_entry *i;
1700 unsigned int num;
1701
1702 if (chain && strcmp(chain, this) != 0)
1703 continue;
1704
1705 if (found) printf("\n");
1706
1707 print_header(format, this, handle);
1708 i = ip6tc_first_rule(this, handle);
1709
1710 num = 0;
1711 while (i) {
1712 print_firewall(i,
1713 ip6tc_get_target(i, handle),
1714 num++,
1715 format,
1716 *handle);
1717 i = ip6tc_next_rule(i, handle);
1718 }
1719 found = 1;
1720 }
1721
1722 errno = ENOENT;
1723 return found;
1724}
1725
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001726static char *get_modprobe(void)
1727{
Harald Welte43ac1912002-03-03 09:43:07 +00001728 int procfile;
1729 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001730
Harald Welte10f7f142004-10-22 08:14:07 +00001731#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001732 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1733 if (procfile < 0)
1734 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001735
Harald Welte10f7f142004-10-22 08:14:07 +00001736 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001737 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001738 memset(ret, 0, PROCFILE_BUFSIZ);
1739 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001740 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001741 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001742 }
1743 if (ret[strlen(ret)-1]=='\n')
1744 ret[strlen(ret)-1]=0;
1745 close(procfile);
1746 return ret;
1747 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001748 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001749 free(ret);
1750 close(procfile);
1751 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001752}
1753
Harald Welte58918652001-06-16 18:25:25 +00001754int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001755{
Harald Welte43ac1912002-03-03 09:43:07 +00001756 char *buf = NULL;
1757 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001758 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001759
Harald Welte43ac1912002-03-03 09:43:07 +00001760 /* If they don't explicitly set it, read out of kernel */
1761 if (!modprobe) {
1762 buf = get_modprobe();
1763 if (!buf)
1764 return -1;
1765 modprobe = buf;
1766 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001767
Harald Welte43ac1912002-03-03 09:43:07 +00001768 switch (fork()) {
1769 case 0:
1770 argv[0] = (char *)modprobe;
1771 argv[1] = (char *)modname;
1772 argv[2] = NULL;
1773 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001774
Harald Welte43ac1912002-03-03 09:43:07 +00001775 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001776 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001777 case -1:
1778 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001779
Harald Welte43ac1912002-03-03 09:43:07 +00001780 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001781 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001782 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001783
Harald Welte43ac1912002-03-03 09:43:07 +00001784 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001785 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1786 return 0;
1787 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001788}
1789
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001790int load_ip6tables_ko(const char *modprobe)
1791{
1792 static int loaded = 0;
1793 static int ret = -1;
1794
1795 if (!loaded) {
1796 ret = ip6tables_insmod("ip6_tables", modprobe);
1797 loaded = 1;
1798 }
1799
1800 return ret;
1801}
1802
Rusty Russell5eed48a2000-06-02 20:12:24 +00001803static struct ip6t_entry *
1804generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001805 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001806 struct ip6t_entry_target *target)
1807{
1808 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001809 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810 struct ip6t_entry *e;
1811
1812 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001813 for (matchp = matches; matchp; matchp = matchp->next)
1814 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815
1816 e = fw_malloc(size + target->u.target_size);
1817 *e = *fw;
1818 e->target_offset = size;
1819 e->next_offset = size + target->u.target_size;
1820
1821 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001822 for (matchp = matches; matchp; matchp = matchp->next) {
1823 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1824 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825 }
1826 memcpy(e->elems + size, target, target->u.target_size);
1827
1828 return e;
1829}
1830
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001831void clear_rule_matches(struct ip6tables_rule_match **matches)
1832{
1833 struct ip6tables_rule_match *matchp, *tmp;
1834
1835 for (matchp = *matches; matchp;) {
1836 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001837 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001838 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001839 matchp->match->m = NULL;
1840 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001841 if (matchp->match == matchp->match->next) {
1842 free(matchp->match);
1843 matchp->match = NULL;
1844 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001845 free(matchp);
1846 matchp = tmp;
1847 }
1848
1849 *matches = NULL;
1850}
1851
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001852static void set_revision(char *name, u_int8_t revision)
1853{
1854 /* Old kernel sources don't have ".revision" field,
1855 but we stole a byte from name. */
1856 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1857 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1858}
1859
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1861{
1862 struct ip6t_entry fw, *e = NULL;
1863 int invert = 0;
1864 unsigned int nsaddrs = 0, ndaddrs = 0;
1865 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1866
1867 int c, verbose = 0;
1868 const char *chain = NULL;
1869 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1870 const char *policy = NULL, *newname = NULL;
1871 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001872 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 int ret = 1;
1874 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001875 struct ip6tables_rule_match *matches = NULL;
1876 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001877 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001878 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001879 const char *jumpto = "";
1880 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001881 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882
1883 memset(&fw, 0, sizeof(fw));
1884
Harald Welte43ac1912002-03-03 09:43:07 +00001885 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001886 * a second time */
1887 optind = 0;
1888
Harald Welte43ac1912002-03-03 09:43:07 +00001889 /* clear mflags in case do_command gets called a second time
1890 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001891 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001892 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001893
Harald Welte43ac1912002-03-03 09:43:07 +00001894 for (t = ip6tables_targets; t; t = t->next) {
1895 t->tflags = 0;
1896 t->used = 0;
1897 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001898
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 /* Suppress error messages: we may add new options if we
1900 demand-load a protocol. */
1901 opterr = 0;
1902
1903 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001904 "-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 +00001905 opts, NULL)) != -1) {
1906 switch (c) {
1907 /*
1908 * Command selection
1909 */
1910 case 'A':
1911 add_command(&command, CMD_APPEND, CMD_NONE,
1912 invert);
1913 chain = optarg;
1914 break;
1915
1916 case 'D':
1917 add_command(&command, CMD_DELETE, CMD_NONE,
1918 invert);
1919 chain = optarg;
1920 if (optind < argc && argv[optind][0] != '-'
1921 && argv[optind][0] != '!') {
1922 rulenum = parse_rulenumber(argv[optind++]);
1923 command = CMD_DELETE_NUM;
1924 }
1925 break;
1926
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 case 'R':
1928 add_command(&command, CMD_REPLACE, CMD_NONE,
1929 invert);
1930 chain = optarg;
1931 if (optind < argc && argv[optind][0] != '-'
1932 && argv[optind][0] != '!')
1933 rulenum = parse_rulenumber(argv[optind++]);
1934 else
1935 exit_error(PARAMETER_PROBLEM,
1936 "-%c requires a rule number",
1937 cmd2char(CMD_REPLACE));
1938 break;
1939
1940 case 'I':
1941 add_command(&command, CMD_INSERT, CMD_NONE,
1942 invert);
1943 chain = optarg;
1944 if (optind < argc && argv[optind][0] != '-'
1945 && argv[optind][0] != '!')
1946 rulenum = parse_rulenumber(argv[optind++]);
1947 else rulenum = 1;
1948 break;
1949
1950 case 'L':
1951 add_command(&command, CMD_LIST, CMD_ZERO,
1952 invert);
1953 if (optarg) chain = optarg;
1954 else if (optind < argc && argv[optind][0] != '-'
1955 && argv[optind][0] != '!')
1956 chain = argv[optind++];
1957 break;
1958
1959 case 'F':
1960 add_command(&command, CMD_FLUSH, CMD_NONE,
1961 invert);
1962 if (optarg) chain = optarg;
1963 else if (optind < argc && argv[optind][0] != '-'
1964 && argv[optind][0] != '!')
1965 chain = argv[optind++];
1966 break;
1967
1968 case 'Z':
1969 add_command(&command, CMD_ZERO, CMD_LIST,
1970 invert);
1971 if (optarg) chain = optarg;
1972 else if (optind < argc && argv[optind][0] != '-'
1973 && argv[optind][0] != '!')
1974 chain = argv[optind++];
1975 break;
1976
1977 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001978 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001979 exit_error(PARAMETER_PROBLEM,
1980 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001981 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001982 if (find_target(optarg, TRY_LOAD))
1983 exit_error(PARAMETER_PROBLEM,
1984 "chain name may not clash "
1985 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001986 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1987 invert);
1988 chain = optarg;
1989 break;
1990
1991 case 'X':
1992 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1993 invert);
1994 if (optarg) chain = optarg;
1995 else if (optind < argc && argv[optind][0] != '-'
1996 && argv[optind][0] != '!')
1997 chain = argv[optind++];
1998 break;
1999
2000 case 'E':
2001 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2002 invert);
2003 chain = optarg;
2004 if (optind < argc && argv[optind][0] != '-'
2005 && argv[optind][0] != '!')
2006 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002007 else
2008 exit_error(PARAMETER_PROBLEM,
2009 "-%c requires old-chain-name and "
2010 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00002011 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002012 break;
2013
2014 case 'P':
2015 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2016 invert);
2017 chain = optarg;
2018 if (optind < argc && argv[optind][0] != '-'
2019 && argv[optind][0] != '!')
2020 policy = argv[optind++];
2021 else
2022 exit_error(PARAMETER_PROBLEM,
2023 "-%c requires a chain and a policy",
2024 cmd2char(CMD_SET_POLICY));
2025 break;
2026
2027 case 'h':
2028 if (!optarg)
2029 optarg = argv[optind];
2030
Jonas Berlin1b91e592005-04-01 06:58:38 +00002031 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002032 if (!matches && protocol)
2033 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002034
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002035 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002036
2037 /*
2038 * Option selection
2039 */
2040 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002041 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002042 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2043 invert);
2044
2045 /* Canonicalize into lower case */
2046 for (protocol = argv[optind-1]; *protocol; protocol++)
2047 *protocol = tolower(*protocol);
2048
2049 protocol = argv[optind-1];
2050 fw.ipv6.proto = parse_protocol(protocol);
2051 fw.ipv6.flags |= IP6T_F_PROTO;
2052
2053 if (fw.ipv6.proto == 0
2054 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2055 exit_error(PARAMETER_PROBLEM,
2056 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002057
2058 if (fw.ipv6.proto != IPPROTO_ESP &&
2059 is_exthdr(fw.ipv6.proto))
2060 printf("Warning: never matched protocol: %s. "
2061 "use exension match instead.", protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002062 break;
2063
2064 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002065 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002066 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2067 invert);
2068 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002069 break;
2070
2071 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002072 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2074 invert);
2075 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002076 break;
2077
2078 case 'j':
2079 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2080 invert);
2081 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00002082 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002083 target = find_target(jumpto, TRY_LOAD);
2084
2085 if (target) {
2086 size_t size;
2087
Harald Welte43ac1912002-03-03 09:43:07 +00002088 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2089 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002090
2091 target->t = fw_calloc(1, size);
2092 target->t->u.target_size = size;
2093 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002094 if (target->init != NULL)
2095 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002096 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002097 }
2098 break;
2099
2100
2101 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002102 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002103 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2104 invert);
2105 parse_interface(argv[optind-1],
2106 fw.ipv6.iniface,
2107 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002108 break;
2109
2110 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002111 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2113 invert);
2114 parse_interface(argv[optind-1],
2115 fw.ipv6.outiface,
2116 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002117 break;
2118
2119 case 'v':
2120 if (!verbose)
2121 set_option(&options, OPT_VERBOSE,
2122 &fw.ipv6.invflags, invert);
2123 verbose++;
2124 break;
2125
2126 case 'm': {
2127 size_t size;
2128
2129 if (invert)
2130 exit_error(PARAMETER_PROBLEM,
2131 "unexpected ! flag before --match");
2132
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002133 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002134 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2135 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002136 m->m = fw_calloc(1, size);
2137 m->m->u.match_size = size;
2138 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002139 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002140 if (m->init != NULL)
2141 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002142 if (m != m->next)
2143 /* Merge options for non-cloned matches */
2144 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002145 }
2146 break;
2147
2148 case 'n':
2149 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2150 invert);
2151 break;
2152
2153 case 't':
2154 if (invert)
2155 exit_error(PARAMETER_PROBLEM,
2156 "unexpected ! flag before --table");
2157 *table = argv[optind-1];
2158 break;
2159
2160 case 'x':
2161 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2162 invert);
2163 break;
2164
2165 case 'V':
2166 if (invert)
2167 printf("Not %s ;-)\n", program_version);
2168 else
2169 printf("%s v%s\n",
2170 program_name, program_version);
2171 exit(0);
2172
2173 case '0':
2174 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2175 invert);
2176 break;
2177
Harald Welte43ac1912002-03-03 09:43:07 +00002178 case 'M':
2179 modprobe = optarg;
2180 break;
2181
2182 case 'c':
2183
2184 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2185 invert);
2186 pcnt = optarg;
2187 if (optind < argc && argv[optind][0] != '-'
2188 && argv[optind][0] != '!')
2189 bcnt = argv[optind++];
2190 else
2191 exit_error(PARAMETER_PROBLEM,
2192 "-%c requires packet and byte counter",
2193 opt2char(OPT_COUNTERS));
2194
Martin Josefssona28d4952004-05-26 16:04:48 +00002195 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002196 exit_error(PARAMETER_PROBLEM,
2197 "-%c packet counter not numeric",
2198 opt2char(OPT_COUNTERS));
2199
Martin Josefssona28d4952004-05-26 16:04:48 +00002200 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002201 exit_error(PARAMETER_PROBLEM,
2202 "-%c byte counter not numeric",
2203 opt2char(OPT_COUNTERS));
2204
2205 break;
2206
2207
Rusty Russell5eed48a2000-06-02 20:12:24 +00002208 case 1: /* non option */
2209 if (optarg[0] == '!' && optarg[1] == '\0') {
2210 if (invert)
2211 exit_error(PARAMETER_PROBLEM,
2212 "multiple consecutive ! not"
2213 " allowed");
2214 invert = TRUE;
2215 optarg[0] = '\0';
2216 continue;
2217 }
2218 printf("Bad argument `%s'\n", optarg);
2219 exit_tryhelp(2);
2220
2221 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002222 if (!target
2223 || !(target->parse(c - target->option_offset,
2224 argv, invert,
2225 &target->tflags,
2226 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002227 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002228 if (matchp->completed)
2229 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002230 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002231 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002232 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002233 &fw,
2234 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002235 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002236 break;
2237 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002238 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002239
2240 /* If you listen carefully, you can
2241 actually hear this code suck. */
2242
2243 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002244 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002245 * parameter, that has not been parsed yet,
2246 * it's not an option of an explicitly loaded
2247 * match or a target. However, we support
2248 * implicit loading of the protocol match
2249 * extension. '-p tcp' means 'l4 proto 6' and
2250 * at the same time 'load tcp protocol match on
2251 * demand if we specify --dport'.
2252 *
2253 * To make this work, we need to make sure:
2254 * - the parameter has not been parsed by
2255 * a match (m above)
2256 * - a protocol has been specified
2257 * - the protocol extension has not been
2258 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002259 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002260 * - the protocol extension can be successively
2261 * loaded
2262 */
2263 if (m == NULL
2264 && protocol
2265 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002266 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002267 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002268 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002269 && (proto_used == 0))
2270 )
2271 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002272 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002273 /* Try loading protocol */
2274 size_t size;
2275
2276 proto_used = 1;
2277
2278 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2279 + m->size;
2280
2281 m->m = fw_calloc(1, size);
2282 m->m->u.match_size = size;
2283 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002284 set_revision(m->m->u.user.name,
2285 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002286 if (m->init != NULL)
2287 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002288
2289 opts = merge_options(opts,
2290 m->extra_opts, &m->option_offset);
2291
2292 optind--;
2293 continue;
2294 }
2295
Rusty Russell5eed48a2000-06-02 20:12:24 +00002296 if (!m)
2297 exit_error(PARAMETER_PROBLEM,
2298 "Unknown arg `%s'",
2299 argv[optind-1]);
2300 }
2301 }
2302 invert = FALSE;
2303 }
2304
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002305 for (matchp = matches; matchp; matchp = matchp->next)
2306 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002307
Harald Welte43ac1912002-03-03 09:43:07 +00002308 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002309 target->final_check(target->tflags);
2310
2311 /* Fix me: must put inverse options checking here --MN */
2312
2313 if (optind < argc)
2314 exit_error(PARAMETER_PROBLEM,
2315 "unknown arguments found on commandline");
2316 if (!command)
2317 exit_error(PARAMETER_PROBLEM, "no command specified");
2318 if (invert)
2319 exit_error(PARAMETER_PROBLEM,
2320 "nothing appropriate following !");
2321
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002322 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002323 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002324 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002325 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002326 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002327 }
2328
2329 if (shostnetworkmask)
2330 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2331 &(fw.ipv6.smsk), &nsaddrs);
2332
2333 if (dhostnetworkmask)
2334 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2335 &(fw.ipv6.dmsk), &ndaddrs);
2336
2337 if ((nsaddrs > 1 || ndaddrs > 1) &&
2338 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2339 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2340 " source or destination IP addresses");
2341
Rusty Russell5eed48a2000-06-02 20:12:24 +00002342 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2343 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2344 "specify a unique address");
2345
2346 generic_opt_check(command, options);
2347
2348 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2349 exit_error(PARAMETER_PROBLEM,
2350 "chain name `%s' too long (must be under %i chars)",
2351 chain, IP6T_FUNCTION_MAXNAMELEN);
2352
Harald Welte43ac1912002-03-03 09:43:07 +00002353 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002354 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002355 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002356
Rusty Russell8beb0492004-12-22 00:37:10 +00002357 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00002358 if (!*handle && load_ip6tables_ko(modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002359 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002360
Harald Welte43ac1912002-03-03 09:43:07 +00002361 if (!*handle)
2362 exit_error(VERSION_PROBLEM,
2363 "can't initialize ip6tables table `%s': %s",
2364 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002365
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002366 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002367 || command == CMD_DELETE
2368 || command == CMD_INSERT
2369 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002370 if (strcmp(chain, "PREROUTING") == 0
2371 || strcmp(chain, "INPUT") == 0) {
2372 /* -o not valid with incoming packets. */
2373 if (options & OPT_VIANAMEOUT)
2374 exit_error(PARAMETER_PROBLEM,
2375 "Can't use -%c with %s\n",
2376 opt2char(OPT_VIANAMEOUT),
2377 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002378 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002379
Harald Welte43ac1912002-03-03 09:43:07 +00002380 if (strcmp(chain, "POSTROUTING") == 0
2381 || strcmp(chain, "OUTPUT") == 0) {
2382 /* -i not valid with outgoing packets */
2383 if (options & OPT_VIANAMEIN)
2384 exit_error(PARAMETER_PROBLEM,
2385 "Can't use -%c with %s\n",
2386 opt2char(OPT_VIANAMEIN),
2387 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002388 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002389
2390 if (target && ip6tc_is_chain(jumpto, *handle)) {
2391 printf("Warning: using chain %s, not extension\n",
2392 jumpto);
2393
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002394 if (target->t)
2395 free(target->t);
2396
Rusty Russell5eed48a2000-06-02 20:12:24 +00002397 target = NULL;
2398 }
2399
2400 /* If they didn't specify a target, or it's a chain
2401 name, use standard. */
2402 if (!target
2403 && (strlen(jumpto) == 0
2404 || ip6tc_is_chain(jumpto, *handle))) {
2405 size_t size;
2406
2407 target = find_target(IP6T_STANDARD_TARGET,
2408 LOAD_MUST_SUCCEED);
2409
2410 size = sizeof(struct ip6t_entry_target)
2411 + target->size;
2412 target->t = fw_calloc(1, size);
2413 target->t->u.target_size = size;
2414 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002415 if (target->init != NULL)
2416 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002417 }
2418
2419 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002420 /* it is no chain, and we can't load a plugin.
2421 * We cannot know if the plugin is corrupt, non
2422 * existant OR if the user just misspelled a
2423 * chain. */
2424 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002425 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002426 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002427 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002428 }
2429 }
2430
2431 switch (command) {
2432 case CMD_APPEND:
2433 ret = append_entry(chain, e,
2434 nsaddrs, saddrs, ndaddrs, daddrs,
2435 options&OPT_VERBOSE,
2436 handle);
2437 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002438 case CMD_DELETE:
2439 ret = delete_entry(chain, e,
2440 nsaddrs, saddrs, ndaddrs, daddrs,
2441 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002442 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002443 break;
2444 case CMD_DELETE_NUM:
2445 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2446 break;
2447 case CMD_REPLACE:
2448 ret = replace_entry(chain, e, rulenum - 1,
2449 saddrs, daddrs, options&OPT_VERBOSE,
2450 handle);
2451 break;
2452 case CMD_INSERT:
2453 ret = insert_entry(chain, e, rulenum - 1,
2454 nsaddrs, saddrs, ndaddrs, daddrs,
2455 options&OPT_VERBOSE,
2456 handle);
2457 break;
2458 case CMD_LIST:
2459 ret = list_entries(chain,
2460 options&OPT_VERBOSE,
2461 options&OPT_NUMERIC,
2462 options&OPT_EXPANDED,
2463 options&OPT_LINENUMBERS,
2464 handle);
2465 break;
2466 case CMD_FLUSH:
2467 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2468 break;
2469 case CMD_ZERO:
2470 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2471 break;
2472 case CMD_LIST|CMD_ZERO:
2473 ret = list_entries(chain,
2474 options&OPT_VERBOSE,
2475 options&OPT_NUMERIC,
2476 options&OPT_EXPANDED,
2477 options&OPT_LINENUMBERS,
2478 handle);
2479 if (ret)
2480 ret = zero_entries(chain,
2481 options&OPT_VERBOSE, handle);
2482 break;
2483 case CMD_NEW_CHAIN:
2484 ret = ip6tc_create_chain(chain, handle);
2485 break;
2486 case CMD_DELETE_CHAIN:
2487 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2488 break;
2489 case CMD_RENAME_CHAIN:
2490 ret = ip6tc_rename_chain(chain, newname, handle);
2491 break;
2492 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002493 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002494 break;
2495 default:
2496 /* We should never reach this... */
2497 exit_tryhelp(2);
2498 }
2499
2500 if (verbose > 1)
2501 dump_entries6(*handle);
2502
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002503 clear_rule_matches(&matches);
2504
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002505 if (e != NULL) {
2506 free(e);
2507 e = NULL;
2508 }
2509
2510 for (c = 0; c < nsaddrs; c++)
2511 free(&saddrs[c]);
2512
2513 for (c = 0; c < ndaddrs; c++)
2514 free(&daddrs[c]);
2515
Pablo Neiradfdcd642005-05-29 19:05:23 +00002516 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002517
Rusty Russell5eed48a2000-06-02 20:12:24 +00002518 return ret;
2519}