blob: a096b7952fad962e3a9fbdc3ccbdd8a1954ea352 [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>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
43#include <sys/wait.h>
44#include <sys/types.h>
45#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000046
47#ifndef TRUE
48#define TRUE 1
49#endif
50#ifndef FALSE
51#define FALSE 0
52#endif
53
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000054#ifndef PROC_SYS_MODPROBE
55#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
56#endif
57
Rusty Russell5eed48a2000-06-02 20:12:24 +000058#define FMT_NUMERIC 0x0001
59#define FMT_NOCOUNTS 0x0002
60#define FMT_KILOMEGAGIGA 0x0004
61#define FMT_OPTIONS 0x0008
62#define FMT_NOTABLE 0x0010
63#define FMT_NOTARGET 0x0020
64#define FMT_VIA 0x0040
65#define FMT_NONEWLINE 0x0080
66#define FMT_LINENUMBERS 0x0100
67
68#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
69 | FMT_NUMERIC | FMT_NOTABLE)
70#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
71
72
73#define CMD_NONE 0x0000U
74#define CMD_INSERT 0x0001U
75#define CMD_DELETE 0x0002U
76#define CMD_DELETE_NUM 0x0004U
77#define CMD_REPLACE 0x0008U
78#define CMD_APPEND 0x0010U
79#define CMD_LIST 0x0020U
80#define CMD_FLUSH 0x0040U
81#define CMD_ZERO 0x0080U
82#define CMD_NEW_CHAIN 0x0100U
83#define CMD_DELETE_CHAIN 0x0200U
84#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000085#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000086#define NUMBER_OF_CMD 13
87static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000088 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000089
90#define OPTION_OFFSET 256
91
92#define OPT_NONE 0x00000U
93#define OPT_NUMERIC 0x00001U
94#define OPT_SOURCE 0x00002U
95#define OPT_DESTINATION 0x00004U
96#define OPT_PROTOCOL 0x00008U
97#define OPT_JUMP 0x00010U
98#define OPT_VERBOSE 0x00020U
99#define OPT_EXPANDED 0x00040U
100#define OPT_VIANAMEIN 0x00080U
101#define OPT_VIANAMEOUT 0x00100U
102#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000103#define OPT_COUNTERS 0x00400U
104#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000105static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000106= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000107
108static struct option original_opts[] = {
109 { "append", 1, 0, 'A' },
110 { "delete", 1, 0, 'D' },
111 { "insert", 1, 0, 'I' },
112 { "replace", 1, 0, 'R' },
113 { "list", 2, 0, 'L' },
114 { "flush", 2, 0, 'F' },
115 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000116 { "new-chain", 1, 0, 'N' },
117 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000118 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000119 { "policy", 1, 0, 'P' },
120 { "source", 1, 0, 's' },
121 { "destination", 1, 0, 'd' },
122 { "src", 1, 0, 's' }, /* synonym */
123 { "dst", 1, 0, 'd' }, /* synonym */
124 { "protocol", 1, 0, 'p' },
125 { "in-interface", 1, 0, 'i' },
126 { "jump", 1, 0, 'j' },
127 { "table", 1, 0, 't' },
128 { "match", 1, 0, 'm' },
129 { "numeric", 0, 0, 'n' },
130 { "out-interface", 1, 0, 'o' },
131 { "verbose", 0, 0, 'v' },
132 { "exact", 0, 0, 'x' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000136 { "modprobe", 1, 0, 'M' },
137 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138 { 0 }
139};
140
Harald Weltea8658ca2003-03-05 07:46:15 +0000141/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. ip6tables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
146
Rusty Russell5eed48a2000-06-02 20:12:24 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162 /* -n -s -d -p -j -v -x -i -o --line -c */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IP6T_INV_SRCIP,
181/* -d */ IP6T_INV_DSTIP,
182/* -p */ IP6T_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IP6T_INV_VIA_IN,
187/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190};
191
192const char *program_version;
193const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000194char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000195
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +0000196/* the path to command to load kernel module */
197const char *modprobe = NULL;
198
Rusty Russell5eed48a2000-06-02 20:12:24 +0000199/* Keeping track of external matches and targets: linked lists. */
200struct ip6tables_match *ip6tables_matches = NULL;
201struct ip6tables_target *ip6tables_targets = NULL;
202
203/* Extra debugging from libiptc */
204extern void dump_entries6(const ip6tc_handle_t handle);
205
206/* A few hardcoded protocols for 'all' and in case the user has no
207 /etc/protocols */
208struct pprot {
209 char *name;
210 u_int8_t num;
211};
212
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000213/* Primitive headers... */
214/* defined in netinet/in.h */
215#if 0
216#ifndef IPPROTO_ESP
217#define IPPROTO_ESP 50
218#endif
219#ifndef IPPROTO_AH
220#define IPPROTO_AH 51
221#endif
222#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000223#ifndef IPPROTO_MH
224#define IPPROTO_MH 135
225#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000226
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227static const struct pprot chain_protos[] = {
228 { "tcp", IPPROTO_TCP },
229 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000230 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000231 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000232 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000233 { "esp", IPPROTO_ESP },
234 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000235 { "ipv6-mh", IPPROTO_MH },
236 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000237 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000238};
239
240static char *
241proto_to_name(u_int8_t proto, int nolookup)
242{
243 unsigned int i;
244
245 if (proto && !nolookup) {
246 struct protoent *pent = getprotobynumber(proto);
247 if (pent)
248 return pent->p_name;
249 }
250
251 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
252 if (chain_protos[i].num == proto)
253 return chain_protos[i].name;
254
255 return NULL;
256}
257
Phil Oester58179b12006-07-20 17:00:19 +0000258int
259service_to_port(const char *name, const char *proto)
260{
261 struct servent *service;
262
263 if ((service = getservbyname(name, proto)) != NULL)
264 return ntohs((unsigned short) service->s_port);
265
266 return -1;
267}
268
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000269u_int16_t
270parse_port(const char *port, const char *proto)
271{
272 unsigned int portnum;
273
274 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
275 (portnum = service_to_port(port, proto)) != -1)
276 return (u_int16_t)portnum;
277
278 exit_error(PARAMETER_PROBLEM,
279 "invalid port/service `%s' specified", port);
280}
281
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282static void
283in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
284{
285 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000286 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287}
288
Pablo Neiradfdcd642005-05-29 19:05:23 +0000289static void free_opts(int reset_offset)
290{
291 if (opts != original_opts) {
292 free(opts);
293 opts = original_opts;
294 if (reset_offset)
295 global_option_offset = 0;
296 }
297}
298
Rusty Russell5eed48a2000-06-02 20:12:24 +0000299void
300exit_error(enum exittype status, char *msg, ...)
301{
302 va_list args;
303
304 va_start(args, msg);
305 fprintf(stderr, "%s v%s: ", program_name, program_version);
306 vfprintf(stderr, msg, args);
307 va_end(args);
308 fprintf(stderr, "\n");
309 if (status == PARAMETER_PROBLEM)
310 exit_tryhelp(status);
311 if (status == VERSION_PROBLEM)
312 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000313 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000314 /* On error paths, make sure that we don't leak memory */
315 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000316 exit(status);
317}
318
319void
320exit_tryhelp(int status)
321{
Harald Weltea8658ca2003-03-05 07:46:15 +0000322 if (line != -1)
323 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000324 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
325 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000326 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327 exit(status);
328}
329
330void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000331exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000333 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334 struct ip6tables_target *t = NULL;
335
336 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000337"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000338" %s -[RI] chain rulenum rule-specification [options]\n"
339" %s -D chain rulenum [options]\n"
340" %s -[LFZ] [chain] [options]\n"
341" %s -[NX] chain\n"
342" %s -E old-chain-name new-chain-name\n"
343" %s -P chain target [options]\n"
344" %s -h (print this help information)\n\n",
345 program_name, program_version, program_name, program_name,
346 program_name, program_name, program_name, program_name,
347 program_name, program_name);
348
349 printf(
350"Commands:\n"
351"Either long or short options are allowed.\n"
352" --append -A chain Append to chain\n"
353" --delete -D chain Delete matching rule from chain\n"
354" --delete -D chain rulenum\n"
355" Delete rule rulenum (1 = first) from chain\n"
356" --insert -I chain [rulenum]\n"
357" Insert in chain as rulenum (default 1=first)\n"
358" --replace -R chain rulenum\n"
359" Replace rule rulenum (1 = first) in chain\n"
360" --list -L [chain] List the rules in a chain or all chains\n"
361" --flush -F [chain] Delete all rules in chain or all chains\n"
362" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000363" --new -N chain Create a new user-defined chain\n"
364" --delete-chain\n"
365" -X [chain] Delete a user-defined chain\n"
366" --policy -P chain target\n"
367" Change policy on chain to target\n"
368" --rename-chain\n"
369" -E old-chain new-chain\n"
370" Change chain name, (moving any references)\n"
371
372"Options:\n"
373" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
374" --source -s [!] address[/mask]\n"
375" source specification\n"
376" --destination -d [!] address[/mask]\n"
377" destination specification\n"
378" --in-interface -i [!] input name[+]\n"
379" network interface name ([+] for wildcard)\n"
380" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000381" target for rule (may load target extension)\n"
382" --match -m match\n"
383" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000384" --numeric -n numeric output of addresses and ports\n"
385" --out-interface -o [!] output name[+]\n"
386" network interface name ([+] for wildcard)\n"
387" --table -t table table to manipulate (default: `filter')\n"
388" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000389" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000390" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000391/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000392" --modprobe=<command> try to insert modules using this command\n"
393" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000394"[!] --version -V print package version.\n");
395
396 /* Print out any special helps. A user might like to be able to add a --help
397 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000398 specified matches & targets */
399 for (t = ip6tables_targets; t; t = t->next) {
400 if (t->used) {
401 printf("\n");
402 t->help();
403 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000404 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000405 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000406 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000407 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000408 }
409 exit(0);
410}
411
412static void
413generic_opt_check(int command, int options)
414{
415 int i, j, legal = 0;
416
417 /* Check that commands are valid with options. Complicated by the
418 * fact that if an option is legal with *any* command given, it is
419 * legal overall (ie. -z and -l).
420 */
421 for (i = 0; i < NUMBER_OF_OPT; i++) {
422 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
423
424 for (j = 0; j < NUMBER_OF_CMD; j++) {
425 if (!(command & (1<<j)))
426 continue;
427
428 if (!(options & (1<<i))) {
429 if (commands_v_options[j][i] == '+')
430 exit_error(PARAMETER_PROBLEM,
431 "You need to supply the `-%c' "
432 "option for this command\n",
433 optflags[i]);
434 } else {
435 if (commands_v_options[j][i] != 'x')
436 legal = 1;
437 else if (legal == 0)
438 legal = -1;
439 }
440 }
441 if (legal == -1)
442 exit_error(PARAMETER_PROBLEM,
443 "Illegal option `-%c' with this command\n",
444 optflags[i]);
445 }
446}
447
448static char
449opt2char(int option)
450{
451 const char *ptr;
452 for (ptr = optflags; option > 1; option >>= 1, ptr++);
453
454 return *ptr;
455}
456
457static char
458cmd2char(int option)
459{
460 const char *ptr;
461 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
462
463 return *ptr;
464}
465
466static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000467add_command(unsigned int *cmd, const int newcmd, const int othercmds,
468 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469{
470 if (invert)
471 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
472 if (*cmd & (~othercmds))
473 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
474 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
475 *cmd |= newcmd;
476}
477
478int
Harald Welteb77f1da2002-03-14 11:35:58 +0000479check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000480{
481 if (option && strcmp(option, "!") == 0) {
482 if (*invert)
483 exit_error(PARAMETER_PROBLEM,
484 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000485 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000486 if (optind) {
487 *optind = *optind+1;
488 if (argc && *optind > argc)
489 exit_error(PARAMETER_PROBLEM,
490 "no argument following `!'");
491 }
492
Rusty Russell5eed48a2000-06-02 20:12:24 +0000493 return TRUE;
494 }
495 return FALSE;
496}
497
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498static char *
499addr_to_numeric(const struct in6_addr *addrp)
500{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000501 /* 0000:0000:0000:0000:0000:000.000.000.000
502 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000503 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000504 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000505}
506
507static struct in6_addr *
508numeric_to_addr(const char *num)
509{
510 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000511 int err;
512 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000513 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000514#ifdef DEBUG
515 fprintf(stderr, "\nnumeric2addr: %d\n", err);
516#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000517 return (struct in6_addr *)NULL;
518}
519
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000520
521static struct in6_addr *
522host_to_addr(const char *name, unsigned int *naddr)
523{
524 struct addrinfo hints;
525 struct addrinfo *res;
526 static struct in6_addr *addr;
527 int err;
528
529 memset(&hints, 0, sizeof(hints));
530 hints.ai_flags=AI_CANONNAME;
531 hints.ai_family=AF_INET6;
532 hints.ai_socktype=SOCK_RAW;
533 hints.ai_protocol=41;
534 hints.ai_next=NULL;
535
536 *naddr = 0;
537 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
538#ifdef DEBUG
539 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
540#endif
541 return (struct in6_addr *) NULL;
542 } else {
543 if (res->ai_family != AF_INET6 ||
544 res->ai_addrlen != sizeof(struct sockaddr_in6))
545 return (struct in6_addr *) NULL;
546
547#ifdef DEBUG
548 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
549 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
550#endif
551 /* Get the first element of the address-chain */
552 addr = fw_calloc(1, sizeof(struct in6_addr));
553 in6addrcpy(addr, (struct in6_addr *)
554 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
555 freeaddrinfo(res);
556 *naddr = 1;
557 return addr;
558 }
559
560 return (struct in6_addr *) NULL;
561}
562
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000563static char *
564addr_to_host(const struct in6_addr *addr)
565{
566 struct sockaddr_in6 saddr;
567 int err;
568 static char hostname[NI_MAXHOST];
569
570 memset(&saddr, 0, sizeof(struct sockaddr_in6));
571 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000572 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000573
574 if ( (err=getnameinfo((struct sockaddr *)&saddr,
575 sizeof(struct sockaddr_in6),
576 hostname, sizeof(hostname)-1,
577 NULL, 0, 0)) != 0 ){
578#ifdef DEBUG
579 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
580#endif
581 return (char *) NULL;
582 } else {
583#ifdef DEBUG
584 fprintf (stderr, "\naddr2host: %s\n", hostname);
585#endif
586
587 return hostname;
588 }
589
590 return (char *) NULL;
591}
592
Rusty Russell5eed48a2000-06-02 20:12:24 +0000593static char *
594mask_to_numeric(const struct in6_addr *addrp)
595{
Harald Welte8a50ab82003-06-24 18:15:59 +0000596 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000597 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000598 if (l == -1) {
599 strcpy(buf, "/");
600 strcat(buf, addr_to_numeric(addrp));
601 return buf;
602 }
Harald Welte43ac1912002-03-03 09:43:07 +0000603 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000604 return buf;
605}
606
607static struct in6_addr *
608network_to_addr(const char *name)
609{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000610 /* abort();*/
611 /* TODO: not implemented yet, but the exception breaks the
612 * name resolvation */
613 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614}
615
616static char *
617addr_to_anyname(const struct in6_addr *addr)
618{
619 char *name;
620
621 if ((name = addr_to_host(addr)) != NULL)
622 return name;
623
624 return addr_to_numeric(addr);
625}
626
627/*
628 * All functions starting with "parse" should succeed, otherwise
629 * the program fails.
630 * Most routines return pointers to static data that may change
631 * between calls to the same or other routines with a few exceptions:
632 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
633 * return global static data.
634*/
635
636static struct in6_addr *
637parse_hostnetwork(const char *name, unsigned int *naddrs)
638{
639 struct in6_addr *addrp, *addrptmp;
640
641 if ((addrptmp = numeric_to_addr(name)) != NULL ||
642 (addrptmp = network_to_addr(name)) != NULL) {
643 addrp = fw_malloc(sizeof(struct in6_addr));
644 in6addrcpy(addrp, addrptmp);
645 *naddrs = 1;
646 return addrp;
647 }
648 if ((addrp = host_to_addr(name, naddrs)) != NULL)
649 return addrp;
650
651 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
652}
653
654static struct in6_addr *
655parse_mask(char *mask)
656{
657 static struct in6_addr maskaddr;
658 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000659 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660
661 if (mask == NULL) {
662 /* no mask at all defaults to 128 bits */
663 memset(&maskaddr, 0xff, sizeof maskaddr);
664 return &maskaddr;
665 }
666 if ((addrp = numeric_to_addr(mask)) != NULL)
667 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000668 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000669 exit_error(PARAMETER_PROBLEM,
670 "invalid mask `%s' specified", mask);
671 if (bits != 0) {
672 char *p = (char *)&maskaddr;
673 memset(p, 0xff, bits / 8);
674 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
675 p[bits / 8] = 0xff << (8 - (bits & 7));
676 return &maskaddr;
677 }
678
679 memset(&maskaddr, 0, sizeof maskaddr);
680 return &maskaddr;
681}
682
Harald Welte43ac1912002-03-03 09:43:07 +0000683void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000684parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
685 struct in6_addr *maskp, unsigned int *naddrs)
686{
687 struct in6_addr *addrp;
688 char buf[256];
689 char *p;
690 int i, j, n;
691
692 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000693 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000694 if ((p = strrchr(buf, '/')) != NULL) {
695 *p = '\0';
696 addrp = parse_mask(p + 1);
697 } else
698 addrp = parse_mask(NULL);
699 in6addrcpy(maskp, addrp);
700
701 /* if a null mask is given, the name is ignored, like in "any/0" */
702 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
703 strcpy(buf, "::");
704
705 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
706 n = *naddrs;
707 for (i = 0, j = 0; i < n; i++) {
708 int k;
709 for (k = 0; k < 4; k++)
710 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
711 j++;
712 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000713 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714 (*naddrs)--;
715 j--;
716 break;
717 }
718 }
719 }
720}
721
722struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000723find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724{
725 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000726 const char *icmp6 = "icmp6";
727 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000728
729 /* This is ugly as hell. Nonetheless, there is no way of changing
730 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000731 if ( (strcmp(match_name,"icmpv6") == 0) ||
732 (strcmp(match_name,"ipv6-icmp") == 0) ||
733 (strcmp(match_name,"icmp6") == 0) )
734 name = icmp6;
735 else
736 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000737
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000738 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
739 if (strcmp(name, ptr->name) == 0) {
740 struct ip6tables_match *clone;
741
742 /* First match of this type: */
743 if (ptr->m == NULL)
744 break;
745
746 /* Second and subsequent clones */
747 clone = fw_malloc(sizeof(struct ip6tables_match));
748 memcpy(clone, ptr, sizeof(struct ip6tables_match));
749 clone->mflags = 0;
750 /* This is a clone: */
751 clone->next = clone;
752
753 ptr = clone;
754 break;
755 }
756 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000757
Harald Welte3efb6ea2001-08-06 18:50:21 +0000758#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000759 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000760 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000762 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000763 if (dlopen(path, RTLD_NOW)) {
764 /* Found library. If it didn't register itself,
765 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000766 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000767
768 if (!ptr)
769 exit_error(PARAMETER_PROBLEM,
770 "Couldn't load match `%s'\n",
771 name);
772 } else if (tryload == LOAD_MUST_SUCCEED)
773 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000774 "Couldn't load match `%s':%s\n",
775 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000776 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000777#else
778 if (ptr && !ptr->loaded) {
779 if (tryload != DONT_LOAD)
780 ptr->loaded = 1;
781 else
782 ptr = NULL;
783 }
Marc Boucher067477b2002-03-24 15:09:31 +0000784 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
785 exit_error(PARAMETER_PROBLEM,
786 "Couldn't find match `%s'\n", name);
787 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000788#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000790 if (ptr && matches) {
791 struct ip6tables_rule_match **i;
792 struct ip6tables_rule_match *newentry;
793
794 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
795
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000796 for (i = matches; *i; i = &(*i)->next) {
797 if (strcmp(name, (*i)->match->name) == 0)
798 (*i)->completed = 1;
799 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000800 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000801 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000802 newentry->next = NULL;
803 *i = newentry;
804 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000805
Rusty Russell5eed48a2000-06-02 20:12:24 +0000806 return ptr;
807}
808
809/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
810static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000811find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000812{
Harald Welteed498492001-07-23 01:24:22 +0000813 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814
Harald Welte43ac1912002-03-03 09:43:07 +0000815 if (string_to_number(pname, 0, 255, &proto) != -1) {
816 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000817
Harald Welte43ac1912002-03-03 09:43:07 +0000818 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000819 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000820 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000821 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000822
823 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824}
825
Harald Welte43ac1912002-03-03 09:43:07 +0000826u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827parse_protocol(const char *s)
828{
Harald Welteed498492001-07-23 01:24:22 +0000829 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830
Harald Welteed498492001-07-23 01:24:22 +0000831 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000832 struct protoent *pent;
833
Harald Weltecbe1ec72006-02-11 09:50:11 +0000834 /* first deal with the special case of 'all' to prevent
835 * people from being able to redefine 'all' in nsswitch
836 * and/or provoke expensive [not working] ldap/nis/...
837 * lookups */
838 if (!strcmp(s, "all"))
839 return 0;
840
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841 if ((pent = getprotobyname(s)))
842 proto = pent->p_proto;
843 else {
844 unsigned int i;
845 for (i = 0;
846 i < sizeof(chain_protos)/sizeof(struct pprot);
847 i++) {
848 if (strcmp(s, chain_protos[i].name) == 0) {
849 proto = chain_protos[i].num;
850 break;
851 }
852 }
853 if (i == sizeof(chain_protos)/sizeof(struct pprot))
854 exit_error(PARAMETER_PROBLEM,
855 "unknown protocol `%s' specified",
856 s);
857 }
858 }
859
860 return (u_int16_t)proto;
861}
862
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000863/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000864static int is_exthdr(u_int16_t proto)
865{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000866 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000867 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000868 proto == IPPROTO_AH ||
869 proto == IPPROTO_DSTOPTS);
870}
871
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000872void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873{
874 int vialen = strlen(arg);
875 unsigned int i;
876
877 memset(mask, 0, IFNAMSIZ);
878 memset(vianame, 0, IFNAMSIZ);
879
880 if (vialen + 1 > IFNAMSIZ)
881 exit_error(PARAMETER_PROBLEM,
882 "interface name `%s' must be shorter than IFNAMSIZ"
883 " (%i)", arg, IFNAMSIZ-1);
884
885 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000886 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000887 memset(mask, 0, IFNAMSIZ);
888 else if (vianame[vialen - 1] == '+') {
889 memset(mask, 0xFF, vialen - 1);
890 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000891 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892 } else {
893 /* Include nul-terminator in match */
894 memset(mask, 0xFF, vialen + 1);
895 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000896 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000897 if (vianame[i] == ':' ||
898 vianame[i] == '!' ||
899 vianame[i] == '*') {
900 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000901 " `%s' (No aliases, :, ! or *).\n",
902 vianame);
903 break;
904 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000905 }
906 }
907}
908
909/* Can't be zero. */
910static int
911parse_rulenumber(const char *rule)
912{
Harald Welte43ac1912002-03-03 09:43:07 +0000913 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000914
Harald Welte43ac1912002-03-03 09:43:07 +0000915 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916 exit_error(PARAMETER_PROBLEM,
917 "Invalid rule number `%s'", rule);
918
919 return rulenum;
920}
921
922static const char *
923parse_target(const char *targetname)
924{
925 const char *ptr;
926
927 if (strlen(targetname) < 1)
928 exit_error(PARAMETER_PROBLEM,
929 "Invalid target name (too short)");
930
931 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
932 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000933 "Invalid target name `%s' (%u chars max)",
934 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935
936 for (ptr = targetname; *ptr; ptr++)
937 if (isspace(*ptr))
938 exit_error(PARAMETER_PROBLEM,
939 "Invalid target name `%s'", targetname);
940 return targetname;
941}
942
943int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000944string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
945 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000946{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000947 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000948 char *end;
949
950 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000951 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000952 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000953 if (*end == '\0' && end != s) {
954 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000955 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000956 *ret = number;
957 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000958 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000959 }
960 return -1;
961}
962
Martin Josefssonb105bc92004-05-26 15:54:49 +0000963int
964string_to_number_l(const char *s, unsigned long min, unsigned long max,
965 unsigned long *ret)
966{
967 int result;
968 unsigned long long number;
969
970 result = string_to_number_ll(s, min, max, &number);
971 *ret = (unsigned long)number;
972
973 return result;
974}
975
976int string_to_number(const char *s, unsigned int min, unsigned int max,
977 unsigned int *ret)
978{
979 int result;
980 unsigned long number;
981
982 result = string_to_number_l(s, min, max, &number);
983 *ret = (unsigned int)number;
984
985 return result;
986}
987
Rusty Russell5eed48a2000-06-02 20:12:24 +0000988static void
989set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
990 int invert)
991{
992 if (*options & option)
993 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
994 opt2char(option));
995 *options |= option;
996
997 if (invert) {
998 unsigned int i;
999 for (i = 0; 1 << i != option; i++);
1000
1001 if (!inverse_for_options[i])
1002 exit_error(PARAMETER_PROBLEM,
1003 "cannot have ! before -%c",
1004 opt2char(option));
1005 *invflg |= inverse_for_options[i];
1006 }
1007}
1008
1009struct ip6tables_target *
1010find_target(const char *name, enum ip6t_tryload tryload)
1011{
1012 struct ip6tables_target *ptr;
1013
1014 /* Standard target? */
1015 if (strcmp(name, "") == 0
1016 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1017 || strcmp(name, IP6TC_LABEL_DROP) == 0
1018 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1019 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1020 name = "standard";
1021
1022 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1023 if (strcmp(name, ptr->name) == 0)
1024 break;
1025 }
1026
Harald Welte3efb6ea2001-08-06 18:50:21 +00001027#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001028 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001029 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001031 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001032 if (dlopen(path, RTLD_NOW)) {
1033 /* Found library. If it didn't register itself,
1034 maybe they specified match as a target. */
1035 ptr = find_target(name, DONT_LOAD);
1036 if (!ptr)
1037 exit_error(PARAMETER_PROBLEM,
1038 "Couldn't load target `%s'\n",
1039 name);
1040 } else if (tryload == LOAD_MUST_SUCCEED)
1041 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001042 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001043 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001044 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001045#else
1046 if (ptr && !ptr->loaded) {
1047 if (tryload != DONT_LOAD)
1048 ptr->loaded = 1;
1049 else
1050 ptr = NULL;
1051 }
Marc Boucher067477b2002-03-24 15:09:31 +00001052 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1053 exit_error(PARAMETER_PROBLEM,
1054 "Couldn't find target `%s'\n", name);
1055 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001056#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001057
Harald Welte43ac1912002-03-03 09:43:07 +00001058 if (ptr)
1059 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001060
Rusty Russell5eed48a2000-06-02 20:12:24 +00001061 return ptr;
1062}
1063
1064static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001065merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001066 unsigned int *option_offset)
1067{
1068 unsigned int num_old, num_new, i;
1069 struct option *merge;
1070
1071 for (num_old = 0; oldopts[num_old].name; num_old++);
1072 for (num_new = 0; newopts[num_new].name; num_new++);
1073
1074 global_option_offset += OPTION_OFFSET;
1075 *option_offset = global_option_offset;
1076
1077 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1078 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001079 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001080 for (i = 0; i < num_new; i++) {
1081 merge[num_old + i] = newopts[i];
1082 merge[num_old + i].val += *option_offset;
1083 }
1084 memset(merge + num_old + num_new, 0, sizeof(struct option));
1085
1086 return merge;
1087}
1088
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001089static int compatible_revision(const char *name, u_int8_t revision, int opt)
1090{
1091 struct ip6t_get_revision rev;
1092 socklen_t s = sizeof(rev);
1093 int max_rev, sockfd;
1094
1095 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1096 if (sockfd < 0) {
1097 fprintf(stderr, "Could not open socket to kernel: %s\n",
1098 strerror(errno));
1099 exit(1);
1100 }
1101
1102 strcpy(rev.name, name);
1103 rev.revision = revision;
1104
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001105 load_ip6tables_ko(modprobe, 1);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001106
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001107 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1108 if (max_rev < 0) {
1109 /* Definitely don't support this? */
Patrick McHardy76258722007-06-26 15:29:45 +00001110 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001111 close(sockfd);
1112 return 0;
1113 } else if (errno == ENOPROTOOPT) {
1114 close(sockfd);
1115 /* Assume only revision 0 support (old kernel) */
1116 return (revision == 0);
1117 } else {
1118 fprintf(stderr, "getsockopt failed strangely: %s\n",
1119 strerror(errno));
1120 exit(1);
1121 }
1122 }
1123 close(sockfd);
1124 return 1;
1125}
1126
1127static int compatible_match_revision(const char *name, u_int8_t revision)
1128{
1129 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1130}
1131
Rusty Russell5eed48a2000-06-02 20:12:24 +00001132void
1133register_match6(struct ip6tables_match *me)
1134{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001135 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001136
Rusty Russell5eed48a2000-06-02 20:12:24 +00001137 if (strcmp(me->version, program_version) != 0) {
1138 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1139 program_name, me->name, me->version, program_version);
1140 exit(1);
1141 }
1142
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001143 /* Revision field stole a char from name. */
1144 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1145 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001146 program_name, me->name);
1147 exit(1);
1148 }
1149
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001150 old = find_match(me->name, DURING_LOAD, NULL);
1151 if (old) {
1152 if (old->revision == me->revision) {
1153 fprintf(stderr,
1154 "%s: match `%s' already registered.\n",
1155 program_name, me->name);
1156 exit(1);
1157 }
1158
1159 /* Now we have two (or more) options, check compatibility. */
1160 if (compatible_match_revision(old->name, old->revision)
1161 && old->revision > me->revision)
1162 return;
1163
1164 /* Replace if compatible. */
1165 if (!compatible_match_revision(me->name, me->revision))
1166 return;
1167
1168 /* Delete old one. */
1169 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1170 *i = old->next;
1171 }
1172
Harald Welte43ac1912002-03-03 09:43:07 +00001173 if (me->size != IP6T_ALIGN(me->size)) {
1174 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001175 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001176 exit(1);
1177 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001178
Harald Welte43ac1912002-03-03 09:43:07 +00001179 /* Append to list. */
1180 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1181 me->next = NULL;
1182 *i = me;
1183
Rusty Russell5eed48a2000-06-02 20:12:24 +00001184 me->m = NULL;
1185 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001186}
1187
1188void
1189register_target6(struct ip6tables_target *me)
1190{
1191 if (strcmp(me->version, program_version) != 0) {
1192 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1193 program_name, me->name, me->version, program_version);
1194 exit(1);
1195 }
1196
Jones Desougif5b86e62005-12-22 03:33:50 +00001197 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001198 fprintf(stderr, "%s: target `%s' already registered.\n",
1199 program_name, me->name);
1200 exit(1);
1201 }
1202
Harald Welte43ac1912002-03-03 09:43:07 +00001203 if (me->size != IP6T_ALIGN(me->size)) {
1204 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001205 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001206 exit(1);
1207 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001208
Rusty Russell5eed48a2000-06-02 20:12:24 +00001209 /* Prepend to list. */
1210 me->next = ip6tables_targets;
1211 ip6tables_targets = me;
1212 me->t = NULL;
1213 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001214}
1215
1216static void
1217print_num(u_int64_t number, unsigned int format)
1218{
Harald Welte43ac1912002-03-03 09:43:07 +00001219 if (format & FMT_KILOMEGAGIGA) {
1220 if (number > 99999) {
1221 number = (number + 500) / 1000;
1222 if (number > 9999) {
1223 number = (number + 500) / 1000;
1224 if (number > 9999) {
1225 number = (number + 500) / 1000;
1226 if (number > 9999) {
1227 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001228 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001229 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001231 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001232 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001233 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001234 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001235 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001236 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001237 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001238 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001239}
1240
Harald Welte43ac1912002-03-03 09:43:07 +00001241
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242static void
1243print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1244{
1245 struct ip6t_counters counters;
1246 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1247 printf("Chain %s", chain);
1248 if (pol) {
1249 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001250 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001251 fputc(' ', stdout);
1252 print_num(counters.pcnt, (format|FMT_NOTABLE));
1253 fputs("packets, ", stdout);
1254 print_num(counters.bcnt, (format|FMT_NOTABLE));
1255 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001256 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001257 printf(")\n");
1258 } else {
1259 unsigned int refs;
1260 if (!ip6tc_get_references(&refs, chain, handle))
1261 printf(" (ERROR obtaining refs)\n");
1262 else
1263 printf(" (%u references)\n", refs);
1264 }
1265
1266 if (format & FMT_LINENUMBERS)
1267 printf(FMT("%-4s ", "%s "), "num");
1268 if (!(format & FMT_NOCOUNTS)) {
1269 if (format & FMT_KILOMEGAGIGA) {
1270 printf(FMT("%5s ","%s "), "pkts");
1271 printf(FMT("%5s ","%s "), "bytes");
1272 } else {
1273 printf(FMT("%8s ","%s "), "pkts");
1274 printf(FMT("%10s ","%s "), "bytes");
1275 }
1276 }
1277 if (!(format & FMT_NOTARGET))
1278 printf(FMT("%-9s ","%s "), "target");
1279 fputs(" prot ", stdout);
1280 if (format & FMT_OPTIONS)
1281 fputs("opt", stdout);
1282 if (format & FMT_VIA) {
1283 printf(FMT(" %-6s ","%s "), "in");
1284 printf(FMT("%-6s ","%s "), "out");
1285 }
1286 printf(FMT(" %-19s ","%s "), "source");
1287 printf(FMT(" %-19s "," %s "), "destination");
1288 printf("\n");
1289}
1290
Harald Welte43ac1912002-03-03 09:43:07 +00001291
Rusty Russell5eed48a2000-06-02 20:12:24 +00001292static int
1293print_match(const struct ip6t_entry_match *m,
1294 const struct ip6t_ip6 *ip,
1295 int numeric)
1296{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001297 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001298
1299 if (match) {
1300 if (match->print)
1301 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001302 else
1303 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001304 } else {
1305 if (m->u.user.name[0])
1306 printf("UNKNOWN match `%s' ", m->u.user.name);
1307 }
1308 /* Don't stop iterating. */
1309 return 0;
1310}
1311
1312/* e is called `fw' here for hysterical raisins */
1313static void
1314print_firewall(const struct ip6t_entry *fw,
1315 const char *targname,
1316 unsigned int num,
1317 unsigned int format,
1318 const ip6tc_handle_t handle)
1319{
1320 struct ip6tables_target *target = NULL;
1321 const struct ip6t_entry_target *t;
1322 u_int8_t flags;
1323 char buf[BUFSIZ];
1324
Rusty Russell5eed48a2000-06-02 20:12:24 +00001325 if (!ip6tc_is_chain(targname, handle))
1326 target = find_target(targname, TRY_LOAD);
1327 else
1328 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1329
1330 t = ip6t_get_target((struct ip6t_entry *)fw);
1331 flags = fw->ipv6.flags;
1332
1333 if (format & FMT_LINENUMBERS)
1334 printf(FMT("%-4u ", "%u "), num+1);
1335
1336 if (!(format & FMT_NOCOUNTS)) {
1337 print_num(fw->counters.pcnt, format);
1338 print_num(fw->counters.bcnt, format);
1339 }
1340
1341 if (!(format & FMT_NOTARGET))
1342 printf(FMT("%-9s ", "%s "), targname);
1343
1344 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1345 {
Harald Welte43ac1912002-03-03 09:43:07 +00001346 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001347 if (pname)
1348 printf(FMT("%-5s", "%s "), pname);
1349 else
1350 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1351 }
1352
1353 if (format & FMT_OPTIONS) {
1354 if (format & FMT_NOTABLE)
1355 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001356 fputc(' ', stdout); /* Invert flag of FRAG */
1357 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001358 fputc(' ', stdout);
1359 }
1360
1361 if (format & FMT_VIA) {
1362 char iface[IFNAMSIZ+2];
1363
1364 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1365 iface[0] = '!';
1366 iface[1] = '\0';
1367 }
1368 else iface[0] = '\0';
1369
1370 if (fw->ipv6.iniface[0] != '\0') {
1371 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001372 }
1373 else if (format & FMT_NUMERIC) strcat(iface, "*");
1374 else strcat(iface, "any");
1375 printf(FMT(" %-6s ","in %s "), iface);
1376
1377 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1378 iface[0] = '!';
1379 iface[1] = '\0';
1380 }
1381 else iface[0] = '\0';
1382
1383 if (fw->ipv6.outiface[0] != '\0') {
1384 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001385 }
1386 else if (format & FMT_NUMERIC) strcat(iface, "*");
1387 else strcat(iface, "any");
1388 printf(FMT("%-6s ","out %s "), iface);
1389 }
1390
1391 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1392 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1393 && !(format & FMT_NUMERIC))
1394 printf(FMT("%-19s ","%s "), "anywhere");
1395 else {
1396 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001397 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001398 else
Harald Welte43ac1912002-03-03 09:43:07 +00001399 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001400 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1401 printf(FMT("%-19s ","%s "), buf);
1402 }
1403
1404 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1405 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1406 && !(format & FMT_NUMERIC))
1407 printf(FMT("%-19s","-> %s"), "anywhere");
1408 else {
1409 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001410 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001411 else
Harald Welte43ac1912002-03-03 09:43:07 +00001412 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1414 printf(FMT("%-19s","-> %s"), buf);
1415 }
1416
1417 if (format & FMT_NOTABLE)
1418 fputs(" ", stdout);
1419
1420 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1421
1422 if (target) {
1423 if (target->print)
1424 /* Print the target information. */
1425 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1426 } else if (t->u.target_size != sizeof(*t))
1427 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001428 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429
1430 if (!(format & FMT_NONEWLINE))
1431 fputc('\n', stdout);
1432}
1433
1434static void
1435print_firewall_line(const struct ip6t_entry *fw,
1436 const ip6tc_handle_t h)
1437{
1438 struct ip6t_entry_target *t;
1439
1440 t = ip6t_get_target((struct ip6t_entry *)fw);
1441 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1442}
1443
1444static int
1445append_entry(const ip6t_chainlabel chain,
1446 struct ip6t_entry *fw,
1447 unsigned int nsaddrs,
1448 const struct in6_addr saddrs[],
1449 unsigned int ndaddrs,
1450 const struct in6_addr daddrs[],
1451 int verbose,
1452 ip6tc_handle_t *handle)
1453{
1454 unsigned int i, j;
1455 int ret = 1;
1456
1457 for (i = 0; i < nsaddrs; i++) {
1458 fw->ipv6.src = saddrs[i];
1459 for (j = 0; j < ndaddrs; j++) {
1460 fw->ipv6.dst = daddrs[j];
1461 if (verbose)
1462 print_firewall_line(fw, *handle);
1463 ret &= ip6tc_append_entry(chain, fw, handle);
1464 }
1465 }
1466
1467 return ret;
1468}
1469
1470static int
1471replace_entry(const ip6t_chainlabel chain,
1472 struct ip6t_entry *fw,
1473 unsigned int rulenum,
1474 const struct in6_addr *saddr,
1475 const struct in6_addr *daddr,
1476 int verbose,
1477 ip6tc_handle_t *handle)
1478{
1479 fw->ipv6.src = *saddr;
1480 fw->ipv6.dst = *daddr;
1481
1482 if (verbose)
1483 print_firewall_line(fw, *handle);
1484 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1485}
1486
1487static int
1488insert_entry(const ip6t_chainlabel chain,
1489 struct ip6t_entry *fw,
1490 unsigned int rulenum,
1491 unsigned int nsaddrs,
1492 const struct in6_addr saddrs[],
1493 unsigned int ndaddrs,
1494 const struct in6_addr daddrs[],
1495 int verbose,
1496 ip6tc_handle_t *handle)
1497{
1498 unsigned int i, j;
1499 int ret = 1;
1500
1501 for (i = 0; i < nsaddrs; i++) {
1502 fw->ipv6.src = saddrs[i];
1503 for (j = 0; j < ndaddrs; j++) {
1504 fw->ipv6.dst = daddrs[j];
1505 if (verbose)
1506 print_firewall_line(fw, *handle);
1507 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1508 }
1509 }
1510
1511 return ret;
1512}
1513
1514static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001515make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516{
1517 /* Establish mask for comparison */
1518 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001519 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001520 unsigned char *mask, *mptr;
1521
1522 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001523 for (matchp = matches; matchp; matchp = matchp->next)
1524 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001525
1526 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001527 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001528 + ip6tables_targets->size);
1529
1530 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1531 mptr = mask + sizeof(struct ip6t_entry);
1532
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001533 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001535 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001536 + matchp->match->userspacesize);
1537 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 }
1539
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001540 memset(mptr, 0xFF,
1541 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1542 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001543
1544 return mask;
1545}
1546
1547static int
1548delete_entry(const ip6t_chainlabel chain,
1549 struct ip6t_entry *fw,
1550 unsigned int nsaddrs,
1551 const struct in6_addr saddrs[],
1552 unsigned int ndaddrs,
1553 const struct in6_addr daddrs[],
1554 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001555 ip6tc_handle_t *handle,
1556 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001557{
1558 unsigned int i, j;
1559 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001560 unsigned char *mask;
1561
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001562 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001564 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001565 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001566 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567 if (verbose)
1568 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001569 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570 }
1571 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001572 free(mask);
1573
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 return ret;
1575}
1576
András Kis-Szabó764316a2001-02-26 17:31:20 +00001577int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1579 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1580{
1581 int ret = 1;
1582 const char *chain;
1583 char *chains;
1584 unsigned int i, chaincount = 0;
1585
1586 chain = ip6tc_first_chain(handle);
1587 while (chain) {
1588 chaincount++;
1589 chain = ip6tc_next_chain(handle);
1590 }
1591
1592 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1593 i = 0;
1594 chain = ip6tc_first_chain(handle);
1595 while (chain) {
1596 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1597 i++;
1598 chain = ip6tc_next_chain(handle);
1599 }
1600
1601 for (i = 0; i < chaincount; i++) {
1602 if (!builtinstoo
1603 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001604 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605 continue;
1606 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1607 }
1608
1609 free(chains);
1610 return ret;
1611}
1612
András Kis-Szabó764316a2001-02-26 17:31:20 +00001613int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614flush_entries(const ip6t_chainlabel chain, int verbose,
1615 ip6tc_handle_t *handle)
1616{
1617 if (!chain)
1618 return for_each_chain(flush_entries, verbose, 1, handle);
1619
1620 if (verbose)
1621 fprintf(stdout, "Flushing chain `%s'\n", chain);
1622 return ip6tc_flush_entries(chain, handle);
1623}
1624
1625static int
1626zero_entries(const ip6t_chainlabel chain, int verbose,
1627 ip6tc_handle_t *handle)
1628{
1629 if (!chain)
1630 return for_each_chain(zero_entries, verbose, 1, handle);
1631
1632 if (verbose)
1633 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1634 return ip6tc_zero_entries(chain, handle);
1635}
1636
András Kis-Szabó764316a2001-02-26 17:31:20 +00001637int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001638delete_chain(const ip6t_chainlabel chain, int verbose,
1639 ip6tc_handle_t *handle)
1640{
1641 if (!chain)
1642 return for_each_chain(delete_chain, verbose, 0, handle);
1643
1644 if (verbose)
1645 fprintf(stdout, "Deleting chain `%s'\n", chain);
1646 return ip6tc_delete_chain(chain, handle);
1647}
1648
1649static int
1650list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1651 int expanded, int linenumbers, ip6tc_handle_t *handle)
1652{
1653 int found = 0;
1654 unsigned int format;
1655 const char *this;
1656
1657 format = FMT_OPTIONS;
1658 if (!verbose)
1659 format |= FMT_NOCOUNTS;
1660 else
1661 format |= FMT_VIA;
1662
1663 if (numeric)
1664 format |= FMT_NUMERIC;
1665
1666 if (!expanded)
1667 format |= FMT_KILOMEGAGIGA;
1668
1669 if (linenumbers)
1670 format |= FMT_LINENUMBERS;
1671
1672 for (this = ip6tc_first_chain(handle);
1673 this;
1674 this = ip6tc_next_chain(handle)) {
1675 const struct ip6t_entry *i;
1676 unsigned int num;
1677
1678 if (chain && strcmp(chain, this) != 0)
1679 continue;
1680
1681 if (found) printf("\n");
1682
1683 print_header(format, this, handle);
1684 i = ip6tc_first_rule(this, handle);
1685
1686 num = 0;
1687 while (i) {
1688 print_firewall(i,
1689 ip6tc_get_target(i, handle),
1690 num++,
1691 format,
1692 *handle);
1693 i = ip6tc_next_rule(i, handle);
1694 }
1695 found = 1;
1696 }
1697
1698 errno = ENOENT;
1699 return found;
1700}
1701
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001702static char *get_modprobe(void)
1703{
Harald Welte43ac1912002-03-03 09:43:07 +00001704 int procfile;
1705 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001706
Harald Welte10f7f142004-10-22 08:14:07 +00001707#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001708 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1709 if (procfile < 0)
1710 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001711
Harald Welte10f7f142004-10-22 08:14:07 +00001712 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001713 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001714 memset(ret, 0, PROCFILE_BUFSIZ);
1715 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001716 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001717 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001718 }
1719 if (ret[strlen(ret)-1]=='\n')
1720 ret[strlen(ret)-1]=0;
1721 close(procfile);
1722 return ret;
1723 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001724 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001725 free(ret);
1726 close(procfile);
1727 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001728}
1729
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001730int ip6tables_insmod(const char *modname, const char *modprobe, int quiet)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001731{
Harald Welte43ac1912002-03-03 09:43:07 +00001732 char *buf = NULL;
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001733 char *argv[4];
Rusty Russell8beb0492004-12-22 00:37:10 +00001734 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001735
Harald Welte43ac1912002-03-03 09:43:07 +00001736 /* If they don't explicitly set it, read out of kernel */
1737 if (!modprobe) {
1738 buf = get_modprobe();
1739 if (!buf)
1740 return -1;
1741 modprobe = buf;
1742 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001743
Harald Welte43ac1912002-03-03 09:43:07 +00001744 switch (fork()) {
1745 case 0:
1746 argv[0] = (char *)modprobe;
1747 argv[1] = (char *)modname;
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001748 if (quiet) {
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001749 argv[2] = "-q";
1750 argv[3] = NULL;
1751 } else {
1752 argv[2] = NULL;
1753 argv[3] = NULL;
1754 }
Harald Welte43ac1912002-03-03 09:43:07 +00001755 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001756
Harald Welte43ac1912002-03-03 09:43:07 +00001757 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001758 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001759 case -1:
1760 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001761
Harald Welte43ac1912002-03-03 09:43:07 +00001762 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001763 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001764 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001765
Harald Welte43ac1912002-03-03 09:43:07 +00001766 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001767 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1768 return 0;
1769 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001770}
1771
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001772int load_ip6tables_ko(const char *modprobe, int quiet)
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001773{
1774 static int loaded = 0;
1775 static int ret = -1;
1776
1777 if (!loaded) {
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001778 ret = ip6tables_insmod("ip6_tables", modprobe, quiet);
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001779 loaded = (ret == 0);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001780 }
1781
1782 return ret;
1783}
1784
Rusty Russell5eed48a2000-06-02 20:12:24 +00001785static struct ip6t_entry *
1786generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001787 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001788 struct ip6t_entry_target *target)
1789{
1790 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001791 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001792 struct ip6t_entry *e;
1793
1794 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001795 for (matchp = matches; matchp; matchp = matchp->next)
1796 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001797
1798 e = fw_malloc(size + target->u.target_size);
1799 *e = *fw;
1800 e->target_offset = size;
1801 e->next_offset = size + target->u.target_size;
1802
1803 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001804 for (matchp = matches; matchp; matchp = matchp->next) {
1805 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1806 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001807 }
1808 memcpy(e->elems + size, target, target->u.target_size);
1809
1810 return e;
1811}
1812
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001813void clear_rule_matches(struct ip6tables_rule_match **matches)
1814{
1815 struct ip6tables_rule_match *matchp, *tmp;
1816
1817 for (matchp = *matches; matchp;) {
1818 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001819 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001820 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001821 matchp->match->m = NULL;
1822 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001823 if (matchp->match == matchp->match->next) {
1824 free(matchp->match);
1825 matchp->match = NULL;
1826 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001827 free(matchp);
1828 matchp = tmp;
1829 }
1830
1831 *matches = NULL;
1832}
1833
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001834static void set_revision(char *name, u_int8_t revision)
1835{
1836 /* Old kernel sources don't have ".revision" field,
1837 but we stole a byte from name. */
1838 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1839 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1840}
1841
Rusty Russell5eed48a2000-06-02 20:12:24 +00001842int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1843{
1844 struct ip6t_entry fw, *e = NULL;
1845 int invert = 0;
1846 unsigned int nsaddrs = 0, ndaddrs = 0;
1847 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1848
1849 int c, verbose = 0;
1850 const char *chain = NULL;
1851 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1852 const char *policy = NULL, *newname = NULL;
1853 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001854 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001855 int ret = 1;
1856 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001857 struct ip6tables_rule_match *matches = NULL;
1858 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001859 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001860 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001861 const char *jumpto = "";
1862 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001863 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001864
1865 memset(&fw, 0, sizeof(fw));
1866
Harald Welte43ac1912002-03-03 09:43:07 +00001867 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001868 * a second time */
1869 optind = 0;
1870
Harald Welte43ac1912002-03-03 09:43:07 +00001871 /* clear mflags in case do_command gets called a second time
1872 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001873 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001874 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001875
Harald Welte43ac1912002-03-03 09:43:07 +00001876 for (t = ip6tables_targets; t; t = t->next) {
1877 t->tflags = 0;
1878 t->used = 0;
1879 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001880
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881 /* Suppress error messages: we may add new options if we
1882 demand-load a protocol. */
1883 opterr = 0;
1884
1885 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001886 "-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 +00001887 opts, NULL)) != -1) {
1888 switch (c) {
1889 /*
1890 * Command selection
1891 */
1892 case 'A':
1893 add_command(&command, CMD_APPEND, CMD_NONE,
1894 invert);
1895 chain = optarg;
1896 break;
1897
1898 case 'D':
1899 add_command(&command, CMD_DELETE, CMD_NONE,
1900 invert);
1901 chain = optarg;
1902 if (optind < argc && argv[optind][0] != '-'
1903 && argv[optind][0] != '!') {
1904 rulenum = parse_rulenumber(argv[optind++]);
1905 command = CMD_DELETE_NUM;
1906 }
1907 break;
1908
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 case 'R':
1910 add_command(&command, CMD_REPLACE, CMD_NONE,
1911 invert);
1912 chain = optarg;
1913 if (optind < argc && argv[optind][0] != '-'
1914 && argv[optind][0] != '!')
1915 rulenum = parse_rulenumber(argv[optind++]);
1916 else
1917 exit_error(PARAMETER_PROBLEM,
1918 "-%c requires a rule number",
1919 cmd2char(CMD_REPLACE));
1920 break;
1921
1922 case 'I':
1923 add_command(&command, CMD_INSERT, CMD_NONE,
1924 invert);
1925 chain = optarg;
1926 if (optind < argc && argv[optind][0] != '-'
1927 && argv[optind][0] != '!')
1928 rulenum = parse_rulenumber(argv[optind++]);
1929 else rulenum = 1;
1930 break;
1931
1932 case 'L':
1933 add_command(&command, CMD_LIST, CMD_ZERO,
1934 invert);
1935 if (optarg) chain = optarg;
1936 else if (optind < argc && argv[optind][0] != '-'
1937 && argv[optind][0] != '!')
1938 chain = argv[optind++];
1939 break;
1940
1941 case 'F':
1942 add_command(&command, CMD_FLUSH, CMD_NONE,
1943 invert);
1944 if (optarg) chain = optarg;
1945 else if (optind < argc && argv[optind][0] != '-'
1946 && argv[optind][0] != '!')
1947 chain = argv[optind++];
1948 break;
1949
1950 case 'Z':
1951 add_command(&command, CMD_ZERO, CMD_LIST,
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 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001960 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001961 exit_error(PARAMETER_PROBLEM,
1962 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001963 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001964 if (find_target(optarg, TRY_LOAD))
1965 exit_error(PARAMETER_PROBLEM,
1966 "chain name may not clash "
1967 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1969 invert);
1970 chain = optarg;
1971 break;
1972
1973 case 'X':
1974 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1975 invert);
1976 if (optarg) chain = optarg;
1977 else if (optind < argc && argv[optind][0] != '-'
1978 && argv[optind][0] != '!')
1979 chain = argv[optind++];
1980 break;
1981
1982 case 'E':
1983 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1984 invert);
1985 chain = optarg;
1986 if (optind < argc && argv[optind][0] != '-'
1987 && argv[optind][0] != '!')
1988 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001989 else
1990 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001991 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001992 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001993 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001994 break;
1995
1996 case 'P':
1997 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1998 invert);
1999 chain = optarg;
2000 if (optind < argc && argv[optind][0] != '-'
2001 && argv[optind][0] != '!')
2002 policy = argv[optind++];
2003 else
2004 exit_error(PARAMETER_PROBLEM,
2005 "-%c requires a chain and a policy",
2006 cmd2char(CMD_SET_POLICY));
2007 break;
2008
2009 case 'h':
2010 if (!optarg)
2011 optarg = argv[optind];
2012
Jonas Berlin1b91e592005-04-01 06:58:38 +00002013 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002014 if (!matches && protocol)
2015 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002016
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002017 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002018
2019 /*
2020 * Option selection
2021 */
2022 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002023 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002024 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2025 invert);
2026
2027 /* Canonicalize into lower case */
2028 for (protocol = argv[optind-1]; *protocol; protocol++)
2029 *protocol = tolower(*protocol);
2030
2031 protocol = argv[optind-1];
2032 fw.ipv6.proto = parse_protocol(protocol);
2033 fw.ipv6.flags |= IP6T_F_PROTO;
2034
2035 if (fw.ipv6.proto == 0
2036 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2037 exit_error(PARAMETER_PROBLEM,
2038 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002039
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00002040 if (is_exthdr(fw.ipv6.proto)
2041 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002042 printf("Warning: never matched protocol: %s. "
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00002043 "use extension match instead.\n",
2044 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002045 break;
2046
2047 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002048 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2050 invert);
2051 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002052 break;
2053
2054 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002055 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002056 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2057 invert);
2058 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002059 break;
2060
2061 case 'j':
2062 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2063 invert);
2064 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00002065 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002066 target = find_target(jumpto, TRY_LOAD);
2067
2068 if (target) {
2069 size_t size;
2070
Harald Welte43ac1912002-03-03 09:43:07 +00002071 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2072 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073
2074 target->t = fw_calloc(1, size);
2075 target->t->u.target_size = size;
2076 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002077 if (target->init != NULL)
2078 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002079 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002080 }
2081 break;
2082
2083
2084 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002085 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002086 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2087 invert);
2088 parse_interface(argv[optind-1],
2089 fw.ipv6.iniface,
2090 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002091 break;
2092
2093 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002094 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002095 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2096 invert);
2097 parse_interface(argv[optind-1],
2098 fw.ipv6.outiface,
2099 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002100 break;
2101
2102 case 'v':
2103 if (!verbose)
2104 set_option(&options, OPT_VERBOSE,
2105 &fw.ipv6.invflags, invert);
2106 verbose++;
2107 break;
2108
2109 case 'm': {
2110 size_t size;
2111
2112 if (invert)
2113 exit_error(PARAMETER_PROBLEM,
2114 "unexpected ! flag before --match");
2115
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002116 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002117 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2118 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002119 m->m = fw_calloc(1, size);
2120 m->m->u.match_size = size;
2121 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002122 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002123 if (m->init != NULL)
2124 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002125 if (m != m->next)
2126 /* Merge options for non-cloned matches */
2127 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002128 }
2129 break;
2130
2131 case 'n':
2132 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2133 invert);
2134 break;
2135
2136 case 't':
2137 if (invert)
2138 exit_error(PARAMETER_PROBLEM,
2139 "unexpected ! flag before --table");
2140 *table = argv[optind-1];
2141 break;
2142
2143 case 'x':
2144 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2145 invert);
2146 break;
2147
2148 case 'V':
2149 if (invert)
2150 printf("Not %s ;-)\n", program_version);
2151 else
2152 printf("%s v%s\n",
2153 program_name, program_version);
2154 exit(0);
2155
2156 case '0':
2157 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2158 invert);
2159 break;
2160
Harald Welte43ac1912002-03-03 09:43:07 +00002161 case 'M':
2162 modprobe = optarg;
2163 break;
2164
2165 case 'c':
2166
2167 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2168 invert);
2169 pcnt = optarg;
2170 if (optind < argc && argv[optind][0] != '-'
2171 && argv[optind][0] != '!')
2172 bcnt = argv[optind++];
2173 else
2174 exit_error(PARAMETER_PROBLEM,
2175 "-%c requires packet and byte counter",
2176 opt2char(OPT_COUNTERS));
2177
Martin Josefssona28d4952004-05-26 16:04:48 +00002178 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002179 exit_error(PARAMETER_PROBLEM,
2180 "-%c packet counter not numeric",
2181 opt2char(OPT_COUNTERS));
2182
Martin Josefssona28d4952004-05-26 16:04:48 +00002183 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002184 exit_error(PARAMETER_PROBLEM,
2185 "-%c byte counter not numeric",
2186 opt2char(OPT_COUNTERS));
2187
2188 break;
2189
2190
Rusty Russell5eed48a2000-06-02 20:12:24 +00002191 case 1: /* non option */
2192 if (optarg[0] == '!' && optarg[1] == '\0') {
2193 if (invert)
2194 exit_error(PARAMETER_PROBLEM,
2195 "multiple consecutive ! not"
2196 " allowed");
2197 invert = TRUE;
2198 optarg[0] = '\0';
2199 continue;
2200 }
2201 printf("Bad argument `%s'\n", optarg);
2202 exit_tryhelp(2);
2203
2204 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002205 if (!target
2206 || !(target->parse(c - target->option_offset,
2207 argv, invert,
2208 &target->tflags,
2209 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002210 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002211 if (matchp->completed)
2212 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002213 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002214 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002215 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002216 &fw,
2217 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002218 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002219 break;
2220 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002221 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002222
2223 /* If you listen carefully, you can
2224 actually hear this code suck. */
2225
2226 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002227 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002228 * parameter, that has not been parsed yet,
2229 * it's not an option of an explicitly loaded
2230 * match or a target. However, we support
2231 * implicit loading of the protocol match
2232 * extension. '-p tcp' means 'l4 proto 6' and
2233 * at the same time 'load tcp protocol match on
2234 * demand if we specify --dport'.
2235 *
2236 * To make this work, we need to make sure:
2237 * - the parameter has not been parsed by
2238 * a match (m above)
2239 * - a protocol has been specified
2240 * - the protocol extension has not been
2241 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002242 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002243 * - the protocol extension can be successively
2244 * loaded
2245 */
2246 if (m == NULL
2247 && protocol
2248 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002249 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002250 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002251 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002252 && (proto_used == 0))
2253 )
2254 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002255 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002256 /* Try loading protocol */
2257 size_t size;
2258
2259 proto_used = 1;
2260
2261 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2262 + m->size;
2263
2264 m->m = fw_calloc(1, size);
2265 m->m->u.match_size = size;
2266 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002267 set_revision(m->m->u.user.name,
2268 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002269 if (m->init != NULL)
2270 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002271
2272 opts = merge_options(opts,
2273 m->extra_opts, &m->option_offset);
2274
2275 optind--;
2276 continue;
2277 }
2278
Rusty Russell5eed48a2000-06-02 20:12:24 +00002279 if (!m)
2280 exit_error(PARAMETER_PROBLEM,
2281 "Unknown arg `%s'",
2282 argv[optind-1]);
2283 }
2284 }
2285 invert = FALSE;
2286 }
2287
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002288 for (matchp = matches; matchp; matchp = matchp->next)
2289 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002290
Harald Welte43ac1912002-03-03 09:43:07 +00002291 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002292 target->final_check(target->tflags);
2293
2294 /* Fix me: must put inverse options checking here --MN */
2295
2296 if (optind < argc)
2297 exit_error(PARAMETER_PROBLEM,
2298 "unknown arguments found on commandline");
2299 if (!command)
2300 exit_error(PARAMETER_PROBLEM, "no command specified");
2301 if (invert)
2302 exit_error(PARAMETER_PROBLEM,
2303 "nothing appropriate following !");
2304
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002305 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002306 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002307 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002308 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002309 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002310 }
2311
2312 if (shostnetworkmask)
2313 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2314 &(fw.ipv6.smsk), &nsaddrs);
2315
2316 if (dhostnetworkmask)
2317 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2318 &(fw.ipv6.dmsk), &ndaddrs);
2319
2320 if ((nsaddrs > 1 || ndaddrs > 1) &&
2321 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2322 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2323 " source or destination IP addresses");
2324
Rusty Russell5eed48a2000-06-02 20:12:24 +00002325 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2326 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2327 "specify a unique address");
2328
2329 generic_opt_check(command, options);
2330
2331 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2332 exit_error(PARAMETER_PROBLEM,
2333 "chain name `%s' too long (must be under %i chars)",
2334 chain, IP6T_FUNCTION_MAXNAMELEN);
2335
Harald Welte43ac1912002-03-03 09:43:07 +00002336 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002337 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002338 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002339
Rusty Russell8beb0492004-12-22 00:37:10 +00002340 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00002341 if (!*handle && load_ip6tables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002342 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002343
Harald Welte43ac1912002-03-03 09:43:07 +00002344 if (!*handle)
2345 exit_error(VERSION_PROBLEM,
2346 "can't initialize ip6tables table `%s': %s",
2347 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002348
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002349 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002350 || command == CMD_DELETE
2351 || command == CMD_INSERT
2352 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002353 if (strcmp(chain, "PREROUTING") == 0
2354 || strcmp(chain, "INPUT") == 0) {
2355 /* -o not valid with incoming packets. */
2356 if (options & OPT_VIANAMEOUT)
2357 exit_error(PARAMETER_PROBLEM,
2358 "Can't use -%c with %s\n",
2359 opt2char(OPT_VIANAMEOUT),
2360 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002361 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002362
Harald Welte43ac1912002-03-03 09:43:07 +00002363 if (strcmp(chain, "POSTROUTING") == 0
2364 || strcmp(chain, "OUTPUT") == 0) {
2365 /* -i not valid with outgoing packets */
2366 if (options & OPT_VIANAMEIN)
2367 exit_error(PARAMETER_PROBLEM,
2368 "Can't use -%c with %s\n",
2369 opt2char(OPT_VIANAMEIN),
2370 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002371 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002372
2373 if (target && ip6tc_is_chain(jumpto, *handle)) {
2374 printf("Warning: using chain %s, not extension\n",
2375 jumpto);
2376
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002377 if (target->t)
2378 free(target->t);
2379
Rusty Russell5eed48a2000-06-02 20:12:24 +00002380 target = NULL;
2381 }
2382
2383 /* If they didn't specify a target, or it's a chain
2384 name, use standard. */
2385 if (!target
2386 && (strlen(jumpto) == 0
2387 || ip6tc_is_chain(jumpto, *handle))) {
2388 size_t size;
2389
2390 target = find_target(IP6T_STANDARD_TARGET,
2391 LOAD_MUST_SUCCEED);
2392
2393 size = sizeof(struct ip6t_entry_target)
2394 + target->size;
2395 target->t = fw_calloc(1, size);
2396 target->t->u.target_size = size;
2397 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002398 if (target->init != NULL)
2399 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002400 }
2401
2402 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002403 /* it is no chain, and we can't load a plugin.
2404 * We cannot know if the plugin is corrupt, non
2405 * existant OR if the user just misspelled a
2406 * chain. */
2407 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002408 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002409 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002410 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002411 }
2412 }
2413
2414 switch (command) {
2415 case CMD_APPEND:
2416 ret = append_entry(chain, e,
2417 nsaddrs, saddrs, ndaddrs, daddrs,
2418 options&OPT_VERBOSE,
2419 handle);
2420 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002421 case CMD_DELETE:
2422 ret = delete_entry(chain, e,
2423 nsaddrs, saddrs, ndaddrs, daddrs,
2424 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002425 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002426 break;
2427 case CMD_DELETE_NUM:
2428 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2429 break;
2430 case CMD_REPLACE:
2431 ret = replace_entry(chain, e, rulenum - 1,
2432 saddrs, daddrs, options&OPT_VERBOSE,
2433 handle);
2434 break;
2435 case CMD_INSERT:
2436 ret = insert_entry(chain, e, rulenum - 1,
2437 nsaddrs, saddrs, ndaddrs, daddrs,
2438 options&OPT_VERBOSE,
2439 handle);
2440 break;
2441 case CMD_LIST:
2442 ret = list_entries(chain,
2443 options&OPT_VERBOSE,
2444 options&OPT_NUMERIC,
2445 options&OPT_EXPANDED,
2446 options&OPT_LINENUMBERS,
2447 handle);
2448 break;
2449 case CMD_FLUSH:
2450 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2451 break;
2452 case CMD_ZERO:
2453 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2454 break;
2455 case CMD_LIST|CMD_ZERO:
2456 ret = list_entries(chain,
2457 options&OPT_VERBOSE,
2458 options&OPT_NUMERIC,
2459 options&OPT_EXPANDED,
2460 options&OPT_LINENUMBERS,
2461 handle);
2462 if (ret)
2463 ret = zero_entries(chain,
2464 options&OPT_VERBOSE, handle);
2465 break;
2466 case CMD_NEW_CHAIN:
2467 ret = ip6tc_create_chain(chain, handle);
2468 break;
2469 case CMD_DELETE_CHAIN:
2470 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2471 break;
2472 case CMD_RENAME_CHAIN:
2473 ret = ip6tc_rename_chain(chain, newname, handle);
2474 break;
2475 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002476 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002477 break;
2478 default:
2479 /* We should never reach this... */
2480 exit_tryhelp(2);
2481 }
2482
2483 if (verbose > 1)
2484 dump_entries6(*handle);
2485
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002486 clear_rule_matches(&matches);
2487
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002488 if (e != NULL) {
2489 free(e);
2490 e = NULL;
2491 }
2492
2493 for (c = 0; c < nsaddrs; c++)
2494 free(&saddrs[c]);
2495
2496 for (c = 0; c < ndaddrs; c++)
2497 free(&daddrs[c]);
2498
Pablo Neiradfdcd642005-05-29 19:05:23 +00002499 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002500
Rusty Russell5eed48a2000-06-02 20:12:24 +00002501 return ret;
2502}