blob: 211b81a7c8ccb8aca89608d913d6b40250725d91 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
38#include <ip6tables.h>
39#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
42#include <sys/wait.h>
43#include <sys/types.h>
44#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000053#ifndef PROC_SYS_MODPROBE
54#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55#endif
56
Rusty Russell5eed48a2000-06-02 20:12:24 +000057#define FMT_NUMERIC 0x0001
58#define FMT_NOCOUNTS 0x0002
59#define FMT_KILOMEGAGIGA 0x0004
60#define FMT_OPTIONS 0x0008
61#define FMT_NOTABLE 0x0010
62#define FMT_NOTARGET 0x0020
63#define FMT_VIA 0x0040
64#define FMT_NONEWLINE 0x0080
65#define FMT_LINENUMBERS 0x0100
66
67#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
68 | FMT_NUMERIC | FMT_NOTABLE)
69#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
70
71
72#define CMD_NONE 0x0000U
73#define CMD_INSERT 0x0001U
74#define CMD_DELETE 0x0002U
75#define CMD_DELETE_NUM 0x0004U
76#define CMD_REPLACE 0x0008U
77#define CMD_APPEND 0x0010U
78#define CMD_LIST 0x0020U
79#define CMD_FLUSH 0x0040U
80#define CMD_ZERO 0x0080U
81#define CMD_NEW_CHAIN 0x0100U
82#define CMD_DELETE_CHAIN 0x0200U
83#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000084#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000085#define NUMBER_OF_CMD 13
86static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000087 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
89#define OPTION_OFFSET 256
90
91#define OPT_NONE 0x00000U
92#define OPT_NUMERIC 0x00001U
93#define OPT_SOURCE 0x00002U
94#define OPT_DESTINATION 0x00004U
95#define OPT_PROTOCOL 0x00008U
96#define OPT_JUMP 0x00010U
97#define OPT_VERBOSE 0x00020U
98#define OPT_EXPANDED 0x00040U
99#define OPT_VIANAMEIN 0x00080U
100#define OPT_VIANAMEOUT 0x00100U
101#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000102#define OPT_COUNTERS 0x00400U
103#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
123 { "protocol", 1, 0, 'p' },
124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000135 { "modprobe", 1, 0, 'M' },
136 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000137 { 0 }
138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Rusty Russell5eed48a2000-06-02 20:12:24 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161 /* -n -s -d -p -j -v -x -i -o --line -c */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000173/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000174};
175
176static int inverse_for_options[NUMBER_OF_OPT] =
177{
178/* -n */ 0,
179/* -s */ IP6T_INV_SRCIP,
180/* -d */ IP6T_INV_DSTIP,
181/* -p */ IP6T_INV_PROTO,
182/* -j */ 0,
183/* -v */ 0,
184/* -x */ 0,
185/* -i */ IP6T_INV_VIA_IN,
186/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000187/*--line*/ 0,
188/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189};
190
191const char *program_version;
192const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000193char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000194
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +0000195/* the path to command to load kernel module */
196const char *modprobe = NULL;
197
Rusty Russell5eed48a2000-06-02 20:12:24 +0000198/* Keeping track of external matches and targets: linked lists. */
199struct ip6tables_match *ip6tables_matches = NULL;
200struct ip6tables_target *ip6tables_targets = NULL;
201
202/* Extra debugging from libiptc */
203extern void dump_entries6(const ip6tc_handle_t handle);
204
205/* A few hardcoded protocols for 'all' and in case the user has no
206 /etc/protocols */
207struct pprot {
208 char *name;
209 u_int8_t num;
210};
211
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000212/* Primitive headers... */
213/* defined in netinet/in.h */
214#if 0
215#ifndef IPPROTO_ESP
216#define IPPROTO_ESP 50
217#endif
218#ifndef IPPROTO_AH
219#define IPPROTO_AH 51
220#endif
221#endif
222
Rusty Russell5eed48a2000-06-02 20:12:24 +0000223static const struct pprot chain_protos[] = {
224 { "tcp", IPPROTO_TCP },
225 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000226 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000227 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000228 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000231};
232
233static char *
234proto_to_name(u_int8_t proto, int nolookup)
235{
236 unsigned int i;
237
238 if (proto && !nolookup) {
239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
Phil Oester58179b12006-07-20 17:00:19 +0000251int
252service_to_port(const char *name, const char *proto)
253{
254 struct servent *service;
255
256 if ((service = getservbyname(name, proto)) != NULL)
257 return ntohs((unsigned short) service->s_port);
258
259 return -1;
260}
261
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000262u_int16_t
263parse_port(const char *port, const char *proto)
264{
265 unsigned int portnum;
266
267 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
268 (portnum = service_to_port(port, proto)) != -1)
269 return (u_int16_t)portnum;
270
271 exit_error(PARAMETER_PROBLEM,
272 "invalid port/service `%s' specified", port);
273}
274
Rusty Russell5eed48a2000-06-02 20:12:24 +0000275static void
276in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
277{
278 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000279 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280}
281
Pablo Neiradfdcd642005-05-29 19:05:23 +0000282static void free_opts(int reset_offset)
283{
284 if (opts != original_opts) {
285 free(opts);
286 opts = original_opts;
287 if (reset_offset)
288 global_option_offset = 0;
289 }
290}
291
Rusty Russell5eed48a2000-06-02 20:12:24 +0000292void
293exit_error(enum exittype status, char *msg, ...)
294{
295 va_list args;
296
297 va_start(args, msg);
298 fprintf(stderr, "%s v%s: ", program_name, program_version);
299 vfprintf(stderr, msg, args);
300 va_end(args);
301 fprintf(stderr, "\n");
302 if (status == PARAMETER_PROBLEM)
303 exit_tryhelp(status);
304 if (status == VERSION_PROBLEM)
305 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000306 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000307 /* On error paths, make sure that we don't leak memory */
308 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000309 exit(status);
310}
311
312void
313exit_tryhelp(int status)
314{
Harald Weltea8658ca2003-03-05 07:46:15 +0000315 if (line != -1)
316 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000317 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
318 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000319 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000320 exit(status);
321}
322
323void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000324exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000325{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000326 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327 struct ip6tables_target *t = NULL;
328
329 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000330"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000331" %s -[RI] chain rulenum rule-specification [options]\n"
332" %s -D chain rulenum [options]\n"
333" %s -[LFZ] [chain] [options]\n"
334" %s -[NX] chain\n"
335" %s -E old-chain-name new-chain-name\n"
336" %s -P chain target [options]\n"
337" %s -h (print this help information)\n\n",
338 program_name, program_version, program_name, program_name,
339 program_name, program_name, program_name, program_name,
340 program_name, program_name);
341
342 printf(
343"Commands:\n"
344"Either long or short options are allowed.\n"
345" --append -A chain Append to chain\n"
346" --delete -D chain Delete matching rule from chain\n"
347" --delete -D chain rulenum\n"
348" Delete rule rulenum (1 = first) from chain\n"
349" --insert -I chain [rulenum]\n"
350" Insert in chain as rulenum (default 1=first)\n"
351" --replace -R chain rulenum\n"
352" Replace rule rulenum (1 = first) in chain\n"
353" --list -L [chain] List the rules in a chain or all chains\n"
354" --flush -F [chain] Delete all rules in chain or all chains\n"
355" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000356" --new -N chain Create a new user-defined chain\n"
357" --delete-chain\n"
358" -X [chain] Delete a user-defined chain\n"
359" --policy -P chain target\n"
360" Change policy on chain to target\n"
361" --rename-chain\n"
362" -E old-chain new-chain\n"
363" Change chain name, (moving any references)\n"
364
365"Options:\n"
366" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
367" --source -s [!] address[/mask]\n"
368" source specification\n"
369" --destination -d [!] address[/mask]\n"
370" destination specification\n"
371" --in-interface -i [!] input name[+]\n"
372" network interface name ([+] for wildcard)\n"
373" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000374" target for rule (may load target extension)\n"
375" --match -m match\n"
376" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000377" --numeric -n numeric output of addresses and ports\n"
378" --out-interface -o [!] output name[+]\n"
379" network interface name ([+] for wildcard)\n"
380" --table -t table table to manipulate (default: `filter')\n"
381" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000382" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000383" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000384/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000385" --modprobe=<command> try to insert modules using this command\n"
386" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000387"[!] --version -V print package version.\n");
388
389 /* Print out any special helps. A user might like to be able to add a --help
390 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000391 specified matches & targets */
392 for (t = ip6tables_targets; t; t = t->next) {
393 if (t->used) {
394 printf("\n");
395 t->help();
396 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000397 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000398 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000399 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000400 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000401 }
402 exit(0);
403}
404
405static void
406generic_opt_check(int command, int options)
407{
408 int i, j, legal = 0;
409
410 /* Check that commands are valid with options. Complicated by the
411 * fact that if an option is legal with *any* command given, it is
412 * legal overall (ie. -z and -l).
413 */
414 for (i = 0; i < NUMBER_OF_OPT; i++) {
415 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
416
417 for (j = 0; j < NUMBER_OF_CMD; j++) {
418 if (!(command & (1<<j)))
419 continue;
420
421 if (!(options & (1<<i))) {
422 if (commands_v_options[j][i] == '+')
423 exit_error(PARAMETER_PROBLEM,
424 "You need to supply the `-%c' "
425 "option for this command\n",
426 optflags[i]);
427 } else {
428 if (commands_v_options[j][i] != 'x')
429 legal = 1;
430 else if (legal == 0)
431 legal = -1;
432 }
433 }
434 if (legal == -1)
435 exit_error(PARAMETER_PROBLEM,
436 "Illegal option `-%c' with this command\n",
437 optflags[i]);
438 }
439}
440
441static char
442opt2char(int option)
443{
444 const char *ptr;
445 for (ptr = optflags; option > 1; option >>= 1, ptr++);
446
447 return *ptr;
448}
449
450static char
451cmd2char(int option)
452{
453 const char *ptr;
454 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
455
456 return *ptr;
457}
458
459static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000460add_command(unsigned int *cmd, const int newcmd, const int othercmds,
461 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462{
463 if (invert)
464 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
465 if (*cmd & (~othercmds))
466 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
467 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
468 *cmd |= newcmd;
469}
470
471int
Harald Welteb77f1da2002-03-14 11:35:58 +0000472check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000473{
474 if (option && strcmp(option, "!") == 0) {
475 if (*invert)
476 exit_error(PARAMETER_PROBLEM,
477 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000479 if (optind) {
480 *optind = *optind+1;
481 if (argc && *optind > argc)
482 exit_error(PARAMETER_PROBLEM,
483 "no argument following `!'");
484 }
485
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486 return TRUE;
487 }
488 return FALSE;
489}
490
491static void *
492fw_calloc(size_t count, size_t size)
493{
494 void *p;
495
496 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000497 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498 exit(1);
499 }
500 return p;
501}
502
503static void *
504fw_malloc(size_t size)
505{
506 void *p;
507
508 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000509 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000510 exit(1);
511 }
512 return p;
513}
514
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515static char *
516addr_to_numeric(const struct in6_addr *addrp)
517{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000518 /* 0000:0000:0000:0000:0000:000.000.000.000
519 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000520 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000521 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000522}
523
524static struct in6_addr *
525numeric_to_addr(const char *num)
526{
527 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000528 int err;
529 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000530 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000531#ifdef DEBUG
532 fprintf(stderr, "\nnumeric2addr: %d\n", err);
533#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000534 return (struct in6_addr *)NULL;
535}
536
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000537
538static struct in6_addr *
539host_to_addr(const char *name, unsigned int *naddr)
540{
541 struct addrinfo hints;
542 struct addrinfo *res;
543 static struct in6_addr *addr;
544 int err;
545
546 memset(&hints, 0, sizeof(hints));
547 hints.ai_flags=AI_CANONNAME;
548 hints.ai_family=AF_INET6;
549 hints.ai_socktype=SOCK_RAW;
550 hints.ai_protocol=41;
551 hints.ai_next=NULL;
552
553 *naddr = 0;
554 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
555#ifdef DEBUG
556 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
557#endif
558 return (struct in6_addr *) NULL;
559 } else {
560 if (res->ai_family != AF_INET6 ||
561 res->ai_addrlen != sizeof(struct sockaddr_in6))
562 return (struct in6_addr *) NULL;
563
564#ifdef DEBUG
565 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
566 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
567#endif
568 /* Get the first element of the address-chain */
569 addr = fw_calloc(1, sizeof(struct in6_addr));
570 in6addrcpy(addr, (struct in6_addr *)
571 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
572 freeaddrinfo(res);
573 *naddr = 1;
574 return addr;
575 }
576
577 return (struct in6_addr *) NULL;
578}
579
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000580static char *
581addr_to_host(const struct in6_addr *addr)
582{
583 struct sockaddr_in6 saddr;
584 int err;
585 static char hostname[NI_MAXHOST];
586
587 memset(&saddr, 0, sizeof(struct sockaddr_in6));
588 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000589 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000590
591 if ( (err=getnameinfo((struct sockaddr *)&saddr,
592 sizeof(struct sockaddr_in6),
593 hostname, sizeof(hostname)-1,
594 NULL, 0, 0)) != 0 ){
595#ifdef DEBUG
596 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
597#endif
598 return (char *) NULL;
599 } else {
600#ifdef DEBUG
601 fprintf (stderr, "\naddr2host: %s\n", hostname);
602#endif
603
604 return hostname;
605 }
606
607 return (char *) NULL;
608}
609
Rusty Russell5eed48a2000-06-02 20:12:24 +0000610static char *
611mask_to_numeric(const struct in6_addr *addrp)
612{
Harald Welte8a50ab82003-06-24 18:15:59 +0000613 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000615 if (l == -1) {
616 strcpy(buf, "/");
617 strcat(buf, addr_to_numeric(addrp));
618 return buf;
619 }
Harald Welte43ac1912002-03-03 09:43:07 +0000620 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000621 return buf;
622}
623
624static struct in6_addr *
625network_to_addr(const char *name)
626{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000627 /* abort();*/
628 /* TODO: not implemented yet, but the exception breaks the
629 * name resolvation */
630 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000631}
632
633static char *
634addr_to_anyname(const struct in6_addr *addr)
635{
636 char *name;
637
638 if ((name = addr_to_host(addr)) != NULL)
639 return name;
640
641 return addr_to_numeric(addr);
642}
643
644/*
645 * All functions starting with "parse" should succeed, otherwise
646 * the program fails.
647 * Most routines return pointers to static data that may change
648 * between calls to the same or other routines with a few exceptions:
649 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
650 * return global static data.
651*/
652
653static struct in6_addr *
654parse_hostnetwork(const char *name, unsigned int *naddrs)
655{
656 struct in6_addr *addrp, *addrptmp;
657
658 if ((addrptmp = numeric_to_addr(name)) != NULL ||
659 (addrptmp = network_to_addr(name)) != NULL) {
660 addrp = fw_malloc(sizeof(struct in6_addr));
661 in6addrcpy(addrp, addrptmp);
662 *naddrs = 1;
663 return addrp;
664 }
665 if ((addrp = host_to_addr(name, naddrs)) != NULL)
666 return addrp;
667
668 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
669}
670
671static struct in6_addr *
672parse_mask(char *mask)
673{
674 static struct in6_addr maskaddr;
675 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000676 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000677
678 if (mask == NULL) {
679 /* no mask at all defaults to 128 bits */
680 memset(&maskaddr, 0xff, sizeof maskaddr);
681 return &maskaddr;
682 }
683 if ((addrp = numeric_to_addr(mask)) != NULL)
684 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000685 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000686 exit_error(PARAMETER_PROBLEM,
687 "invalid mask `%s' specified", mask);
688 if (bits != 0) {
689 char *p = (char *)&maskaddr;
690 memset(p, 0xff, bits / 8);
691 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
692 p[bits / 8] = 0xff << (8 - (bits & 7));
693 return &maskaddr;
694 }
695
696 memset(&maskaddr, 0, sizeof maskaddr);
697 return &maskaddr;
698}
699
Harald Welte43ac1912002-03-03 09:43:07 +0000700void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
702 struct in6_addr *maskp, unsigned int *naddrs)
703{
704 struct in6_addr *addrp;
705 char buf[256];
706 char *p;
707 int i, j, n;
708
709 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000710 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711 if ((p = strrchr(buf, '/')) != NULL) {
712 *p = '\0';
713 addrp = parse_mask(p + 1);
714 } else
715 addrp = parse_mask(NULL);
716 in6addrcpy(maskp, addrp);
717
718 /* if a null mask is given, the name is ignored, like in "any/0" */
719 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
720 strcpy(buf, "::");
721
722 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
723 n = *naddrs;
724 for (i = 0, j = 0; i < n; i++) {
725 int k;
726 for (k = 0; k < 4; k++)
727 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
728 j++;
729 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000730 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000731 (*naddrs)--;
732 j--;
733 break;
734 }
735 }
736 }
737}
738
739struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000740find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000741{
742 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000743 const char *icmp6 = "icmp6";
744 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000745
746 /* This is ugly as hell. Nonetheless, there is no way of changing
747 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000748 if ( (strcmp(match_name,"icmpv6") == 0) ||
749 (strcmp(match_name,"ipv6-icmp") == 0) ||
750 (strcmp(match_name,"icmp6") == 0) )
751 name = icmp6;
752 else
753 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000754
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000755 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
756 if (strcmp(name, ptr->name) == 0) {
757 struct ip6tables_match *clone;
758
759 /* First match of this type: */
760 if (ptr->m == NULL)
761 break;
762
763 /* Second and subsequent clones */
764 clone = fw_malloc(sizeof(struct ip6tables_match));
765 memcpy(clone, ptr, sizeof(struct ip6tables_match));
766 clone->mflags = 0;
767 /* This is a clone: */
768 clone->next = clone;
769
770 ptr = clone;
771 break;
772 }
773 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774
Harald Welte3efb6ea2001-08-06 18:50:21 +0000775#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000776 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000777 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000779 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000780 if (dlopen(path, RTLD_NOW)) {
781 /* Found library. If it didn't register itself,
782 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000783 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784
785 if (!ptr)
786 exit_error(PARAMETER_PROBLEM,
787 "Couldn't load match `%s'\n",
788 name);
789 } else if (tryload == LOAD_MUST_SUCCEED)
790 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000791 "Couldn't load match `%s':%s\n",
792 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000793 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000794#else
795 if (ptr && !ptr->loaded) {
796 if (tryload != DONT_LOAD)
797 ptr->loaded = 1;
798 else
799 ptr = NULL;
800 }
Marc Boucher067477b2002-03-24 15:09:31 +0000801 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
802 exit_error(PARAMETER_PROBLEM,
803 "Couldn't find match `%s'\n", name);
804 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000805#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000806
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000807 if (ptr && matches) {
808 struct ip6tables_rule_match **i;
809 struct ip6tables_rule_match *newentry;
810
811 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
812
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000813 for (i = matches; *i; i = &(*i)->next) {
814 if (strcmp(name, (*i)->match->name) == 0)
815 (*i)->completed = 1;
816 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000817 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000818 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000819 newentry->next = NULL;
820 *i = newentry;
821 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000822
Rusty Russell5eed48a2000-06-02 20:12:24 +0000823 return ptr;
824}
825
826/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
827static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000828find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000829{
Harald Welteed498492001-07-23 01:24:22 +0000830 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831
Harald Welte43ac1912002-03-03 09:43:07 +0000832 if (string_to_number(pname, 0, 255, &proto) != -1) {
833 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000834
Harald Welte43ac1912002-03-03 09:43:07 +0000835 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000836 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000837 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000838 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000839
840 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841}
842
Harald Welte43ac1912002-03-03 09:43:07 +0000843u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844parse_protocol(const char *s)
845{
Harald Welteed498492001-07-23 01:24:22 +0000846 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000847
Harald Welteed498492001-07-23 01:24:22 +0000848 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849 struct protoent *pent;
850
Harald Weltecbe1ec72006-02-11 09:50:11 +0000851 /* first deal with the special case of 'all' to prevent
852 * people from being able to redefine 'all' in nsswitch
853 * and/or provoke expensive [not working] ldap/nis/...
854 * lookups */
855 if (!strcmp(s, "all"))
856 return 0;
857
Rusty Russell5eed48a2000-06-02 20:12:24 +0000858 if ((pent = getprotobyname(s)))
859 proto = pent->p_proto;
860 else {
861 unsigned int i;
862 for (i = 0;
863 i < sizeof(chain_protos)/sizeof(struct pprot);
864 i++) {
865 if (strcmp(s, chain_protos[i].name) == 0) {
866 proto = chain_protos[i].num;
867 break;
868 }
869 }
870 if (i == sizeof(chain_protos)/sizeof(struct pprot))
871 exit_error(PARAMETER_PROBLEM,
872 "unknown protocol `%s' specified",
873 s);
874 }
875 }
876
877 return (u_int16_t)proto;
878}
879
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000880/* proto means IPv6 extension header ? */
881static int is_exthdr(u_int16_t proto)
882{
883 return (proto == IPPROTO_HOPOPTS ||
884 proto == IPPROTO_ROUTING ||
885 proto == IPPROTO_FRAGMENT ||
886 proto == IPPROTO_ESP ||
887 proto == IPPROTO_AH ||
888 proto == IPPROTO_DSTOPTS);
889}
890
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000891void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892{
893 int vialen = strlen(arg);
894 unsigned int i;
895
896 memset(mask, 0, IFNAMSIZ);
897 memset(vianame, 0, IFNAMSIZ);
898
899 if (vialen + 1 > IFNAMSIZ)
900 exit_error(PARAMETER_PROBLEM,
901 "interface name `%s' must be shorter than IFNAMSIZ"
902 " (%i)", arg, IFNAMSIZ-1);
903
904 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000905 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906 memset(mask, 0, IFNAMSIZ);
907 else if (vianame[vialen - 1] == '+') {
908 memset(mask, 0xFF, vialen - 1);
909 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000910 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000911 } else {
912 /* Include nul-terminator in match */
913 memset(mask, 0xFF, vialen + 1);
914 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000915 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000916 if (vianame[i] == ':' ||
917 vianame[i] == '!' ||
918 vianame[i] == '*') {
919 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000920 " `%s' (No aliases, :, ! or *).\n",
921 vianame);
922 break;
923 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000924 }
925 }
926}
927
928/* Can't be zero. */
929static int
930parse_rulenumber(const char *rule)
931{
Harald Welte43ac1912002-03-03 09:43:07 +0000932 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933
Harald Welte43ac1912002-03-03 09:43:07 +0000934 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935 exit_error(PARAMETER_PROBLEM,
936 "Invalid rule number `%s'", rule);
937
938 return rulenum;
939}
940
941static const char *
942parse_target(const char *targetname)
943{
944 const char *ptr;
945
946 if (strlen(targetname) < 1)
947 exit_error(PARAMETER_PROBLEM,
948 "Invalid target name (too short)");
949
950 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
951 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000952 "Invalid target name `%s' (%u chars max)",
953 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000954
955 for (ptr = targetname; *ptr; ptr++)
956 if (isspace(*ptr))
957 exit_error(PARAMETER_PROBLEM,
958 "Invalid target name `%s'", targetname);
959 return targetname;
960}
961
962int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000963string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
964 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000965{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000966 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000967 char *end;
968
969 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000970 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000971 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000972 if (*end == '\0' && end != s) {
973 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000974 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000975 *ret = number;
976 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000977 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 }
979 return -1;
980}
981
Martin Josefssonb105bc92004-05-26 15:54:49 +0000982int
983string_to_number_l(const char *s, unsigned long min, unsigned long max,
984 unsigned long *ret)
985{
986 int result;
987 unsigned long long number;
988
989 result = string_to_number_ll(s, min, max, &number);
990 *ret = (unsigned long)number;
991
992 return result;
993}
994
995int string_to_number(const char *s, unsigned int min, unsigned int max,
996 unsigned int *ret)
997{
998 int result;
999 unsigned long number;
1000
1001 result = string_to_number_l(s, min, max, &number);
1002 *ret = (unsigned int)number;
1003
1004 return result;
1005}
1006
Rusty Russell5eed48a2000-06-02 20:12:24 +00001007static void
1008set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1009 int invert)
1010{
1011 if (*options & option)
1012 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1013 opt2char(option));
1014 *options |= option;
1015
1016 if (invert) {
1017 unsigned int i;
1018 for (i = 0; 1 << i != option; i++);
1019
1020 if (!inverse_for_options[i])
1021 exit_error(PARAMETER_PROBLEM,
1022 "cannot have ! before -%c",
1023 opt2char(option));
1024 *invflg |= inverse_for_options[i];
1025 }
1026}
1027
1028struct ip6tables_target *
1029find_target(const char *name, enum ip6t_tryload tryload)
1030{
1031 struct ip6tables_target *ptr;
1032
1033 /* Standard target? */
1034 if (strcmp(name, "") == 0
1035 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1036 || strcmp(name, IP6TC_LABEL_DROP) == 0
1037 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1038 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1039 name = "standard";
1040
1041 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1042 if (strcmp(name, ptr->name) == 0)
1043 break;
1044 }
1045
Harald Welte3efb6ea2001-08-06 18:50:21 +00001046#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001047 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001048 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001049 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001050 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001051 if (dlopen(path, RTLD_NOW)) {
1052 /* Found library. If it didn't register itself,
1053 maybe they specified match as a target. */
1054 ptr = find_target(name, DONT_LOAD);
1055 if (!ptr)
1056 exit_error(PARAMETER_PROBLEM,
1057 "Couldn't load target `%s'\n",
1058 name);
1059 } else if (tryload == LOAD_MUST_SUCCEED)
1060 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001061 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001062 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001063 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001064#else
1065 if (ptr && !ptr->loaded) {
1066 if (tryload != DONT_LOAD)
1067 ptr->loaded = 1;
1068 else
1069 ptr = NULL;
1070 }
Marc Boucher067477b2002-03-24 15:09:31 +00001071 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1072 exit_error(PARAMETER_PROBLEM,
1073 "Couldn't find target `%s'\n", name);
1074 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001075#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001076
Harald Welte43ac1912002-03-03 09:43:07 +00001077 if (ptr)
1078 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001079
Rusty Russell5eed48a2000-06-02 20:12:24 +00001080 return ptr;
1081}
1082
1083static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001084merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001085 unsigned int *option_offset)
1086{
1087 unsigned int num_old, num_new, i;
1088 struct option *merge;
1089
1090 for (num_old = 0; oldopts[num_old].name; num_old++);
1091 for (num_new = 0; newopts[num_new].name; num_new++);
1092
1093 global_option_offset += OPTION_OFFSET;
1094 *option_offset = global_option_offset;
1095
1096 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1097 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001098 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001099 for (i = 0; i < num_new; i++) {
1100 merge[num_old + i] = newopts[i];
1101 merge[num_old + i].val += *option_offset;
1102 }
1103 memset(merge + num_old + num_new, 0, sizeof(struct option));
1104
1105 return merge;
1106}
1107
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001108static int compatible_revision(const char *name, u_int8_t revision, int opt)
1109{
1110 struct ip6t_get_revision rev;
1111 socklen_t s = sizeof(rev);
1112 int max_rev, sockfd;
1113
1114 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1115 if (sockfd < 0) {
1116 fprintf(stderr, "Could not open socket to kernel: %s\n",
1117 strerror(errno));
1118 exit(1);
1119 }
1120
1121 strcpy(rev.name, name);
1122 rev.revision = revision;
1123
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001124 load_ip6tables_ko(modprobe);
1125
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001126 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1127 if (max_rev < 0) {
1128 /* Definitely don't support this? */
1129 if (errno == EPROTONOSUPPORT) {
1130 close(sockfd);
1131 return 0;
1132 } else if (errno == ENOPROTOOPT) {
1133 close(sockfd);
1134 /* Assume only revision 0 support (old kernel) */
1135 return (revision == 0);
1136 } else {
1137 fprintf(stderr, "getsockopt failed strangely: %s\n",
1138 strerror(errno));
1139 exit(1);
1140 }
1141 }
1142 close(sockfd);
1143 return 1;
1144}
1145
1146static int compatible_match_revision(const char *name, u_int8_t revision)
1147{
1148 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1149}
1150
Rusty Russell5eed48a2000-06-02 20:12:24 +00001151void
1152register_match6(struct ip6tables_match *me)
1153{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001154 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001155
Rusty Russell5eed48a2000-06-02 20:12:24 +00001156 if (strcmp(me->version, program_version) != 0) {
1157 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1158 program_name, me->name, me->version, program_version);
1159 exit(1);
1160 }
1161
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001162 /* Revision field stole a char from name. */
1163 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1164 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001165 program_name, me->name);
1166 exit(1);
1167 }
1168
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001169 old = find_match(me->name, DURING_LOAD, NULL);
1170 if (old) {
1171 if (old->revision == me->revision) {
1172 fprintf(stderr,
1173 "%s: match `%s' already registered.\n",
1174 program_name, me->name);
1175 exit(1);
1176 }
1177
1178 /* Now we have two (or more) options, check compatibility. */
1179 if (compatible_match_revision(old->name, old->revision)
1180 && old->revision > me->revision)
1181 return;
1182
1183 /* Replace if compatible. */
1184 if (!compatible_match_revision(me->name, me->revision))
1185 return;
1186
1187 /* Delete old one. */
1188 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1189 *i = old->next;
1190 }
1191
Harald Welte43ac1912002-03-03 09:43:07 +00001192 if (me->size != IP6T_ALIGN(me->size)) {
1193 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001194 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001195 exit(1);
1196 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001197
Harald Welte43ac1912002-03-03 09:43:07 +00001198 /* Append to list. */
1199 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1200 me->next = NULL;
1201 *i = me;
1202
Rusty Russell5eed48a2000-06-02 20:12:24 +00001203 me->m = NULL;
1204 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001205}
1206
1207void
1208register_target6(struct ip6tables_target *me)
1209{
1210 if (strcmp(me->version, program_version) != 0) {
1211 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1212 program_name, me->name, me->version, program_version);
1213 exit(1);
1214 }
1215
Jones Desougif5b86e62005-12-22 03:33:50 +00001216 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001217 fprintf(stderr, "%s: target `%s' already registered.\n",
1218 program_name, me->name);
1219 exit(1);
1220 }
1221
Harald Welte43ac1912002-03-03 09:43:07 +00001222 if (me->size != IP6T_ALIGN(me->size)) {
1223 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001224 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001225 exit(1);
1226 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001227
Rusty Russell5eed48a2000-06-02 20:12:24 +00001228 /* Prepend to list. */
1229 me->next = ip6tables_targets;
1230 ip6tables_targets = me;
1231 me->t = NULL;
1232 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001233}
1234
1235static void
1236print_num(u_int64_t number, unsigned int format)
1237{
Harald Welte43ac1912002-03-03 09:43:07 +00001238 if (format & FMT_KILOMEGAGIGA) {
1239 if (number > 99999) {
1240 number = (number + 500) / 1000;
1241 if (number > 9999) {
1242 number = (number + 500) / 1000;
1243 if (number > 9999) {
1244 number = (number + 500) / 1000;
1245 if (number > 9999) {
1246 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001247 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001248 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001249 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001250 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001251 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001252 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001253 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001254 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001255 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001256 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001257 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001258}
1259
Harald Welte43ac1912002-03-03 09:43:07 +00001260
Rusty Russell5eed48a2000-06-02 20:12:24 +00001261static void
1262print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1263{
1264 struct ip6t_counters counters;
1265 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1266 printf("Chain %s", chain);
1267 if (pol) {
1268 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001269 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001270 fputc(' ', stdout);
1271 print_num(counters.pcnt, (format|FMT_NOTABLE));
1272 fputs("packets, ", stdout);
1273 print_num(counters.bcnt, (format|FMT_NOTABLE));
1274 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001275 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001276 printf(")\n");
1277 } else {
1278 unsigned int refs;
1279 if (!ip6tc_get_references(&refs, chain, handle))
1280 printf(" (ERROR obtaining refs)\n");
1281 else
1282 printf(" (%u references)\n", refs);
1283 }
1284
1285 if (format & FMT_LINENUMBERS)
1286 printf(FMT("%-4s ", "%s "), "num");
1287 if (!(format & FMT_NOCOUNTS)) {
1288 if (format & FMT_KILOMEGAGIGA) {
1289 printf(FMT("%5s ","%s "), "pkts");
1290 printf(FMT("%5s ","%s "), "bytes");
1291 } else {
1292 printf(FMT("%8s ","%s "), "pkts");
1293 printf(FMT("%10s ","%s "), "bytes");
1294 }
1295 }
1296 if (!(format & FMT_NOTARGET))
1297 printf(FMT("%-9s ","%s "), "target");
1298 fputs(" prot ", stdout);
1299 if (format & FMT_OPTIONS)
1300 fputs("opt", stdout);
1301 if (format & FMT_VIA) {
1302 printf(FMT(" %-6s ","%s "), "in");
1303 printf(FMT("%-6s ","%s "), "out");
1304 }
1305 printf(FMT(" %-19s ","%s "), "source");
1306 printf(FMT(" %-19s "," %s "), "destination");
1307 printf("\n");
1308}
1309
Harald Welte43ac1912002-03-03 09:43:07 +00001310
Rusty Russell5eed48a2000-06-02 20:12:24 +00001311static int
1312print_match(const struct ip6t_entry_match *m,
1313 const struct ip6t_ip6 *ip,
1314 int numeric)
1315{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001316 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001317
1318 if (match) {
1319 if (match->print)
1320 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001321 else
1322 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001323 } else {
1324 if (m->u.user.name[0])
1325 printf("UNKNOWN match `%s' ", m->u.user.name);
1326 }
1327 /* Don't stop iterating. */
1328 return 0;
1329}
1330
1331/* e is called `fw' here for hysterical raisins */
1332static void
1333print_firewall(const struct ip6t_entry *fw,
1334 const char *targname,
1335 unsigned int num,
1336 unsigned int format,
1337 const ip6tc_handle_t handle)
1338{
1339 struct ip6tables_target *target = NULL;
1340 const struct ip6t_entry_target *t;
1341 u_int8_t flags;
1342 char buf[BUFSIZ];
1343
Rusty Russell5eed48a2000-06-02 20:12:24 +00001344 if (!ip6tc_is_chain(targname, handle))
1345 target = find_target(targname, TRY_LOAD);
1346 else
1347 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1348
1349 t = ip6t_get_target((struct ip6t_entry *)fw);
1350 flags = fw->ipv6.flags;
1351
1352 if (format & FMT_LINENUMBERS)
1353 printf(FMT("%-4u ", "%u "), num+1);
1354
1355 if (!(format & FMT_NOCOUNTS)) {
1356 print_num(fw->counters.pcnt, format);
1357 print_num(fw->counters.bcnt, format);
1358 }
1359
1360 if (!(format & FMT_NOTARGET))
1361 printf(FMT("%-9s ", "%s "), targname);
1362
1363 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1364 {
Harald Welte43ac1912002-03-03 09:43:07 +00001365 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001366 if (pname)
1367 printf(FMT("%-5s", "%s "), pname);
1368 else
1369 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1370 }
1371
1372 if (format & FMT_OPTIONS) {
1373 if (format & FMT_NOTABLE)
1374 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001375 fputc(' ', stdout); /* Invert flag of FRAG */
1376 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001377 fputc(' ', stdout);
1378 }
1379
1380 if (format & FMT_VIA) {
1381 char iface[IFNAMSIZ+2];
1382
1383 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1384 iface[0] = '!';
1385 iface[1] = '\0';
1386 }
1387 else iface[0] = '\0';
1388
1389 if (fw->ipv6.iniface[0] != '\0') {
1390 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 }
1392 else if (format & FMT_NUMERIC) strcat(iface, "*");
1393 else strcat(iface, "any");
1394 printf(FMT(" %-6s ","in %s "), iface);
1395
1396 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1397 iface[0] = '!';
1398 iface[1] = '\0';
1399 }
1400 else iface[0] = '\0';
1401
1402 if (fw->ipv6.outiface[0] != '\0') {
1403 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001404 }
1405 else if (format & FMT_NUMERIC) strcat(iface, "*");
1406 else strcat(iface, "any");
1407 printf(FMT("%-6s ","out %s "), iface);
1408 }
1409
1410 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1411 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1412 && !(format & FMT_NUMERIC))
1413 printf(FMT("%-19s ","%s "), "anywhere");
1414 else {
1415 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001416 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 else
Harald Welte43ac1912002-03-03 09:43:07 +00001418 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1420 printf(FMT("%-19s ","%s "), buf);
1421 }
1422
1423 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1424 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1425 && !(format & FMT_NUMERIC))
1426 printf(FMT("%-19s","-> %s"), "anywhere");
1427 else {
1428 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001429 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001430 else
Harald Welte43ac1912002-03-03 09:43:07 +00001431 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1433 printf(FMT("%-19s","-> %s"), buf);
1434 }
1435
1436 if (format & FMT_NOTABLE)
1437 fputs(" ", stdout);
1438
1439 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1440
1441 if (target) {
1442 if (target->print)
1443 /* Print the target information. */
1444 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1445 } else if (t->u.target_size != sizeof(*t))
1446 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001447 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001448
1449 if (!(format & FMT_NONEWLINE))
1450 fputc('\n', stdout);
1451}
1452
1453static void
1454print_firewall_line(const struct ip6t_entry *fw,
1455 const ip6tc_handle_t h)
1456{
1457 struct ip6t_entry_target *t;
1458
1459 t = ip6t_get_target((struct ip6t_entry *)fw);
1460 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1461}
1462
1463static int
1464append_entry(const ip6t_chainlabel chain,
1465 struct ip6t_entry *fw,
1466 unsigned int nsaddrs,
1467 const struct in6_addr saddrs[],
1468 unsigned int ndaddrs,
1469 const struct in6_addr daddrs[],
1470 int verbose,
1471 ip6tc_handle_t *handle)
1472{
1473 unsigned int i, j;
1474 int ret = 1;
1475
1476 for (i = 0; i < nsaddrs; i++) {
1477 fw->ipv6.src = saddrs[i];
1478 for (j = 0; j < ndaddrs; j++) {
1479 fw->ipv6.dst = daddrs[j];
1480 if (verbose)
1481 print_firewall_line(fw, *handle);
1482 ret &= ip6tc_append_entry(chain, fw, handle);
1483 }
1484 }
1485
1486 return ret;
1487}
1488
1489static int
1490replace_entry(const ip6t_chainlabel chain,
1491 struct ip6t_entry *fw,
1492 unsigned int rulenum,
1493 const struct in6_addr *saddr,
1494 const struct in6_addr *daddr,
1495 int verbose,
1496 ip6tc_handle_t *handle)
1497{
1498 fw->ipv6.src = *saddr;
1499 fw->ipv6.dst = *daddr;
1500
1501 if (verbose)
1502 print_firewall_line(fw, *handle);
1503 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1504}
1505
1506static int
1507insert_entry(const ip6t_chainlabel chain,
1508 struct ip6t_entry *fw,
1509 unsigned int rulenum,
1510 unsigned int nsaddrs,
1511 const struct in6_addr saddrs[],
1512 unsigned int ndaddrs,
1513 const struct in6_addr daddrs[],
1514 int verbose,
1515 ip6tc_handle_t *handle)
1516{
1517 unsigned int i, j;
1518 int ret = 1;
1519
1520 for (i = 0; i < nsaddrs; i++) {
1521 fw->ipv6.src = saddrs[i];
1522 for (j = 0; j < ndaddrs; j++) {
1523 fw->ipv6.dst = daddrs[j];
1524 if (verbose)
1525 print_firewall_line(fw, *handle);
1526 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1527 }
1528 }
1529
1530 return ret;
1531}
1532
1533static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001534make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001535{
1536 /* Establish mask for comparison */
1537 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001538 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001539 unsigned char *mask, *mptr;
1540
1541 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001542 for (matchp = matches; matchp; matchp = matchp->next)
1543 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544
1545 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001546 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 + ip6tables_targets->size);
1548
1549 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1550 mptr = mask + sizeof(struct ip6t_entry);
1551
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001552 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001553 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001554 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001555 + matchp->match->userspacesize);
1556 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001557 }
1558
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001559 memset(mptr, 0xFF,
1560 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1561 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001562
1563 return mask;
1564}
1565
1566static int
1567delete_entry(const ip6t_chainlabel chain,
1568 struct ip6t_entry *fw,
1569 unsigned int nsaddrs,
1570 const struct in6_addr saddrs[],
1571 unsigned int ndaddrs,
1572 const struct in6_addr daddrs[],
1573 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001574 ip6tc_handle_t *handle,
1575 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001576{
1577 unsigned int i, j;
1578 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001579 unsigned char *mask;
1580
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001581 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001582 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001583 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001584 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001585 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001586 if (verbose)
1587 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001588 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001589 }
1590 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001591 free(mask);
1592
Rusty Russell5eed48a2000-06-02 20:12:24 +00001593 return ret;
1594}
1595
András Kis-Szabó764316a2001-02-26 17:31:20 +00001596int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001597for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1598 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1599{
1600 int ret = 1;
1601 const char *chain;
1602 char *chains;
1603 unsigned int i, chaincount = 0;
1604
1605 chain = ip6tc_first_chain(handle);
1606 while (chain) {
1607 chaincount++;
1608 chain = ip6tc_next_chain(handle);
1609 }
1610
1611 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1612 i = 0;
1613 chain = ip6tc_first_chain(handle);
1614 while (chain) {
1615 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1616 i++;
1617 chain = ip6tc_next_chain(handle);
1618 }
1619
1620 for (i = 0; i < chaincount; i++) {
1621 if (!builtinstoo
1622 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001623 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001624 continue;
1625 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1626 }
1627
1628 free(chains);
1629 return ret;
1630}
1631
András Kis-Szabó764316a2001-02-26 17:31:20 +00001632int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001633flush_entries(const ip6t_chainlabel chain, int verbose,
1634 ip6tc_handle_t *handle)
1635{
1636 if (!chain)
1637 return for_each_chain(flush_entries, verbose, 1, handle);
1638
1639 if (verbose)
1640 fprintf(stdout, "Flushing chain `%s'\n", chain);
1641 return ip6tc_flush_entries(chain, handle);
1642}
1643
1644static int
1645zero_entries(const ip6t_chainlabel chain, int verbose,
1646 ip6tc_handle_t *handle)
1647{
1648 if (!chain)
1649 return for_each_chain(zero_entries, verbose, 1, handle);
1650
1651 if (verbose)
1652 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1653 return ip6tc_zero_entries(chain, handle);
1654}
1655
András Kis-Szabó764316a2001-02-26 17:31:20 +00001656int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001657delete_chain(const ip6t_chainlabel chain, int verbose,
1658 ip6tc_handle_t *handle)
1659{
1660 if (!chain)
1661 return for_each_chain(delete_chain, verbose, 0, handle);
1662
1663 if (verbose)
1664 fprintf(stdout, "Deleting chain `%s'\n", chain);
1665 return ip6tc_delete_chain(chain, handle);
1666}
1667
1668static int
1669list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1670 int expanded, int linenumbers, ip6tc_handle_t *handle)
1671{
1672 int found = 0;
1673 unsigned int format;
1674 const char *this;
1675
1676 format = FMT_OPTIONS;
1677 if (!verbose)
1678 format |= FMT_NOCOUNTS;
1679 else
1680 format |= FMT_VIA;
1681
1682 if (numeric)
1683 format |= FMT_NUMERIC;
1684
1685 if (!expanded)
1686 format |= FMT_KILOMEGAGIGA;
1687
1688 if (linenumbers)
1689 format |= FMT_LINENUMBERS;
1690
1691 for (this = ip6tc_first_chain(handle);
1692 this;
1693 this = ip6tc_next_chain(handle)) {
1694 const struct ip6t_entry *i;
1695 unsigned int num;
1696
1697 if (chain && strcmp(chain, this) != 0)
1698 continue;
1699
1700 if (found) printf("\n");
1701
1702 print_header(format, this, handle);
1703 i = ip6tc_first_rule(this, handle);
1704
1705 num = 0;
1706 while (i) {
1707 print_firewall(i,
1708 ip6tc_get_target(i, handle),
1709 num++,
1710 format,
1711 *handle);
1712 i = ip6tc_next_rule(i, handle);
1713 }
1714 found = 1;
1715 }
1716
1717 errno = ENOENT;
1718 return found;
1719}
1720
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001721static char *get_modprobe(void)
1722{
Harald Welte43ac1912002-03-03 09:43:07 +00001723 int procfile;
1724 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001725
Harald Welte10f7f142004-10-22 08:14:07 +00001726#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001727 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1728 if (procfile < 0)
1729 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001730
Harald Welte10f7f142004-10-22 08:14:07 +00001731 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001732 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001733 memset(ret, 0, PROCFILE_BUFSIZ);
1734 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001735 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001736 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001737 }
1738 if (ret[strlen(ret)-1]=='\n')
1739 ret[strlen(ret)-1]=0;
1740 close(procfile);
1741 return ret;
1742 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001743 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001744 free(ret);
1745 close(procfile);
1746 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001747}
1748
Harald Welte58918652001-06-16 18:25:25 +00001749int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001750{
Harald Welte43ac1912002-03-03 09:43:07 +00001751 char *buf = NULL;
1752 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001753 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001754
Harald Welte43ac1912002-03-03 09:43:07 +00001755 /* If they don't explicitly set it, read out of kernel */
1756 if (!modprobe) {
1757 buf = get_modprobe();
1758 if (!buf)
1759 return -1;
1760 modprobe = buf;
1761 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001762
Harald Welte43ac1912002-03-03 09:43:07 +00001763 switch (fork()) {
1764 case 0:
1765 argv[0] = (char *)modprobe;
1766 argv[1] = (char *)modname;
1767 argv[2] = NULL;
1768 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001769
Harald Welte43ac1912002-03-03 09:43:07 +00001770 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001771 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001772 case -1:
1773 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001774
Harald Welte43ac1912002-03-03 09:43:07 +00001775 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001776 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001777 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001778
Harald Welte43ac1912002-03-03 09:43:07 +00001779 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001780 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1781 return 0;
1782 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001783}
1784
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001785int load_ip6tables_ko(const char *modprobe)
1786{
1787 static int loaded = 0;
1788 static int ret = -1;
1789
1790 if (!loaded) {
1791 ret = ip6tables_insmod("ip6_tables", modprobe);
1792 loaded = 1;
1793 }
1794
1795 return ret;
1796}
1797
Rusty Russell5eed48a2000-06-02 20:12:24 +00001798static struct ip6t_entry *
1799generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001800 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001801 struct ip6t_entry_target *target)
1802{
1803 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001804 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001805 struct ip6t_entry *e;
1806
1807 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001808 for (matchp = matches; matchp; matchp = matchp->next)
1809 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810
1811 e = fw_malloc(size + target->u.target_size);
1812 *e = *fw;
1813 e->target_offset = size;
1814 e->next_offset = size + target->u.target_size;
1815
1816 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001817 for (matchp = matches; matchp; matchp = matchp->next) {
1818 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1819 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001820 }
1821 memcpy(e->elems + size, target, target->u.target_size);
1822
1823 return e;
1824}
1825
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001826void clear_rule_matches(struct ip6tables_rule_match **matches)
1827{
1828 struct ip6tables_rule_match *matchp, *tmp;
1829
1830 for (matchp = *matches; matchp;) {
1831 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001832 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001833 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001834 matchp->match->m = NULL;
1835 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001836 if (matchp->match == matchp->match->next) {
1837 free(matchp->match);
1838 matchp->match = NULL;
1839 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001840 free(matchp);
1841 matchp = tmp;
1842 }
1843
1844 *matches = NULL;
1845}
1846
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001847static void set_revision(char *name, u_int8_t revision)
1848{
1849 /* Old kernel sources don't have ".revision" field,
1850 but we stole a byte from name. */
1851 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1852 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1853}
1854
Rusty Russell5eed48a2000-06-02 20:12:24 +00001855int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1856{
1857 struct ip6t_entry fw, *e = NULL;
1858 int invert = 0;
1859 unsigned int nsaddrs = 0, ndaddrs = 0;
1860 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1861
1862 int c, verbose = 0;
1863 const char *chain = NULL;
1864 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1865 const char *policy = NULL, *newname = NULL;
1866 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001867 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001868 int ret = 1;
1869 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001870 struct ip6tables_rule_match *matches = NULL;
1871 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001872 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001873 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874 const char *jumpto = "";
1875 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001876 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001877
1878 memset(&fw, 0, sizeof(fw));
1879
Harald Welte43ac1912002-03-03 09:43:07 +00001880 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001881 * a second time */
1882 optind = 0;
1883
Harald Welte43ac1912002-03-03 09:43:07 +00001884 /* clear mflags in case do_command gets called a second time
1885 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001886 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001887 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001888
Harald Welte43ac1912002-03-03 09:43:07 +00001889 for (t = ip6tables_targets; t; t = t->next) {
1890 t->tflags = 0;
1891 t->used = 0;
1892 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001893
Rusty Russell5eed48a2000-06-02 20:12:24 +00001894 /* Suppress error messages: we may add new options if we
1895 demand-load a protocol. */
1896 opterr = 0;
1897
1898 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001899 "-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 +00001900 opts, NULL)) != -1) {
1901 switch (c) {
1902 /*
1903 * Command selection
1904 */
1905 case 'A':
1906 add_command(&command, CMD_APPEND, CMD_NONE,
1907 invert);
1908 chain = optarg;
1909 break;
1910
1911 case 'D':
1912 add_command(&command, CMD_DELETE, CMD_NONE,
1913 invert);
1914 chain = optarg;
1915 if (optind < argc && argv[optind][0] != '-'
1916 && argv[optind][0] != '!') {
1917 rulenum = parse_rulenumber(argv[optind++]);
1918 command = CMD_DELETE_NUM;
1919 }
1920 break;
1921
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922 case 'R':
1923 add_command(&command, CMD_REPLACE, 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
1930 exit_error(PARAMETER_PROBLEM,
1931 "-%c requires a rule number",
1932 cmd2char(CMD_REPLACE));
1933 break;
1934
1935 case 'I':
1936 add_command(&command, CMD_INSERT, CMD_NONE,
1937 invert);
1938 chain = optarg;
1939 if (optind < argc && argv[optind][0] != '-'
1940 && argv[optind][0] != '!')
1941 rulenum = parse_rulenumber(argv[optind++]);
1942 else rulenum = 1;
1943 break;
1944
1945 case 'L':
1946 add_command(&command, CMD_LIST, CMD_ZERO,
1947 invert);
1948 if (optarg) chain = optarg;
1949 else if (optind < argc && argv[optind][0] != '-'
1950 && argv[optind][0] != '!')
1951 chain = argv[optind++];
1952 break;
1953
1954 case 'F':
1955 add_command(&command, CMD_FLUSH, CMD_NONE,
1956 invert);
1957 if (optarg) chain = optarg;
1958 else if (optind < argc && argv[optind][0] != '-'
1959 && argv[optind][0] != '!')
1960 chain = argv[optind++];
1961 break;
1962
1963 case 'Z':
1964 add_command(&command, CMD_ZERO, CMD_LIST,
1965 invert);
1966 if (optarg) chain = optarg;
1967 else if (optind < argc && argv[optind][0] != '-'
1968 && argv[optind][0] != '!')
1969 chain = argv[optind++];
1970 break;
1971
1972 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001973 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001974 exit_error(PARAMETER_PROBLEM,
1975 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001976 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001977 if (find_target(optarg, TRY_LOAD))
1978 exit_error(PARAMETER_PROBLEM,
1979 "chain name may not clash "
1980 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001981 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1982 invert);
1983 chain = optarg;
1984 break;
1985
1986 case 'X':
1987 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1988 invert);
1989 if (optarg) chain = optarg;
1990 else if (optind < argc && argv[optind][0] != '-'
1991 && argv[optind][0] != '!')
1992 chain = argv[optind++];
1993 break;
1994
1995 case 'E':
1996 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1997 invert);
1998 chain = optarg;
1999 if (optind < argc && argv[optind][0] != '-'
2000 && argv[optind][0] != '!')
2001 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002002 else
2003 exit_error(PARAMETER_PROBLEM,
2004 "-%c requires old-chain-name and "
2005 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00002006 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007 break;
2008
2009 case 'P':
2010 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2011 invert);
2012 chain = optarg;
2013 if (optind < argc && argv[optind][0] != '-'
2014 && argv[optind][0] != '!')
2015 policy = argv[optind++];
2016 else
2017 exit_error(PARAMETER_PROBLEM,
2018 "-%c requires a chain and a policy",
2019 cmd2char(CMD_SET_POLICY));
2020 break;
2021
2022 case 'h':
2023 if (!optarg)
2024 optarg = argv[optind];
2025
Jonas Berlin1b91e592005-04-01 06:58:38 +00002026 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002027 if (!matches && protocol)
2028 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002029
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002030 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002031
2032 /*
2033 * Option selection
2034 */
2035 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002036 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002037 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2038 invert);
2039
2040 /* Canonicalize into lower case */
2041 for (protocol = argv[optind-1]; *protocol; protocol++)
2042 *protocol = tolower(*protocol);
2043
2044 protocol = argv[optind-1];
2045 fw.ipv6.proto = parse_protocol(protocol);
2046 fw.ipv6.flags |= IP6T_F_PROTO;
2047
2048 if (fw.ipv6.proto == 0
2049 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2050 exit_error(PARAMETER_PROBLEM,
2051 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002052
2053 if (fw.ipv6.proto != IPPROTO_ESP &&
2054 is_exthdr(fw.ipv6.proto))
2055 printf("Warning: never matched protocol: %s. "
2056 "use exension match instead.", protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002057 break;
2058
2059 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002060 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2062 invert);
2063 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002064 break;
2065
2066 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002067 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002068 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2069 invert);
2070 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002071 break;
2072
2073 case 'j':
2074 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2075 invert);
2076 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00002077 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002078 target = find_target(jumpto, TRY_LOAD);
2079
2080 if (target) {
2081 size_t size;
2082
Harald Welte43ac1912002-03-03 09:43:07 +00002083 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2084 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002085
2086 target->t = fw_calloc(1, size);
2087 target->t->u.target_size = size;
2088 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002089 if (target->init != NULL)
2090 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002091 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002092 }
2093 break;
2094
2095
2096 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002097 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002098 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2099 invert);
2100 parse_interface(argv[optind-1],
2101 fw.ipv6.iniface,
2102 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002103 break;
2104
2105 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002106 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002107 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2108 invert);
2109 parse_interface(argv[optind-1],
2110 fw.ipv6.outiface,
2111 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 break;
2113
2114 case 'v':
2115 if (!verbose)
2116 set_option(&options, OPT_VERBOSE,
2117 &fw.ipv6.invflags, invert);
2118 verbose++;
2119 break;
2120
2121 case 'm': {
2122 size_t size;
2123
2124 if (invert)
2125 exit_error(PARAMETER_PROBLEM,
2126 "unexpected ! flag before --match");
2127
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002128 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002129 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2130 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002131 m->m = fw_calloc(1, size);
2132 m->m->u.match_size = size;
2133 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002134 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002135 if (m->init != NULL)
2136 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002137 if (m != m->next)
2138 /* Merge options for non-cloned matches */
2139 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002140 }
2141 break;
2142
2143 case 'n':
2144 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2145 invert);
2146 break;
2147
2148 case 't':
2149 if (invert)
2150 exit_error(PARAMETER_PROBLEM,
2151 "unexpected ! flag before --table");
2152 *table = argv[optind-1];
2153 break;
2154
2155 case 'x':
2156 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2157 invert);
2158 break;
2159
2160 case 'V':
2161 if (invert)
2162 printf("Not %s ;-)\n", program_version);
2163 else
2164 printf("%s v%s\n",
2165 program_name, program_version);
2166 exit(0);
2167
2168 case '0':
2169 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2170 invert);
2171 break;
2172
Harald Welte43ac1912002-03-03 09:43:07 +00002173 case 'M':
2174 modprobe = optarg;
2175 break;
2176
2177 case 'c':
2178
2179 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2180 invert);
2181 pcnt = optarg;
2182 if (optind < argc && argv[optind][0] != '-'
2183 && argv[optind][0] != '!')
2184 bcnt = argv[optind++];
2185 else
2186 exit_error(PARAMETER_PROBLEM,
2187 "-%c requires packet and byte counter",
2188 opt2char(OPT_COUNTERS));
2189
Martin Josefssona28d4952004-05-26 16:04:48 +00002190 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002191 exit_error(PARAMETER_PROBLEM,
2192 "-%c packet counter not numeric",
2193 opt2char(OPT_COUNTERS));
2194
Martin Josefssona28d4952004-05-26 16:04:48 +00002195 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002196 exit_error(PARAMETER_PROBLEM,
2197 "-%c byte counter not numeric",
2198 opt2char(OPT_COUNTERS));
2199
2200 break;
2201
2202
Rusty Russell5eed48a2000-06-02 20:12:24 +00002203 case 1: /* non option */
2204 if (optarg[0] == '!' && optarg[1] == '\0') {
2205 if (invert)
2206 exit_error(PARAMETER_PROBLEM,
2207 "multiple consecutive ! not"
2208 " allowed");
2209 invert = TRUE;
2210 optarg[0] = '\0';
2211 continue;
2212 }
2213 printf("Bad argument `%s'\n", optarg);
2214 exit_tryhelp(2);
2215
2216 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002217 if (!target
2218 || !(target->parse(c - target->option_offset,
2219 argv, invert,
2220 &target->tflags,
2221 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002222 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002223 if (matchp->completed)
2224 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002225 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002226 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002227 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002228 &fw,
2229 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002230 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002231 break;
2232 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002233 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002234
2235 /* If you listen carefully, you can
2236 actually hear this code suck. */
2237
2238 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002239 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002240 * parameter, that has not been parsed yet,
2241 * it's not an option of an explicitly loaded
2242 * match or a target. However, we support
2243 * implicit loading of the protocol match
2244 * extension. '-p tcp' means 'l4 proto 6' and
2245 * at the same time 'load tcp protocol match on
2246 * demand if we specify --dport'.
2247 *
2248 * To make this work, we need to make sure:
2249 * - the parameter has not been parsed by
2250 * a match (m above)
2251 * - a protocol has been specified
2252 * - the protocol extension has not been
2253 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002254 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002255 * - the protocol extension can be successively
2256 * loaded
2257 */
2258 if (m == NULL
2259 && protocol
2260 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002261 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002262 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002263 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002264 && (proto_used == 0))
2265 )
2266 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002267 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002268 /* Try loading protocol */
2269 size_t size;
2270
2271 proto_used = 1;
2272
2273 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2274 + m->size;
2275
2276 m->m = fw_calloc(1, size);
2277 m->m->u.match_size = size;
2278 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002279 set_revision(m->m->u.user.name,
2280 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002281 if (m->init != NULL)
2282 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002283
2284 opts = merge_options(opts,
2285 m->extra_opts, &m->option_offset);
2286
2287 optind--;
2288 continue;
2289 }
2290
Rusty Russell5eed48a2000-06-02 20:12:24 +00002291 if (!m)
2292 exit_error(PARAMETER_PROBLEM,
2293 "Unknown arg `%s'",
2294 argv[optind-1]);
2295 }
2296 }
2297 invert = FALSE;
2298 }
2299
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002300 for (matchp = matches; matchp; matchp = matchp->next)
2301 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002302
Harald Welte43ac1912002-03-03 09:43:07 +00002303 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002304 target->final_check(target->tflags);
2305
2306 /* Fix me: must put inverse options checking here --MN */
2307
2308 if (optind < argc)
2309 exit_error(PARAMETER_PROBLEM,
2310 "unknown arguments found on commandline");
2311 if (!command)
2312 exit_error(PARAMETER_PROBLEM, "no command specified");
2313 if (invert)
2314 exit_error(PARAMETER_PROBLEM,
2315 "nothing appropriate following !");
2316
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002317 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002318 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002319 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002320 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002321 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002322 }
2323
2324 if (shostnetworkmask)
2325 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2326 &(fw.ipv6.smsk), &nsaddrs);
2327
2328 if (dhostnetworkmask)
2329 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2330 &(fw.ipv6.dmsk), &ndaddrs);
2331
2332 if ((nsaddrs > 1 || ndaddrs > 1) &&
2333 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2334 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2335 " source or destination IP addresses");
2336
Rusty Russell5eed48a2000-06-02 20:12:24 +00002337 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2338 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2339 "specify a unique address");
2340
2341 generic_opt_check(command, options);
2342
2343 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2344 exit_error(PARAMETER_PROBLEM,
2345 "chain name `%s' too long (must be under %i chars)",
2346 chain, IP6T_FUNCTION_MAXNAMELEN);
2347
Harald Welte43ac1912002-03-03 09:43:07 +00002348 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002349 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002350 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002351
Rusty Russell8beb0492004-12-22 00:37:10 +00002352 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00002353 if (!*handle && load_ip6tables_ko(modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002354 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002355
Harald Welte43ac1912002-03-03 09:43:07 +00002356 if (!*handle)
2357 exit_error(VERSION_PROBLEM,
2358 "can't initialize ip6tables table `%s': %s",
2359 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002360
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002361 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002362 || command == CMD_DELETE
2363 || command == CMD_INSERT
2364 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002365 if (strcmp(chain, "PREROUTING") == 0
2366 || strcmp(chain, "INPUT") == 0) {
2367 /* -o not valid with incoming packets. */
2368 if (options & OPT_VIANAMEOUT)
2369 exit_error(PARAMETER_PROBLEM,
2370 "Can't use -%c with %s\n",
2371 opt2char(OPT_VIANAMEOUT),
2372 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002373 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002374
Harald Welte43ac1912002-03-03 09:43:07 +00002375 if (strcmp(chain, "POSTROUTING") == 0
2376 || strcmp(chain, "OUTPUT") == 0) {
2377 /* -i not valid with outgoing packets */
2378 if (options & OPT_VIANAMEIN)
2379 exit_error(PARAMETER_PROBLEM,
2380 "Can't use -%c with %s\n",
2381 opt2char(OPT_VIANAMEIN),
2382 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002383 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002384
2385 if (target && ip6tc_is_chain(jumpto, *handle)) {
2386 printf("Warning: using chain %s, not extension\n",
2387 jumpto);
2388
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002389 if (target->t)
2390 free(target->t);
2391
Rusty Russell5eed48a2000-06-02 20:12:24 +00002392 target = NULL;
2393 }
2394
2395 /* If they didn't specify a target, or it's a chain
2396 name, use standard. */
2397 if (!target
2398 && (strlen(jumpto) == 0
2399 || ip6tc_is_chain(jumpto, *handle))) {
2400 size_t size;
2401
2402 target = find_target(IP6T_STANDARD_TARGET,
2403 LOAD_MUST_SUCCEED);
2404
2405 size = sizeof(struct ip6t_entry_target)
2406 + target->size;
2407 target->t = fw_calloc(1, size);
2408 target->t->u.target_size = size;
2409 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002410 if (target->init != NULL)
2411 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002412 }
2413
2414 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002415 /* it is no chain, and we can't load a plugin.
2416 * We cannot know if the plugin is corrupt, non
2417 * existant OR if the user just misspelled a
2418 * chain. */
2419 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002420 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002421 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002422 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002423 }
2424 }
2425
2426 switch (command) {
2427 case CMD_APPEND:
2428 ret = append_entry(chain, e,
2429 nsaddrs, saddrs, ndaddrs, daddrs,
2430 options&OPT_VERBOSE,
2431 handle);
2432 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002433 case CMD_DELETE:
2434 ret = delete_entry(chain, e,
2435 nsaddrs, saddrs, ndaddrs, daddrs,
2436 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002437 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002438 break;
2439 case CMD_DELETE_NUM:
2440 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2441 break;
2442 case CMD_REPLACE:
2443 ret = replace_entry(chain, e, rulenum - 1,
2444 saddrs, daddrs, options&OPT_VERBOSE,
2445 handle);
2446 break;
2447 case CMD_INSERT:
2448 ret = insert_entry(chain, e, rulenum - 1,
2449 nsaddrs, saddrs, ndaddrs, daddrs,
2450 options&OPT_VERBOSE,
2451 handle);
2452 break;
2453 case CMD_LIST:
2454 ret = list_entries(chain,
2455 options&OPT_VERBOSE,
2456 options&OPT_NUMERIC,
2457 options&OPT_EXPANDED,
2458 options&OPT_LINENUMBERS,
2459 handle);
2460 break;
2461 case CMD_FLUSH:
2462 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2463 break;
2464 case CMD_ZERO:
2465 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2466 break;
2467 case CMD_LIST|CMD_ZERO:
2468 ret = list_entries(chain,
2469 options&OPT_VERBOSE,
2470 options&OPT_NUMERIC,
2471 options&OPT_EXPANDED,
2472 options&OPT_LINENUMBERS,
2473 handle);
2474 if (ret)
2475 ret = zero_entries(chain,
2476 options&OPT_VERBOSE, handle);
2477 break;
2478 case CMD_NEW_CHAIN:
2479 ret = ip6tc_create_chain(chain, handle);
2480 break;
2481 case CMD_DELETE_CHAIN:
2482 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2483 break;
2484 case CMD_RENAME_CHAIN:
2485 ret = ip6tc_rename_chain(chain, newname, handle);
2486 break;
2487 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002488 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002489 break;
2490 default:
2491 /* We should never reach this... */
2492 exit_tryhelp(2);
2493 }
2494
2495 if (verbose > 1)
2496 dump_entries6(*handle);
2497
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002498 clear_rule_matches(&matches);
2499
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002500 if (e != NULL) {
2501 free(e);
2502 e = NULL;
2503 }
2504
2505 for (c = 0; c < nsaddrs; c++)
2506 free(&saddrs[c]);
2507
2508 for (c = 0; c < ndaddrs; c++)
2509 free(&daddrs[c]);
2510
Pablo Neiradfdcd642005-05-29 19:05:23 +00002511 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002512
Rusty Russell5eed48a2000-06-02 20:12:24 +00002513 return ret;
2514}