blob: 9b1370a455086acb30cc2dcfe0662667d8f0a131 [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 },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000226 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000227 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000228 { "esp", IPPROTO_ESP },
229 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000230};
231
232static char *
233proto_to_name(u_int8_t proto, int nolookup)
234{
235 unsigned int i;
236
237 if (proto && !nolookup) {
238 struct protoent *pent = getprotobynumber(proto);
239 if (pent)
240 return pent->p_name;
241 }
242
243 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
244 if (chain_protos[i].num == proto)
245 return chain_protos[i].name;
246
247 return NULL;
248}
249
Phil Oester58179b12006-07-20 17:00:19 +0000250int
251service_to_port(const char *name, const char *proto)
252{
253 struct servent *service;
254
255 if ((service = getservbyname(name, proto)) != NULL)
256 return ntohs((unsigned short) service->s_port);
257
258 return -1;
259}
260
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000261u_int16_t
262parse_port(const char *port, const char *proto)
263{
264 unsigned int portnum;
265
266 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
267 (portnum = service_to_port(port, proto)) != -1)
268 return (u_int16_t)portnum;
269
270 exit_error(PARAMETER_PROBLEM,
271 "invalid port/service `%s' specified", port);
272}
273
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274static void
275in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
276{
277 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000278 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000279}
280
Pablo Neiradfdcd642005-05-29 19:05:23 +0000281static void free_opts(int reset_offset)
282{
283 if (opts != original_opts) {
284 free(opts);
285 opts = original_opts;
286 if (reset_offset)
287 global_option_offset = 0;
288 }
289}
290
Rusty Russell5eed48a2000-06-02 20:12:24 +0000291void
292exit_error(enum exittype status, char *msg, ...)
293{
294 va_list args;
295
296 va_start(args, msg);
297 fprintf(stderr, "%s v%s: ", program_name, program_version);
298 vfprintf(stderr, msg, args);
299 va_end(args);
300 fprintf(stderr, "\n");
301 if (status == PARAMETER_PROBLEM)
302 exit_tryhelp(status);
303 if (status == VERSION_PROBLEM)
304 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000305 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000306 /* On error paths, make sure that we don't leak memory */
307 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000308 exit(status);
309}
310
311void
312exit_tryhelp(int status)
313{
Harald Weltea8658ca2003-03-05 07:46:15 +0000314 if (line != -1)
315 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000316 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
317 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000318 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000319 exit(status);
320}
321
322void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000323exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000324{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000325 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000326 struct ip6tables_target *t = NULL;
327
328 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000329"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000330" %s -[RI] chain rulenum rule-specification [options]\n"
331" %s -D chain rulenum [options]\n"
332" %s -[LFZ] [chain] [options]\n"
333" %s -[NX] chain\n"
334" %s -E old-chain-name new-chain-name\n"
335" %s -P chain target [options]\n"
336" %s -h (print this help information)\n\n",
337 program_name, program_version, program_name, program_name,
338 program_name, program_name, program_name, program_name,
339 program_name, program_name);
340
341 printf(
342"Commands:\n"
343"Either long or short options are allowed.\n"
344" --append -A chain Append to chain\n"
345" --delete -D chain Delete matching rule from chain\n"
346" --delete -D chain rulenum\n"
347" Delete rule rulenum (1 = first) from chain\n"
348" --insert -I chain [rulenum]\n"
349" Insert in chain as rulenum (default 1=first)\n"
350" --replace -R chain rulenum\n"
351" Replace rule rulenum (1 = first) in chain\n"
352" --list -L [chain] List the rules in a chain or all chains\n"
353" --flush -F [chain] Delete all rules in chain or all chains\n"
354" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000355" --new -N chain Create a new user-defined chain\n"
356" --delete-chain\n"
357" -X [chain] Delete a user-defined chain\n"
358" --policy -P chain target\n"
359" Change policy on chain to target\n"
360" --rename-chain\n"
361" -E old-chain new-chain\n"
362" Change chain name, (moving any references)\n"
363
364"Options:\n"
365" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
366" --source -s [!] address[/mask]\n"
367" source specification\n"
368" --destination -d [!] address[/mask]\n"
369" destination specification\n"
370" --in-interface -i [!] input name[+]\n"
371" network interface name ([+] for wildcard)\n"
372" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000373" target for rule (may load target extension)\n"
374" --match -m match\n"
375" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000376" --numeric -n numeric output of addresses and ports\n"
377" --out-interface -o [!] output name[+]\n"
378" network interface name ([+] for wildcard)\n"
379" --table -t table table to manipulate (default: `filter')\n"
380" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000381" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000382" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000383/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000384" --modprobe=<command> try to insert modules using this command\n"
385" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000386"[!] --version -V print package version.\n");
387
388 /* Print out any special helps. A user might like to be able to add a --help
389 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000390 specified matches & targets */
391 for (t = ip6tables_targets; t; t = t->next) {
392 if (t->used) {
393 printf("\n");
394 t->help();
395 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000397 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000398 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000399 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000400 }
401 exit(0);
402}
403
404static void
405generic_opt_check(int command, int options)
406{
407 int i, j, legal = 0;
408
409 /* Check that commands are valid with options. Complicated by the
410 * fact that if an option is legal with *any* command given, it is
411 * legal overall (ie. -z and -l).
412 */
413 for (i = 0; i < NUMBER_OF_OPT; i++) {
414 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
415
416 for (j = 0; j < NUMBER_OF_CMD; j++) {
417 if (!(command & (1<<j)))
418 continue;
419
420 if (!(options & (1<<i))) {
421 if (commands_v_options[j][i] == '+')
422 exit_error(PARAMETER_PROBLEM,
423 "You need to supply the `-%c' "
424 "option for this command\n",
425 optflags[i]);
426 } else {
427 if (commands_v_options[j][i] != 'x')
428 legal = 1;
429 else if (legal == 0)
430 legal = -1;
431 }
432 }
433 if (legal == -1)
434 exit_error(PARAMETER_PROBLEM,
435 "Illegal option `-%c' with this command\n",
436 optflags[i]);
437 }
438}
439
440static char
441opt2char(int option)
442{
443 const char *ptr;
444 for (ptr = optflags; option > 1; option >>= 1, ptr++);
445
446 return *ptr;
447}
448
449static char
450cmd2char(int option)
451{
452 const char *ptr;
453 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
454
455 return *ptr;
456}
457
458static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000459add_command(unsigned int *cmd, const int newcmd, const int othercmds,
460 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000461{
462 if (invert)
463 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
464 if (*cmd & (~othercmds))
465 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
466 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
467 *cmd |= newcmd;
468}
469
470int
Harald Welteb77f1da2002-03-14 11:35:58 +0000471check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472{
473 if (option && strcmp(option, "!") == 0) {
474 if (*invert)
475 exit_error(PARAMETER_PROBLEM,
476 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000478 if (optind) {
479 *optind = *optind+1;
480 if (argc && *optind > argc)
481 exit_error(PARAMETER_PROBLEM,
482 "no argument following `!'");
483 }
484
Rusty Russell5eed48a2000-06-02 20:12:24 +0000485 return TRUE;
486 }
487 return FALSE;
488}
489
490static void *
491fw_calloc(size_t count, size_t size)
492{
493 void *p;
494
495 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000496 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000497 exit(1);
498 }
499 return p;
500}
501
502static void *
503fw_malloc(size_t size)
504{
505 void *p;
506
507 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000508 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000509 exit(1);
510 }
511 return p;
512}
513
Rusty Russell5eed48a2000-06-02 20:12:24 +0000514static char *
515addr_to_numeric(const struct in6_addr *addrp)
516{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000517 /* 0000:0000:0000:0000:0000:000.000.000.000
518 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000519 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000520 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000521}
522
523static struct in6_addr *
524numeric_to_addr(const char *num)
525{
526 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000527 int err;
528 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000529 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000530#ifdef DEBUG
531 fprintf(stderr, "\nnumeric2addr: %d\n", err);
532#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000533 return (struct in6_addr *)NULL;
534}
535
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000536
537static struct in6_addr *
538host_to_addr(const char *name, unsigned int *naddr)
539{
540 struct addrinfo hints;
541 struct addrinfo *res;
542 static struct in6_addr *addr;
543 int err;
544
545 memset(&hints, 0, sizeof(hints));
546 hints.ai_flags=AI_CANONNAME;
547 hints.ai_family=AF_INET6;
548 hints.ai_socktype=SOCK_RAW;
549 hints.ai_protocol=41;
550 hints.ai_next=NULL;
551
552 *naddr = 0;
553 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
554#ifdef DEBUG
555 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
556#endif
557 return (struct in6_addr *) NULL;
558 } else {
559 if (res->ai_family != AF_INET6 ||
560 res->ai_addrlen != sizeof(struct sockaddr_in6))
561 return (struct in6_addr *) NULL;
562
563#ifdef DEBUG
564 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
565 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
566#endif
567 /* Get the first element of the address-chain */
568 addr = fw_calloc(1, sizeof(struct in6_addr));
569 in6addrcpy(addr, (struct in6_addr *)
570 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
571 freeaddrinfo(res);
572 *naddr = 1;
573 return addr;
574 }
575
576 return (struct in6_addr *) NULL;
577}
578
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000579static char *
580addr_to_host(const struct in6_addr *addr)
581{
582 struct sockaddr_in6 saddr;
583 int err;
584 static char hostname[NI_MAXHOST];
585
586 memset(&saddr, 0, sizeof(struct sockaddr_in6));
587 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000588 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000589
590 if ( (err=getnameinfo((struct sockaddr *)&saddr,
591 sizeof(struct sockaddr_in6),
592 hostname, sizeof(hostname)-1,
593 NULL, 0, 0)) != 0 ){
594#ifdef DEBUG
595 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
596#endif
597 return (char *) NULL;
598 } else {
599#ifdef DEBUG
600 fprintf (stderr, "\naddr2host: %s\n", hostname);
601#endif
602
603 return hostname;
604 }
605
606 return (char *) NULL;
607}
608
Rusty Russell5eed48a2000-06-02 20:12:24 +0000609static char *
610mask_to_numeric(const struct in6_addr *addrp)
611{
Harald Welte8a50ab82003-06-24 18:15:59 +0000612 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000613 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000614 if (l == -1) {
615 strcpy(buf, "/");
616 strcat(buf, addr_to_numeric(addrp));
617 return buf;
618 }
Harald Welte43ac1912002-03-03 09:43:07 +0000619 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000620 return buf;
621}
622
623static struct in6_addr *
624network_to_addr(const char *name)
625{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000626 /* abort();*/
627 /* TODO: not implemented yet, but the exception breaks the
628 * name resolvation */
629 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000630}
631
632static char *
633addr_to_anyname(const struct in6_addr *addr)
634{
635 char *name;
636
637 if ((name = addr_to_host(addr)) != NULL)
638 return name;
639
640 return addr_to_numeric(addr);
641}
642
643/*
644 * All functions starting with "parse" should succeed, otherwise
645 * the program fails.
646 * Most routines return pointers to static data that may change
647 * between calls to the same or other routines with a few exceptions:
648 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
649 * return global static data.
650*/
651
652static struct in6_addr *
653parse_hostnetwork(const char *name, unsigned int *naddrs)
654{
655 struct in6_addr *addrp, *addrptmp;
656
657 if ((addrptmp = numeric_to_addr(name)) != NULL ||
658 (addrptmp = network_to_addr(name)) != NULL) {
659 addrp = fw_malloc(sizeof(struct in6_addr));
660 in6addrcpy(addrp, addrptmp);
661 *naddrs = 1;
662 return addrp;
663 }
664 if ((addrp = host_to_addr(name, naddrs)) != NULL)
665 return addrp;
666
667 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
668}
669
670static struct in6_addr *
671parse_mask(char *mask)
672{
673 static struct in6_addr maskaddr;
674 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000675 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000676
677 if (mask == NULL) {
678 /* no mask at all defaults to 128 bits */
679 memset(&maskaddr, 0xff, sizeof maskaddr);
680 return &maskaddr;
681 }
682 if ((addrp = numeric_to_addr(mask)) != NULL)
683 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000684 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000685 exit_error(PARAMETER_PROBLEM,
686 "invalid mask `%s' specified", mask);
687 if (bits != 0) {
688 char *p = (char *)&maskaddr;
689 memset(p, 0xff, bits / 8);
690 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
691 p[bits / 8] = 0xff << (8 - (bits & 7));
692 return &maskaddr;
693 }
694
695 memset(&maskaddr, 0, sizeof maskaddr);
696 return &maskaddr;
697}
698
Harald Welte43ac1912002-03-03 09:43:07 +0000699void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
701 struct in6_addr *maskp, unsigned int *naddrs)
702{
703 struct in6_addr *addrp;
704 char buf[256];
705 char *p;
706 int i, j, n;
707
708 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000709 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000710 if ((p = strrchr(buf, '/')) != NULL) {
711 *p = '\0';
712 addrp = parse_mask(p + 1);
713 } else
714 addrp = parse_mask(NULL);
715 in6addrcpy(maskp, addrp);
716
717 /* if a null mask is given, the name is ignored, like in "any/0" */
718 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
719 strcpy(buf, "::");
720
721 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
722 n = *naddrs;
723 for (i = 0, j = 0; i < n; i++) {
724 int k;
725 for (k = 0; k < 4; k++)
726 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
727 j++;
728 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000729 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730 (*naddrs)--;
731 j--;
732 break;
733 }
734 }
735 }
736}
737
738struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000739find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000740{
741 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000742 const char *icmp6 = "icmp6";
743 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000744
745 /* This is ugly as hell. Nonetheless, there is no way of changing
746 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000747 if ( (strcmp(match_name,"icmpv6") == 0) ||
748 (strcmp(match_name,"ipv6-icmp") == 0) ||
749 (strcmp(match_name,"icmp6") == 0) )
750 name = icmp6;
751 else
752 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000753
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000754 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
755 if (strcmp(name, ptr->name) == 0) {
756 struct ip6tables_match *clone;
757
758 /* First match of this type: */
759 if (ptr->m == NULL)
760 break;
761
762 /* Second and subsequent clones */
763 clone = fw_malloc(sizeof(struct ip6tables_match));
764 memcpy(clone, ptr, sizeof(struct ip6tables_match));
765 clone->mflags = 0;
766 /* This is a clone: */
767 clone->next = clone;
768
769 ptr = clone;
770 break;
771 }
772 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000773
Harald Welte3efb6ea2001-08-06 18:50:21 +0000774#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000775 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000776 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000778 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000779 if (dlopen(path, RTLD_NOW)) {
780 /* Found library. If it didn't register itself,
781 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000782 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783
784 if (!ptr)
785 exit_error(PARAMETER_PROBLEM,
786 "Couldn't load match `%s'\n",
787 name);
788 } else if (tryload == LOAD_MUST_SUCCEED)
789 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000790 "Couldn't load match `%s':%s\n",
791 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000793#else
794 if (ptr && !ptr->loaded) {
795 if (tryload != DONT_LOAD)
796 ptr->loaded = 1;
797 else
798 ptr = NULL;
799 }
Marc Boucher067477b2002-03-24 15:09:31 +0000800 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
801 exit_error(PARAMETER_PROBLEM,
802 "Couldn't find match `%s'\n", name);
803 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000804#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000806 if (ptr && matches) {
807 struct ip6tables_rule_match **i;
808 struct ip6tables_rule_match *newentry;
809
810 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
811
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000812 for (i = matches; *i; i = &(*i)->next) {
813 if (strcmp(name, (*i)->match->name) == 0)
814 (*i)->completed = 1;
815 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000816 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000817 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000818 newentry->next = NULL;
819 *i = newentry;
820 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000821
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 return ptr;
823}
824
825/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
826static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000827find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828{
Harald Welteed498492001-07-23 01:24:22 +0000829 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830
Harald Welte43ac1912002-03-03 09:43:07 +0000831 if (string_to_number(pname, 0, 255, &proto) != -1) {
832 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000833
Harald Welte43ac1912002-03-03 09:43:07 +0000834 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000835 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000836 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000837 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000838
839 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840}
841
Harald Welte43ac1912002-03-03 09:43:07 +0000842u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000843parse_protocol(const char *s)
844{
Harald Welteed498492001-07-23 01:24:22 +0000845 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846
Harald Welteed498492001-07-23 01:24:22 +0000847 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000848 struct protoent *pent;
849
Harald Weltecbe1ec72006-02-11 09:50:11 +0000850 /* first deal with the special case of 'all' to prevent
851 * people from being able to redefine 'all' in nsswitch
852 * and/or provoke expensive [not working] ldap/nis/...
853 * lookups */
854 if (!strcmp(s, "all"))
855 return 0;
856
Rusty Russell5eed48a2000-06-02 20:12:24 +0000857 if ((pent = getprotobyname(s)))
858 proto = pent->p_proto;
859 else {
860 unsigned int i;
861 for (i = 0;
862 i < sizeof(chain_protos)/sizeof(struct pprot);
863 i++) {
864 if (strcmp(s, chain_protos[i].name) == 0) {
865 proto = chain_protos[i].num;
866 break;
867 }
868 }
869 if (i == sizeof(chain_protos)/sizeof(struct pprot))
870 exit_error(PARAMETER_PROBLEM,
871 "unknown protocol `%s' specified",
872 s);
873 }
874 }
875
876 return (u_int16_t)proto;
877}
878
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000879/* proto means IPv6 extension header ? */
880static int is_exthdr(u_int16_t proto)
881{
882 return (proto == IPPROTO_HOPOPTS ||
883 proto == IPPROTO_ROUTING ||
884 proto == IPPROTO_FRAGMENT ||
885 proto == IPPROTO_ESP ||
886 proto == IPPROTO_AH ||
887 proto == IPPROTO_DSTOPTS);
888}
889
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000890void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000891{
892 int vialen = strlen(arg);
893 unsigned int i;
894
895 memset(mask, 0, IFNAMSIZ);
896 memset(vianame, 0, IFNAMSIZ);
897
898 if (vialen + 1 > IFNAMSIZ)
899 exit_error(PARAMETER_PROBLEM,
900 "interface name `%s' must be shorter than IFNAMSIZ"
901 " (%i)", arg, IFNAMSIZ-1);
902
903 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000904 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000905 memset(mask, 0, IFNAMSIZ);
906 else if (vianame[vialen - 1] == '+') {
907 memset(mask, 0xFF, vialen - 1);
908 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000909 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000910 } else {
911 /* Include nul-terminator in match */
912 memset(mask, 0xFF, vialen + 1);
913 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000914 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000915 if (vianame[i] == ':' ||
916 vianame[i] == '!' ||
917 vianame[i] == '*') {
918 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000919 " `%s' (No aliases, :, ! or *).\n",
920 vianame);
921 break;
922 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000923 }
924 }
925}
926
927/* Can't be zero. */
928static int
929parse_rulenumber(const char *rule)
930{
Harald Welte43ac1912002-03-03 09:43:07 +0000931 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000932
Harald Welte43ac1912002-03-03 09:43:07 +0000933 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000934 exit_error(PARAMETER_PROBLEM,
935 "Invalid rule number `%s'", rule);
936
937 return rulenum;
938}
939
940static const char *
941parse_target(const char *targetname)
942{
943 const char *ptr;
944
945 if (strlen(targetname) < 1)
946 exit_error(PARAMETER_PROBLEM,
947 "Invalid target name (too short)");
948
949 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
950 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000951 "Invalid target name `%s' (%u chars max)",
952 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000953
954 for (ptr = targetname; *ptr; ptr++)
955 if (isspace(*ptr))
956 exit_error(PARAMETER_PROBLEM,
957 "Invalid target name `%s'", targetname);
958 return targetname;
959}
960
961int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000962string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
963 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000965 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000966 char *end;
967
968 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000969 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000970 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000971 if (*end == '\0' && end != s) {
972 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000973 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000974 *ret = number;
975 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000976 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977 }
978 return -1;
979}
980
Martin Josefssonb105bc92004-05-26 15:54:49 +0000981int
982string_to_number_l(const char *s, unsigned long min, unsigned long max,
983 unsigned long *ret)
984{
985 int result;
986 unsigned long long number;
987
988 result = string_to_number_ll(s, min, max, &number);
989 *ret = (unsigned long)number;
990
991 return result;
992}
993
994int string_to_number(const char *s, unsigned int min, unsigned int max,
995 unsigned int *ret)
996{
997 int result;
998 unsigned long number;
999
1000 result = string_to_number_l(s, min, max, &number);
1001 *ret = (unsigned int)number;
1002
1003 return result;
1004}
1005
Rusty Russell5eed48a2000-06-02 20:12:24 +00001006static void
1007set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1008 int invert)
1009{
1010 if (*options & option)
1011 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1012 opt2char(option));
1013 *options |= option;
1014
1015 if (invert) {
1016 unsigned int i;
1017 for (i = 0; 1 << i != option; i++);
1018
1019 if (!inverse_for_options[i])
1020 exit_error(PARAMETER_PROBLEM,
1021 "cannot have ! before -%c",
1022 opt2char(option));
1023 *invflg |= inverse_for_options[i];
1024 }
1025}
1026
1027struct ip6tables_target *
1028find_target(const char *name, enum ip6t_tryload tryload)
1029{
1030 struct ip6tables_target *ptr;
1031
1032 /* Standard target? */
1033 if (strcmp(name, "") == 0
1034 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1035 || strcmp(name, IP6TC_LABEL_DROP) == 0
1036 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1037 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1038 name = "standard";
1039
1040 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1041 if (strcmp(name, ptr->name) == 0)
1042 break;
1043 }
1044
Harald Welte3efb6ea2001-08-06 18:50:21 +00001045#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001046 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001047 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001049 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001050 if (dlopen(path, RTLD_NOW)) {
1051 /* Found library. If it didn't register itself,
1052 maybe they specified match as a target. */
1053 ptr = find_target(name, DONT_LOAD);
1054 if (!ptr)
1055 exit_error(PARAMETER_PROBLEM,
1056 "Couldn't load target `%s'\n",
1057 name);
1058 } else if (tryload == LOAD_MUST_SUCCEED)
1059 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001060 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001061 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001062 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001063#else
1064 if (ptr && !ptr->loaded) {
1065 if (tryload != DONT_LOAD)
1066 ptr->loaded = 1;
1067 else
1068 ptr = NULL;
1069 }
Marc Boucher067477b2002-03-24 15:09:31 +00001070 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1071 exit_error(PARAMETER_PROBLEM,
1072 "Couldn't find target `%s'\n", name);
1073 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001074#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001075
Harald Welte43ac1912002-03-03 09:43:07 +00001076 if (ptr)
1077 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001078
Rusty Russell5eed48a2000-06-02 20:12:24 +00001079 return ptr;
1080}
1081
1082static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001083merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001084 unsigned int *option_offset)
1085{
1086 unsigned int num_old, num_new, i;
1087 struct option *merge;
1088
1089 for (num_old = 0; oldopts[num_old].name; num_old++);
1090 for (num_new = 0; newopts[num_new].name; num_new++);
1091
1092 global_option_offset += OPTION_OFFSET;
1093 *option_offset = global_option_offset;
1094
1095 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1096 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001097 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001098 for (i = 0; i < num_new; i++) {
1099 merge[num_old + i] = newopts[i];
1100 merge[num_old + i].val += *option_offset;
1101 }
1102 memset(merge + num_old + num_new, 0, sizeof(struct option));
1103
1104 return merge;
1105}
1106
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001107static int compatible_revision(const char *name, u_int8_t revision, int opt)
1108{
1109 struct ip6t_get_revision rev;
1110 socklen_t s = sizeof(rev);
1111 int max_rev, sockfd;
1112
1113 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1114 if (sockfd < 0) {
1115 fprintf(stderr, "Could not open socket to kernel: %s\n",
1116 strerror(errno));
1117 exit(1);
1118 }
1119
1120 strcpy(rev.name, name);
1121 rev.revision = revision;
1122
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001123 load_ip6tables_ko(modprobe);
1124
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001125 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1126 if (max_rev < 0) {
1127 /* Definitely don't support this? */
1128 if (errno == EPROTONOSUPPORT) {
1129 close(sockfd);
1130 return 0;
1131 } else if (errno == ENOPROTOOPT) {
1132 close(sockfd);
1133 /* Assume only revision 0 support (old kernel) */
1134 return (revision == 0);
1135 } else {
1136 fprintf(stderr, "getsockopt failed strangely: %s\n",
1137 strerror(errno));
1138 exit(1);
1139 }
1140 }
1141 close(sockfd);
1142 return 1;
1143}
1144
1145static int compatible_match_revision(const char *name, u_int8_t revision)
1146{
1147 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1148}
1149
Rusty Russell5eed48a2000-06-02 20:12:24 +00001150void
1151register_match6(struct ip6tables_match *me)
1152{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001153 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001154
Rusty Russell5eed48a2000-06-02 20:12:24 +00001155 if (strcmp(me->version, program_version) != 0) {
1156 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1157 program_name, me->name, me->version, program_version);
1158 exit(1);
1159 }
1160
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001161 /* Revision field stole a char from name. */
1162 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1163 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001164 program_name, me->name);
1165 exit(1);
1166 }
1167
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001168 old = find_match(me->name, DURING_LOAD, NULL);
1169 if (old) {
1170 if (old->revision == me->revision) {
1171 fprintf(stderr,
1172 "%s: match `%s' already registered.\n",
1173 program_name, me->name);
1174 exit(1);
1175 }
1176
1177 /* Now we have two (or more) options, check compatibility. */
1178 if (compatible_match_revision(old->name, old->revision)
1179 && old->revision > me->revision)
1180 return;
1181
1182 /* Replace if compatible. */
1183 if (!compatible_match_revision(me->name, me->revision))
1184 return;
1185
1186 /* Delete old one. */
1187 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1188 *i = old->next;
1189 }
1190
Harald Welte43ac1912002-03-03 09:43:07 +00001191 if (me->size != IP6T_ALIGN(me->size)) {
1192 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001193 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001194 exit(1);
1195 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001196
Harald Welte43ac1912002-03-03 09:43:07 +00001197 /* Append to list. */
1198 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1199 me->next = NULL;
1200 *i = me;
1201
Rusty Russell5eed48a2000-06-02 20:12:24 +00001202 me->m = NULL;
1203 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001204}
1205
1206void
1207register_target6(struct ip6tables_target *me)
1208{
1209 if (strcmp(me->version, program_version) != 0) {
1210 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1211 program_name, me->name, me->version, program_version);
1212 exit(1);
1213 }
1214
Jones Desougif5b86e62005-12-22 03:33:50 +00001215 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001216 fprintf(stderr, "%s: target `%s' already registered.\n",
1217 program_name, me->name);
1218 exit(1);
1219 }
1220
Harald Welte43ac1912002-03-03 09:43:07 +00001221 if (me->size != IP6T_ALIGN(me->size)) {
1222 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001223 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001224 exit(1);
1225 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001226
Rusty Russell5eed48a2000-06-02 20:12:24 +00001227 /* Prepend to list. */
1228 me->next = ip6tables_targets;
1229 ip6tables_targets = me;
1230 me->t = NULL;
1231 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001232}
1233
1234static void
1235print_num(u_int64_t number, unsigned int format)
1236{
Harald Welte43ac1912002-03-03 09:43:07 +00001237 if (format & FMT_KILOMEGAGIGA) {
1238 if (number > 99999) {
1239 number = (number + 500) / 1000;
1240 if (number > 9999) {
1241 number = (number + 500) / 1000;
1242 if (number > 9999) {
1243 number = (number + 500) / 1000;
1244 if (number > 9999) {
1245 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001246 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001247 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001248 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001249 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001250 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001251 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001252 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001253 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001254 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001255 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001256 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001257}
1258
Harald Welte43ac1912002-03-03 09:43:07 +00001259
Rusty Russell5eed48a2000-06-02 20:12:24 +00001260static void
1261print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1262{
1263 struct ip6t_counters counters;
1264 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1265 printf("Chain %s", chain);
1266 if (pol) {
1267 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001268 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001269 fputc(' ', stdout);
1270 print_num(counters.pcnt, (format|FMT_NOTABLE));
1271 fputs("packets, ", stdout);
1272 print_num(counters.bcnt, (format|FMT_NOTABLE));
1273 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001274 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001275 printf(")\n");
1276 } else {
1277 unsigned int refs;
1278 if (!ip6tc_get_references(&refs, chain, handle))
1279 printf(" (ERROR obtaining refs)\n");
1280 else
1281 printf(" (%u references)\n", refs);
1282 }
1283
1284 if (format & FMT_LINENUMBERS)
1285 printf(FMT("%-4s ", "%s "), "num");
1286 if (!(format & FMT_NOCOUNTS)) {
1287 if (format & FMT_KILOMEGAGIGA) {
1288 printf(FMT("%5s ","%s "), "pkts");
1289 printf(FMT("%5s ","%s "), "bytes");
1290 } else {
1291 printf(FMT("%8s ","%s "), "pkts");
1292 printf(FMT("%10s ","%s "), "bytes");
1293 }
1294 }
1295 if (!(format & FMT_NOTARGET))
1296 printf(FMT("%-9s ","%s "), "target");
1297 fputs(" prot ", stdout);
1298 if (format & FMT_OPTIONS)
1299 fputs("opt", stdout);
1300 if (format & FMT_VIA) {
1301 printf(FMT(" %-6s ","%s "), "in");
1302 printf(FMT("%-6s ","%s "), "out");
1303 }
1304 printf(FMT(" %-19s ","%s "), "source");
1305 printf(FMT(" %-19s "," %s "), "destination");
1306 printf("\n");
1307}
1308
Harald Welte43ac1912002-03-03 09:43:07 +00001309
Rusty Russell5eed48a2000-06-02 20:12:24 +00001310static int
1311print_match(const struct ip6t_entry_match *m,
1312 const struct ip6t_ip6 *ip,
1313 int numeric)
1314{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001315 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001316
1317 if (match) {
1318 if (match->print)
1319 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001320 else
1321 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001322 } else {
1323 if (m->u.user.name[0])
1324 printf("UNKNOWN match `%s' ", m->u.user.name);
1325 }
1326 /* Don't stop iterating. */
1327 return 0;
1328}
1329
1330/* e is called `fw' here for hysterical raisins */
1331static void
1332print_firewall(const struct ip6t_entry *fw,
1333 const char *targname,
1334 unsigned int num,
1335 unsigned int format,
1336 const ip6tc_handle_t handle)
1337{
1338 struct ip6tables_target *target = NULL;
1339 const struct ip6t_entry_target *t;
1340 u_int8_t flags;
1341 char buf[BUFSIZ];
1342
Rusty Russell5eed48a2000-06-02 20:12:24 +00001343 if (!ip6tc_is_chain(targname, handle))
1344 target = find_target(targname, TRY_LOAD);
1345 else
1346 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1347
1348 t = ip6t_get_target((struct ip6t_entry *)fw);
1349 flags = fw->ipv6.flags;
1350
1351 if (format & FMT_LINENUMBERS)
1352 printf(FMT("%-4u ", "%u "), num+1);
1353
1354 if (!(format & FMT_NOCOUNTS)) {
1355 print_num(fw->counters.pcnt, format);
1356 print_num(fw->counters.bcnt, format);
1357 }
1358
1359 if (!(format & FMT_NOTARGET))
1360 printf(FMT("%-9s ", "%s "), targname);
1361
1362 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1363 {
Harald Welte43ac1912002-03-03 09:43:07 +00001364 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001365 if (pname)
1366 printf(FMT("%-5s", "%s "), pname);
1367 else
1368 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1369 }
1370
1371 if (format & FMT_OPTIONS) {
1372 if (format & FMT_NOTABLE)
1373 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001374 fputc(' ', stdout); /* Invert flag of FRAG */
1375 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001376 fputc(' ', stdout);
1377 }
1378
1379 if (format & FMT_VIA) {
1380 char iface[IFNAMSIZ+2];
1381
1382 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1383 iface[0] = '!';
1384 iface[1] = '\0';
1385 }
1386 else iface[0] = '\0';
1387
1388 if (fw->ipv6.iniface[0] != '\0') {
1389 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001390 }
1391 else if (format & FMT_NUMERIC) strcat(iface, "*");
1392 else strcat(iface, "any");
1393 printf(FMT(" %-6s ","in %s "), iface);
1394
1395 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1396 iface[0] = '!';
1397 iface[1] = '\0';
1398 }
1399 else iface[0] = '\0';
1400
1401 if (fw->ipv6.outiface[0] != '\0') {
1402 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001403 }
1404 else if (format & FMT_NUMERIC) strcat(iface, "*");
1405 else strcat(iface, "any");
1406 printf(FMT("%-6s ","out %s "), iface);
1407 }
1408
1409 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1410 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1411 && !(format & FMT_NUMERIC))
1412 printf(FMT("%-19s ","%s "), "anywhere");
1413 else {
1414 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001415 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 else
Harald Welte43ac1912002-03-03 09:43:07 +00001417 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1419 printf(FMT("%-19s ","%s "), buf);
1420 }
1421
1422 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1423 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1424 && !(format & FMT_NUMERIC))
1425 printf(FMT("%-19s","-> %s"), "anywhere");
1426 else {
1427 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001428 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429 else
Harald Welte43ac1912002-03-03 09:43:07 +00001430 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1432 printf(FMT("%-19s","-> %s"), buf);
1433 }
1434
1435 if (format & FMT_NOTABLE)
1436 fputs(" ", stdout);
1437
1438 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1439
1440 if (target) {
1441 if (target->print)
1442 /* Print the target information. */
1443 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1444 } else if (t->u.target_size != sizeof(*t))
1445 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001446 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001447
1448 if (!(format & FMT_NONEWLINE))
1449 fputc('\n', stdout);
1450}
1451
1452static void
1453print_firewall_line(const struct ip6t_entry *fw,
1454 const ip6tc_handle_t h)
1455{
1456 struct ip6t_entry_target *t;
1457
1458 t = ip6t_get_target((struct ip6t_entry *)fw);
1459 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1460}
1461
1462static int
1463append_entry(const ip6t_chainlabel chain,
1464 struct ip6t_entry *fw,
1465 unsigned int nsaddrs,
1466 const struct in6_addr saddrs[],
1467 unsigned int ndaddrs,
1468 const struct in6_addr daddrs[],
1469 int verbose,
1470 ip6tc_handle_t *handle)
1471{
1472 unsigned int i, j;
1473 int ret = 1;
1474
1475 for (i = 0; i < nsaddrs; i++) {
1476 fw->ipv6.src = saddrs[i];
1477 for (j = 0; j < ndaddrs; j++) {
1478 fw->ipv6.dst = daddrs[j];
1479 if (verbose)
1480 print_firewall_line(fw, *handle);
1481 ret &= ip6tc_append_entry(chain, fw, handle);
1482 }
1483 }
1484
1485 return ret;
1486}
1487
1488static int
1489replace_entry(const ip6t_chainlabel chain,
1490 struct ip6t_entry *fw,
1491 unsigned int rulenum,
1492 const struct in6_addr *saddr,
1493 const struct in6_addr *daddr,
1494 int verbose,
1495 ip6tc_handle_t *handle)
1496{
1497 fw->ipv6.src = *saddr;
1498 fw->ipv6.dst = *daddr;
1499
1500 if (verbose)
1501 print_firewall_line(fw, *handle);
1502 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1503}
1504
1505static int
1506insert_entry(const ip6t_chainlabel chain,
1507 struct ip6t_entry *fw,
1508 unsigned int rulenum,
1509 unsigned int nsaddrs,
1510 const struct in6_addr saddrs[],
1511 unsigned int ndaddrs,
1512 const struct in6_addr daddrs[],
1513 int verbose,
1514 ip6tc_handle_t *handle)
1515{
1516 unsigned int i, j;
1517 int ret = 1;
1518
1519 for (i = 0; i < nsaddrs; i++) {
1520 fw->ipv6.src = saddrs[i];
1521 for (j = 0; j < ndaddrs; j++) {
1522 fw->ipv6.dst = daddrs[j];
1523 if (verbose)
1524 print_firewall_line(fw, *handle);
1525 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1526 }
1527 }
1528
1529 return ret;
1530}
1531
1532static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001533make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534{
1535 /* Establish mask for comparison */
1536 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001537 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 unsigned char *mask, *mptr;
1539
1540 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001541 for (matchp = matches; matchp; matchp = matchp->next)
1542 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001543
1544 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001545 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546 + ip6tables_targets->size);
1547
1548 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1549 mptr = mask + sizeof(struct ip6t_entry);
1550
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001551 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001552 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001553 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001554 + matchp->match->userspacesize);
1555 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556 }
1557
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001558 memset(mptr, 0xFF,
1559 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1560 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561
1562 return mask;
1563}
1564
1565static int
1566delete_entry(const ip6t_chainlabel chain,
1567 struct ip6t_entry *fw,
1568 unsigned int nsaddrs,
1569 const struct in6_addr saddrs[],
1570 unsigned int ndaddrs,
1571 const struct in6_addr daddrs[],
1572 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001573 ip6tc_handle_t *handle,
1574 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001575{
1576 unsigned int i, j;
1577 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578 unsigned char *mask;
1579
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001580 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001582 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001583 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001584 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001585 if (verbose)
1586 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001587 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001588 }
1589 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001590 free(mask);
1591
Rusty Russell5eed48a2000-06-02 20:12:24 +00001592 return ret;
1593}
1594
András Kis-Szabó764316a2001-02-26 17:31:20 +00001595int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001596for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1597 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1598{
1599 int ret = 1;
1600 const char *chain;
1601 char *chains;
1602 unsigned int i, chaincount = 0;
1603
1604 chain = ip6tc_first_chain(handle);
1605 while (chain) {
1606 chaincount++;
1607 chain = ip6tc_next_chain(handle);
1608 }
1609
1610 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1611 i = 0;
1612 chain = ip6tc_first_chain(handle);
1613 while (chain) {
1614 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1615 i++;
1616 chain = ip6tc_next_chain(handle);
1617 }
1618
1619 for (i = 0; i < chaincount; i++) {
1620 if (!builtinstoo
1621 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001622 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623 continue;
1624 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1625 }
1626
1627 free(chains);
1628 return ret;
1629}
1630
András Kis-Szabó764316a2001-02-26 17:31:20 +00001631int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632flush_entries(const ip6t_chainlabel chain, int verbose,
1633 ip6tc_handle_t *handle)
1634{
1635 if (!chain)
1636 return for_each_chain(flush_entries, verbose, 1, handle);
1637
1638 if (verbose)
1639 fprintf(stdout, "Flushing chain `%s'\n", chain);
1640 return ip6tc_flush_entries(chain, handle);
1641}
1642
1643static int
1644zero_entries(const ip6t_chainlabel chain, int verbose,
1645 ip6tc_handle_t *handle)
1646{
1647 if (!chain)
1648 return for_each_chain(zero_entries, verbose, 1, handle);
1649
1650 if (verbose)
1651 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1652 return ip6tc_zero_entries(chain, handle);
1653}
1654
András Kis-Szabó764316a2001-02-26 17:31:20 +00001655int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001656delete_chain(const ip6t_chainlabel chain, int verbose,
1657 ip6tc_handle_t *handle)
1658{
1659 if (!chain)
1660 return for_each_chain(delete_chain, verbose, 0, handle);
1661
1662 if (verbose)
1663 fprintf(stdout, "Deleting chain `%s'\n", chain);
1664 return ip6tc_delete_chain(chain, handle);
1665}
1666
1667static int
1668list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1669 int expanded, int linenumbers, ip6tc_handle_t *handle)
1670{
1671 int found = 0;
1672 unsigned int format;
1673 const char *this;
1674
1675 format = FMT_OPTIONS;
1676 if (!verbose)
1677 format |= FMT_NOCOUNTS;
1678 else
1679 format |= FMT_VIA;
1680
1681 if (numeric)
1682 format |= FMT_NUMERIC;
1683
1684 if (!expanded)
1685 format |= FMT_KILOMEGAGIGA;
1686
1687 if (linenumbers)
1688 format |= FMT_LINENUMBERS;
1689
1690 for (this = ip6tc_first_chain(handle);
1691 this;
1692 this = ip6tc_next_chain(handle)) {
1693 const struct ip6t_entry *i;
1694 unsigned int num;
1695
1696 if (chain && strcmp(chain, this) != 0)
1697 continue;
1698
1699 if (found) printf("\n");
1700
1701 print_header(format, this, handle);
1702 i = ip6tc_first_rule(this, handle);
1703
1704 num = 0;
1705 while (i) {
1706 print_firewall(i,
1707 ip6tc_get_target(i, handle),
1708 num++,
1709 format,
1710 *handle);
1711 i = ip6tc_next_rule(i, handle);
1712 }
1713 found = 1;
1714 }
1715
1716 errno = ENOENT;
1717 return found;
1718}
1719
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001720static char *get_modprobe(void)
1721{
Harald Welte43ac1912002-03-03 09:43:07 +00001722 int procfile;
1723 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001724
Harald Welte10f7f142004-10-22 08:14:07 +00001725#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001726 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1727 if (procfile < 0)
1728 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001729
Harald Welte10f7f142004-10-22 08:14:07 +00001730 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001731 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001732 memset(ret, 0, PROCFILE_BUFSIZ);
1733 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001734 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001735 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001736 }
1737 if (ret[strlen(ret)-1]=='\n')
1738 ret[strlen(ret)-1]=0;
1739 close(procfile);
1740 return ret;
1741 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001742 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001743 free(ret);
1744 close(procfile);
1745 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001746}
1747
Harald Welte58918652001-06-16 18:25:25 +00001748int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001749{
Harald Welte43ac1912002-03-03 09:43:07 +00001750 char *buf = NULL;
1751 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001752 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001753
Harald Welte43ac1912002-03-03 09:43:07 +00001754 /* If they don't explicitly set it, read out of kernel */
1755 if (!modprobe) {
1756 buf = get_modprobe();
1757 if (!buf)
1758 return -1;
1759 modprobe = buf;
1760 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001761
Harald Welte43ac1912002-03-03 09:43:07 +00001762 switch (fork()) {
1763 case 0:
1764 argv[0] = (char *)modprobe;
1765 argv[1] = (char *)modname;
1766 argv[2] = NULL;
1767 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001768
Harald Welte43ac1912002-03-03 09:43:07 +00001769 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001770 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001771 case -1:
1772 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001773
Harald Welte43ac1912002-03-03 09:43:07 +00001774 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001775 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001776 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001777
Harald Welte43ac1912002-03-03 09:43:07 +00001778 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001779 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1780 return 0;
1781 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001782}
1783
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001784int load_ip6tables_ko(const char *modprobe)
1785{
1786 static int loaded = 0;
1787 static int ret = -1;
1788
1789 if (!loaded) {
1790 ret = ip6tables_insmod("ip6_tables", modprobe);
1791 loaded = 1;
1792 }
1793
1794 return ret;
1795}
1796
Rusty Russell5eed48a2000-06-02 20:12:24 +00001797static struct ip6t_entry *
1798generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001799 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001800 struct ip6t_entry_target *target)
1801{
1802 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001803 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001804 struct ip6t_entry *e;
1805
1806 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001807 for (matchp = matches; matchp; matchp = matchp->next)
1808 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001809
1810 e = fw_malloc(size + target->u.target_size);
1811 *e = *fw;
1812 e->target_offset = size;
1813 e->next_offset = size + target->u.target_size;
1814
1815 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001816 for (matchp = matches; matchp; matchp = matchp->next) {
1817 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1818 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001819 }
1820 memcpy(e->elems + size, target, target->u.target_size);
1821
1822 return e;
1823}
1824
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001825void clear_rule_matches(struct ip6tables_rule_match **matches)
1826{
1827 struct ip6tables_rule_match *matchp, *tmp;
1828
1829 for (matchp = *matches; matchp;) {
1830 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001831 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001832 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001833 matchp->match->m = NULL;
1834 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001835 if (matchp->match == matchp->match->next) {
1836 free(matchp->match);
1837 matchp->match = NULL;
1838 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001839 free(matchp);
1840 matchp = tmp;
1841 }
1842
1843 *matches = NULL;
1844}
1845
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001846static void set_revision(char *name, u_int8_t revision)
1847{
1848 /* Old kernel sources don't have ".revision" field,
1849 but we stole a byte from name. */
1850 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1851 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1852}
1853
Rusty Russell5eed48a2000-06-02 20:12:24 +00001854int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1855{
1856 struct ip6t_entry fw, *e = NULL;
1857 int invert = 0;
1858 unsigned int nsaddrs = 0, ndaddrs = 0;
1859 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1860
1861 int c, verbose = 0;
1862 const char *chain = NULL;
1863 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1864 const char *policy = NULL, *newname = NULL;
1865 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001866 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001867 int ret = 1;
1868 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001869 struct ip6tables_rule_match *matches = NULL;
1870 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001871 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001872 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 const char *jumpto = "";
1874 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001875 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001876
1877 memset(&fw, 0, sizeof(fw));
1878
Harald Welte43ac1912002-03-03 09:43:07 +00001879 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001880 * a second time */
1881 optind = 0;
1882
Harald Welte43ac1912002-03-03 09:43:07 +00001883 /* clear mflags in case do_command gets called a second time
1884 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001885 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001886 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001887
Harald Welte43ac1912002-03-03 09:43:07 +00001888 for (t = ip6tables_targets; t; t = t->next) {
1889 t->tflags = 0;
1890 t->used = 0;
1891 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001892
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893 /* Suppress error messages: we may add new options if we
1894 demand-load a protocol. */
1895 opterr = 0;
1896
1897 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001898 "-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 +00001899 opts, NULL)) != -1) {
1900 switch (c) {
1901 /*
1902 * Command selection
1903 */
1904 case 'A':
1905 add_command(&command, CMD_APPEND, CMD_NONE,
1906 invert);
1907 chain = optarg;
1908 break;
1909
1910 case 'D':
1911 add_command(&command, CMD_DELETE, CMD_NONE,
1912 invert);
1913 chain = optarg;
1914 if (optind < argc && argv[optind][0] != '-'
1915 && argv[optind][0] != '!') {
1916 rulenum = parse_rulenumber(argv[optind++]);
1917 command = CMD_DELETE_NUM;
1918 }
1919 break;
1920
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 case 'R':
1922 add_command(&command, CMD_REPLACE, CMD_NONE,
1923 invert);
1924 chain = optarg;
1925 if (optind < argc && argv[optind][0] != '-'
1926 && argv[optind][0] != '!')
1927 rulenum = parse_rulenumber(argv[optind++]);
1928 else
1929 exit_error(PARAMETER_PROBLEM,
1930 "-%c requires a rule number",
1931 cmd2char(CMD_REPLACE));
1932 break;
1933
1934 case 'I':
1935 add_command(&command, CMD_INSERT, CMD_NONE,
1936 invert);
1937 chain = optarg;
1938 if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 rulenum = parse_rulenumber(argv[optind++]);
1941 else rulenum = 1;
1942 break;
1943
1944 case 'L':
1945 add_command(&command, CMD_LIST, CMD_ZERO,
1946 invert);
1947 if (optarg) chain = optarg;
1948 else if (optind < argc && argv[optind][0] != '-'
1949 && argv[optind][0] != '!')
1950 chain = argv[optind++];
1951 break;
1952
1953 case 'F':
1954 add_command(&command, CMD_FLUSH, CMD_NONE,
1955 invert);
1956 if (optarg) chain = optarg;
1957 else if (optind < argc && argv[optind][0] != '-'
1958 && argv[optind][0] != '!')
1959 chain = argv[optind++];
1960 break;
1961
1962 case 'Z':
1963 add_command(&command, CMD_ZERO, CMD_LIST,
1964 invert);
1965 if (optarg) chain = optarg;
1966 else if (optind < argc && argv[optind][0] != '-'
1967 && argv[optind][0] != '!')
1968 chain = argv[optind++];
1969 break;
1970
1971 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001972 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001973 exit_error(PARAMETER_PROBLEM,
1974 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001975 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001976 if (find_target(optarg, TRY_LOAD))
1977 exit_error(PARAMETER_PROBLEM,
1978 "chain name may not clash "
1979 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001980 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1981 invert);
1982 chain = optarg;
1983 break;
1984
1985 case 'X':
1986 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1987 invert);
1988 if (optarg) chain = optarg;
1989 else if (optind < argc && argv[optind][0] != '-'
1990 && argv[optind][0] != '!')
1991 chain = argv[optind++];
1992 break;
1993
1994 case 'E':
1995 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1996 invert);
1997 chain = optarg;
1998 if (optind < argc && argv[optind][0] != '-'
1999 && argv[optind][0] != '!')
2000 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002001 else
2002 exit_error(PARAMETER_PROBLEM,
2003 "-%c requires old-chain-name and "
2004 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00002005 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002006 break;
2007
2008 case 'P':
2009 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2010 invert);
2011 chain = optarg;
2012 if (optind < argc && argv[optind][0] != '-'
2013 && argv[optind][0] != '!')
2014 policy = argv[optind++];
2015 else
2016 exit_error(PARAMETER_PROBLEM,
2017 "-%c requires a chain and a policy",
2018 cmd2char(CMD_SET_POLICY));
2019 break;
2020
2021 case 'h':
2022 if (!optarg)
2023 optarg = argv[optind];
2024
Jonas Berlin1b91e592005-04-01 06:58:38 +00002025 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002026 if (!matches && protocol)
2027 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002028
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002029 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002030
2031 /*
2032 * Option selection
2033 */
2034 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002035 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002036 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2037 invert);
2038
2039 /* Canonicalize into lower case */
2040 for (protocol = argv[optind-1]; *protocol; protocol++)
2041 *protocol = tolower(*protocol);
2042
2043 protocol = argv[optind-1];
2044 fw.ipv6.proto = parse_protocol(protocol);
2045 fw.ipv6.flags |= IP6T_F_PROTO;
2046
2047 if (fw.ipv6.proto == 0
2048 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2049 exit_error(PARAMETER_PROBLEM,
2050 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002051
2052 if (fw.ipv6.proto != IPPROTO_ESP &&
2053 is_exthdr(fw.ipv6.proto))
2054 printf("Warning: never matched protocol: %s. "
2055 "use exension match instead.", protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002056 break;
2057
2058 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002059 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002060 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2061 invert);
2062 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002063 break;
2064
2065 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002066 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002067 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2068 invert);
2069 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002070 break;
2071
2072 case 'j':
2073 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2074 invert);
2075 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00002076 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002077 target = find_target(jumpto, TRY_LOAD);
2078
2079 if (target) {
2080 size_t size;
2081
Harald Welte43ac1912002-03-03 09:43:07 +00002082 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2083 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002084
2085 target->t = fw_calloc(1, size);
2086 target->t->u.target_size = size;
2087 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002088 if (target->init != NULL)
2089 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002090 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002091 }
2092 break;
2093
2094
2095 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002096 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002097 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2098 invert);
2099 parse_interface(argv[optind-1],
2100 fw.ipv6.iniface,
2101 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002102 break;
2103
2104 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002105 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002106 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2107 invert);
2108 parse_interface(argv[optind-1],
2109 fw.ipv6.outiface,
2110 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002111 break;
2112
2113 case 'v':
2114 if (!verbose)
2115 set_option(&options, OPT_VERBOSE,
2116 &fw.ipv6.invflags, invert);
2117 verbose++;
2118 break;
2119
2120 case 'm': {
2121 size_t size;
2122
2123 if (invert)
2124 exit_error(PARAMETER_PROBLEM,
2125 "unexpected ! flag before --match");
2126
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002127 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002128 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2129 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002130 m->m = fw_calloc(1, size);
2131 m->m->u.match_size = size;
2132 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002133 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002134 if (m->init != NULL)
2135 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002136 if (m != m->next)
2137 /* Merge options for non-cloned matches */
2138 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002139 }
2140 break;
2141
2142 case 'n':
2143 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2144 invert);
2145 break;
2146
2147 case 't':
2148 if (invert)
2149 exit_error(PARAMETER_PROBLEM,
2150 "unexpected ! flag before --table");
2151 *table = argv[optind-1];
2152 break;
2153
2154 case 'x':
2155 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2156 invert);
2157 break;
2158
2159 case 'V':
2160 if (invert)
2161 printf("Not %s ;-)\n", program_version);
2162 else
2163 printf("%s v%s\n",
2164 program_name, program_version);
2165 exit(0);
2166
2167 case '0':
2168 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2169 invert);
2170 break;
2171
Harald Welte43ac1912002-03-03 09:43:07 +00002172 case 'M':
2173 modprobe = optarg;
2174 break;
2175
2176 case 'c':
2177
2178 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2179 invert);
2180 pcnt = optarg;
2181 if (optind < argc && argv[optind][0] != '-'
2182 && argv[optind][0] != '!')
2183 bcnt = argv[optind++];
2184 else
2185 exit_error(PARAMETER_PROBLEM,
2186 "-%c requires packet and byte counter",
2187 opt2char(OPT_COUNTERS));
2188
Martin Josefssona28d4952004-05-26 16:04:48 +00002189 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002190 exit_error(PARAMETER_PROBLEM,
2191 "-%c packet counter not numeric",
2192 opt2char(OPT_COUNTERS));
2193
Martin Josefssona28d4952004-05-26 16:04:48 +00002194 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002195 exit_error(PARAMETER_PROBLEM,
2196 "-%c byte counter not numeric",
2197 opt2char(OPT_COUNTERS));
2198
2199 break;
2200
2201
Rusty Russell5eed48a2000-06-02 20:12:24 +00002202 case 1: /* non option */
2203 if (optarg[0] == '!' && optarg[1] == '\0') {
2204 if (invert)
2205 exit_error(PARAMETER_PROBLEM,
2206 "multiple consecutive ! not"
2207 " allowed");
2208 invert = TRUE;
2209 optarg[0] = '\0';
2210 continue;
2211 }
2212 printf("Bad argument `%s'\n", optarg);
2213 exit_tryhelp(2);
2214
2215 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002216 if (!target
2217 || !(target->parse(c - target->option_offset,
2218 argv, invert,
2219 &target->tflags,
2220 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002221 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002222 if (matchp->completed)
2223 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002224 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002225 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002226 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002227 &fw,
2228 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002229 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002230 break;
2231 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002232 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002233
2234 /* If you listen carefully, you can
2235 actually hear this code suck. */
2236
2237 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002238 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002239 * parameter, that has not been parsed yet,
2240 * it's not an option of an explicitly loaded
2241 * match or a target. However, we support
2242 * implicit loading of the protocol match
2243 * extension. '-p tcp' means 'l4 proto 6' and
2244 * at the same time 'load tcp protocol match on
2245 * demand if we specify --dport'.
2246 *
2247 * To make this work, we need to make sure:
2248 * - the parameter has not been parsed by
2249 * a match (m above)
2250 * - a protocol has been specified
2251 * - the protocol extension has not been
2252 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002253 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002254 * - the protocol extension can be successively
2255 * loaded
2256 */
2257 if (m == NULL
2258 && protocol
2259 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002260 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002261 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002262 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002263 && (proto_used == 0))
2264 )
2265 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002266 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002267 /* Try loading protocol */
2268 size_t size;
2269
2270 proto_used = 1;
2271
2272 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2273 + m->size;
2274
2275 m->m = fw_calloc(1, size);
2276 m->m->u.match_size = size;
2277 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002278 set_revision(m->m->u.user.name,
2279 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002280 if (m->init != NULL)
2281 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002282
2283 opts = merge_options(opts,
2284 m->extra_opts, &m->option_offset);
2285
2286 optind--;
2287 continue;
2288 }
2289
Rusty Russell5eed48a2000-06-02 20:12:24 +00002290 if (!m)
2291 exit_error(PARAMETER_PROBLEM,
2292 "Unknown arg `%s'",
2293 argv[optind-1]);
2294 }
2295 }
2296 invert = FALSE;
2297 }
2298
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002299 for (matchp = matches; matchp; matchp = matchp->next)
2300 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002301
Harald Welte43ac1912002-03-03 09:43:07 +00002302 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002303 target->final_check(target->tflags);
2304
2305 /* Fix me: must put inverse options checking here --MN */
2306
2307 if (optind < argc)
2308 exit_error(PARAMETER_PROBLEM,
2309 "unknown arguments found on commandline");
2310 if (!command)
2311 exit_error(PARAMETER_PROBLEM, "no command specified");
2312 if (invert)
2313 exit_error(PARAMETER_PROBLEM,
2314 "nothing appropriate following !");
2315
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002316 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002317 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002318 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002319 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002320 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002321 }
2322
2323 if (shostnetworkmask)
2324 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2325 &(fw.ipv6.smsk), &nsaddrs);
2326
2327 if (dhostnetworkmask)
2328 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2329 &(fw.ipv6.dmsk), &ndaddrs);
2330
2331 if ((nsaddrs > 1 || ndaddrs > 1) &&
2332 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2333 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2334 " source or destination IP addresses");
2335
Rusty Russell5eed48a2000-06-02 20:12:24 +00002336 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2337 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2338 "specify a unique address");
2339
2340 generic_opt_check(command, options);
2341
2342 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2343 exit_error(PARAMETER_PROBLEM,
2344 "chain name `%s' too long (must be under %i chars)",
2345 chain, IP6T_FUNCTION_MAXNAMELEN);
2346
Harald Welte43ac1912002-03-03 09:43:07 +00002347 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002348 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002349 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002350
Rusty Russell8beb0492004-12-22 00:37:10 +00002351 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00002352 if (!*handle && load_ip6tables_ko(modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002353 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002354
Harald Welte43ac1912002-03-03 09:43:07 +00002355 if (!*handle)
2356 exit_error(VERSION_PROBLEM,
2357 "can't initialize ip6tables table `%s': %s",
2358 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002359
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002360 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002361 || command == CMD_DELETE
2362 || command == CMD_INSERT
2363 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002364 if (strcmp(chain, "PREROUTING") == 0
2365 || strcmp(chain, "INPUT") == 0) {
2366 /* -o not valid with incoming packets. */
2367 if (options & OPT_VIANAMEOUT)
2368 exit_error(PARAMETER_PROBLEM,
2369 "Can't use -%c with %s\n",
2370 opt2char(OPT_VIANAMEOUT),
2371 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002372 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002373
Harald Welte43ac1912002-03-03 09:43:07 +00002374 if (strcmp(chain, "POSTROUTING") == 0
2375 || strcmp(chain, "OUTPUT") == 0) {
2376 /* -i not valid with outgoing packets */
2377 if (options & OPT_VIANAMEIN)
2378 exit_error(PARAMETER_PROBLEM,
2379 "Can't use -%c with %s\n",
2380 opt2char(OPT_VIANAMEIN),
2381 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002382 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002383
2384 if (target && ip6tc_is_chain(jumpto, *handle)) {
2385 printf("Warning: using chain %s, not extension\n",
2386 jumpto);
2387
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002388 if (target->t)
2389 free(target->t);
2390
Rusty Russell5eed48a2000-06-02 20:12:24 +00002391 target = NULL;
2392 }
2393
2394 /* If they didn't specify a target, or it's a chain
2395 name, use standard. */
2396 if (!target
2397 && (strlen(jumpto) == 0
2398 || ip6tc_is_chain(jumpto, *handle))) {
2399 size_t size;
2400
2401 target = find_target(IP6T_STANDARD_TARGET,
2402 LOAD_MUST_SUCCEED);
2403
2404 size = sizeof(struct ip6t_entry_target)
2405 + target->size;
2406 target->t = fw_calloc(1, size);
2407 target->t->u.target_size = size;
2408 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002409 if (target->init != NULL)
2410 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002411 }
2412
2413 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002414 /* it is no chain, and we can't load a plugin.
2415 * We cannot know if the plugin is corrupt, non
2416 * existant OR if the user just misspelled a
2417 * chain. */
2418 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002419 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002420 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002421 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002422 }
2423 }
2424
2425 switch (command) {
2426 case CMD_APPEND:
2427 ret = append_entry(chain, e,
2428 nsaddrs, saddrs, ndaddrs, daddrs,
2429 options&OPT_VERBOSE,
2430 handle);
2431 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002432 case CMD_DELETE:
2433 ret = delete_entry(chain, e,
2434 nsaddrs, saddrs, ndaddrs, daddrs,
2435 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002436 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002437 break;
2438 case CMD_DELETE_NUM:
2439 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2440 break;
2441 case CMD_REPLACE:
2442 ret = replace_entry(chain, e, rulenum - 1,
2443 saddrs, daddrs, options&OPT_VERBOSE,
2444 handle);
2445 break;
2446 case CMD_INSERT:
2447 ret = insert_entry(chain, e, rulenum - 1,
2448 nsaddrs, saddrs, ndaddrs, daddrs,
2449 options&OPT_VERBOSE,
2450 handle);
2451 break;
2452 case CMD_LIST:
2453 ret = list_entries(chain,
2454 options&OPT_VERBOSE,
2455 options&OPT_NUMERIC,
2456 options&OPT_EXPANDED,
2457 options&OPT_LINENUMBERS,
2458 handle);
2459 break;
2460 case CMD_FLUSH:
2461 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2462 break;
2463 case CMD_ZERO:
2464 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2465 break;
2466 case CMD_LIST|CMD_ZERO:
2467 ret = list_entries(chain,
2468 options&OPT_VERBOSE,
2469 options&OPT_NUMERIC,
2470 options&OPT_EXPANDED,
2471 options&OPT_LINENUMBERS,
2472 handle);
2473 if (ret)
2474 ret = zero_entries(chain,
2475 options&OPT_VERBOSE, handle);
2476 break;
2477 case CMD_NEW_CHAIN:
2478 ret = ip6tc_create_chain(chain, handle);
2479 break;
2480 case CMD_DELETE_CHAIN:
2481 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2482 break;
2483 case CMD_RENAME_CHAIN:
2484 ret = ip6tc_rename_chain(chain, newname, handle);
2485 break;
2486 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002487 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002488 break;
2489 default:
2490 /* We should never reach this... */
2491 exit_tryhelp(2);
2492 }
2493
2494 if (verbose > 1)
2495 dump_entries6(*handle);
2496
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002497 clear_rule_matches(&matches);
2498
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002499 if (e != NULL) {
2500 free(e);
2501 e = NULL;
2502 }
2503
2504 for (c = 0; c < nsaddrs; c++)
2505 free(&saddrs[c]);
2506
2507 for (c = 0; c < ndaddrs; c++)
2508 free(&daddrs[c]);
2509
Pablo Neiradfdcd642005-05-29 19:05:23 +00002510 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002511
Rusty Russell5eed48a2000-06-02 20:12:24 +00002512 return ret;
2513}