blob: 92e434cfdcb3145153ae7db569a07a07f3eb1fed [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
38#include <ip6tables.h>
39#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
42#include <sys/wait.h>
43#include <sys/types.h>
44#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000053#ifndef PROC_SYS_MODPROBE
54#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55#endif
56
Rusty Russell5eed48a2000-06-02 20:12:24 +000057#define FMT_NUMERIC 0x0001
58#define FMT_NOCOUNTS 0x0002
59#define FMT_KILOMEGAGIGA 0x0004
60#define FMT_OPTIONS 0x0008
61#define FMT_NOTABLE 0x0010
62#define FMT_NOTARGET 0x0020
63#define FMT_VIA 0x0040
64#define FMT_NONEWLINE 0x0080
65#define FMT_LINENUMBERS 0x0100
66
67#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
68 | FMT_NUMERIC | FMT_NOTABLE)
69#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
70
71
72#define CMD_NONE 0x0000U
73#define CMD_INSERT 0x0001U
74#define CMD_DELETE 0x0002U
75#define CMD_DELETE_NUM 0x0004U
76#define CMD_REPLACE 0x0008U
77#define CMD_APPEND 0x0010U
78#define CMD_LIST 0x0020U
79#define CMD_FLUSH 0x0040U
80#define CMD_ZERO 0x0080U
81#define CMD_NEW_CHAIN 0x0100U
82#define CMD_DELETE_CHAIN 0x0200U
83#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000084#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000085#define NUMBER_OF_CMD 13
86static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000087 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
89#define OPTION_OFFSET 256
90
91#define OPT_NONE 0x00000U
92#define OPT_NUMERIC 0x00001U
93#define OPT_SOURCE 0x00002U
94#define OPT_DESTINATION 0x00004U
95#define OPT_PROTOCOL 0x00008U
96#define OPT_JUMP 0x00010U
97#define OPT_VERBOSE 0x00020U
98#define OPT_EXPANDED 0x00040U
99#define OPT_VIANAMEIN 0x00080U
100#define OPT_VIANAMEOUT 0x00100U
101#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000102#define OPT_COUNTERS 0x00400U
103#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
123 { "protocol", 1, 0, 'p' },
124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000135 { "modprobe", 1, 0, 'M' },
136 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000137 { 0 }
138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Rusty Russell5eed48a2000-06-02 20:12:24 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161 /* -n -s -d -p -j -v -x -i -o --line -c */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x','x'},
174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IP6T_INV_SRCIP,
181/* -d */ IP6T_INV_DSTIP,
182/* -p */ IP6T_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IP6T_INV_VIA_IN,
187/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190};
191
192const char *program_version;
193const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000194char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000195
Rusty Russell5eed48a2000-06-02 20:12:24 +0000196/* Keeping track of external matches and targets: linked lists. */
197struct ip6tables_match *ip6tables_matches = NULL;
198struct ip6tables_target *ip6tables_targets = NULL;
199
200/* Extra debugging from libiptc */
201extern void dump_entries6(const ip6tc_handle_t handle);
202
203/* A few hardcoded protocols for 'all' and in case the user has no
204 /etc/protocols */
205struct pprot {
206 char *name;
207 u_int8_t num;
208};
209
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000210/* Primitive headers... */
211/* defined in netinet/in.h */
212#if 0
213#ifndef IPPROTO_ESP
214#define IPPROTO_ESP 50
215#endif
216#ifndef IPPROTO_AH
217#define IPPROTO_AH 51
218#endif
219#endif
220
Rusty Russell5eed48a2000-06-02 20:12:24 +0000221static const struct pprot chain_protos[] = {
222 { "tcp", IPPROTO_TCP },
223 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000224 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000225 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000226 { "esp", IPPROTO_ESP },
227 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000228};
229
230static char *
231proto_to_name(u_int8_t proto, int nolookup)
232{
233 unsigned int i;
234
235 if (proto && !nolookup) {
236 struct protoent *pent = getprotobynumber(proto);
237 if (pent)
238 return pent->p_name;
239 }
240
241 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
242 if (chain_protos[i].num == proto)
243 return chain_protos[i].name;
244
245 return NULL;
246}
247
Phil Oester58179b12006-07-20 17:00:19 +0000248int
249service_to_port(const char *name, const char *proto)
250{
251 struct servent *service;
252
253 if ((service = getservbyname(name, proto)) != NULL)
254 return ntohs((unsigned short) service->s_port);
255
256 return -1;
257}
258
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000259u_int16_t
260parse_port(const char *port, const char *proto)
261{
262 unsigned int portnum;
263
264 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
265 (portnum = service_to_port(port, proto)) != -1)
266 return (u_int16_t)portnum;
267
268 exit_error(PARAMETER_PROBLEM,
269 "invalid port/service `%s' specified", port);
270}
271
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272static void
273in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
274{
275 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000276 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000277}
278
Pablo Neiradfdcd642005-05-29 19:05:23 +0000279static void free_opts(int reset_offset)
280{
281 if (opts != original_opts) {
282 free(opts);
283 opts = original_opts;
284 if (reset_offset)
285 global_option_offset = 0;
286 }
287}
288
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289void
290exit_error(enum exittype status, char *msg, ...)
291{
292 va_list args;
293
294 va_start(args, msg);
295 fprintf(stderr, "%s v%s: ", program_name, program_version);
296 vfprintf(stderr, msg, args);
297 va_end(args);
298 fprintf(stderr, "\n");
299 if (status == PARAMETER_PROBLEM)
300 exit_tryhelp(status);
301 if (status == VERSION_PROBLEM)
302 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000303 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000304 /* On error paths, make sure that we don't leak memory */
305 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000306 exit(status);
307}
308
309void
310exit_tryhelp(int status)
311{
Harald Weltea8658ca2003-03-05 07:46:15 +0000312 if (line != -1)
313 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
315 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000316 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000317 exit(status);
318}
319
320void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000321exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000323 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000324 struct ip6tables_target *t = NULL;
325
326 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000327"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000328" %s -[RI] chain rulenum rule-specification [options]\n"
329" %s -D chain rulenum [options]\n"
330" %s -[LFZ] [chain] [options]\n"
331" %s -[NX] chain\n"
332" %s -E old-chain-name new-chain-name\n"
333" %s -P chain target [options]\n"
334" %s -h (print this help information)\n\n",
335 program_name, program_version, program_name, program_name,
336 program_name, program_name, program_name, program_name,
337 program_name, program_name);
338
339 printf(
340"Commands:\n"
341"Either long or short options are allowed.\n"
342" --append -A chain Append to chain\n"
343" --delete -D chain Delete matching rule from chain\n"
344" --delete -D chain rulenum\n"
345" Delete rule rulenum (1 = first) from chain\n"
346" --insert -I chain [rulenum]\n"
347" Insert in chain as rulenum (default 1=first)\n"
348" --replace -R chain rulenum\n"
349" Replace rule rulenum (1 = first) in chain\n"
350" --list -L [chain] List the rules in a chain or all chains\n"
351" --flush -F [chain] Delete all rules in chain or all chains\n"
352" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000353" --new -N chain Create a new user-defined chain\n"
354" --delete-chain\n"
355" -X [chain] Delete a user-defined chain\n"
356" --policy -P chain target\n"
357" Change policy on chain to target\n"
358" --rename-chain\n"
359" -E old-chain new-chain\n"
360" Change chain name, (moving any references)\n"
361
362"Options:\n"
363" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
364" --source -s [!] address[/mask]\n"
365" source specification\n"
366" --destination -d [!] address[/mask]\n"
367" destination specification\n"
368" --in-interface -i [!] input name[+]\n"
369" network interface name ([+] for wildcard)\n"
370" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000371" target for rule (may load target extension)\n"
372" --match -m match\n"
373" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000374" --numeric -n numeric output of addresses and ports\n"
375" --out-interface -o [!] output name[+]\n"
376" network interface name ([+] for wildcard)\n"
377" --table -t table table to manipulate (default: `filter')\n"
378" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000379" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000380" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000381/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000382" --modprobe=<command> try to insert modules using this command\n"
383" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000384"[!] --version -V print package version.\n");
385
386 /* Print out any special helps. A user might like to be able to add a --help
387 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000388 specified matches & targets */
389 for (t = ip6tables_targets; t; t = t->next) {
390 if (t->used) {
391 printf("\n");
392 t->help();
393 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000394 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000395 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000397 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000398 }
399 exit(0);
400}
401
402static void
403generic_opt_check(int command, int options)
404{
405 int i, j, legal = 0;
406
407 /* Check that commands are valid with options. Complicated by the
408 * fact that if an option is legal with *any* command given, it is
409 * legal overall (ie. -z and -l).
410 */
411 for (i = 0; i < NUMBER_OF_OPT; i++) {
412 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
413
414 for (j = 0; j < NUMBER_OF_CMD; j++) {
415 if (!(command & (1<<j)))
416 continue;
417
418 if (!(options & (1<<i))) {
419 if (commands_v_options[j][i] == '+')
420 exit_error(PARAMETER_PROBLEM,
421 "You need to supply the `-%c' "
422 "option for this command\n",
423 optflags[i]);
424 } else {
425 if (commands_v_options[j][i] != 'x')
426 legal = 1;
427 else if (legal == 0)
428 legal = -1;
429 }
430 }
431 if (legal == -1)
432 exit_error(PARAMETER_PROBLEM,
433 "Illegal option `-%c' with this command\n",
434 optflags[i]);
435 }
436}
437
438static char
439opt2char(int option)
440{
441 const char *ptr;
442 for (ptr = optflags; option > 1; option >>= 1, ptr++);
443
444 return *ptr;
445}
446
447static char
448cmd2char(int option)
449{
450 const char *ptr;
451 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
452
453 return *ptr;
454}
455
456static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000457add_command(unsigned int *cmd, const int newcmd, const int othercmds,
458 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000459{
460 if (invert)
461 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
462 if (*cmd & (~othercmds))
463 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
464 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
465 *cmd |= newcmd;
466}
467
468int
Harald Welteb77f1da2002-03-14 11:35:58 +0000469check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470{
471 if (option && strcmp(option, "!") == 0) {
472 if (*invert)
473 exit_error(PARAMETER_PROBLEM,
474 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000475 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000476 if (optind) {
477 *optind = *optind+1;
478 if (argc && *optind > argc)
479 exit_error(PARAMETER_PROBLEM,
480 "no argument following `!'");
481 }
482
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483 return TRUE;
484 }
485 return FALSE;
486}
487
488static void *
489fw_calloc(size_t count, size_t size)
490{
491 void *p;
492
493 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000494 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000495 exit(1);
496 }
497 return p;
498}
499
500static void *
501fw_malloc(size_t size)
502{
503 void *p;
504
505 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000506 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000507 exit(1);
508 }
509 return p;
510}
511
Rusty Russell5eed48a2000-06-02 20:12:24 +0000512static char *
513addr_to_numeric(const struct in6_addr *addrp)
514{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000515 /* 0000:0000:0000:0000:0000:000.000.000.000
516 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000517 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000518 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519}
520
521static struct in6_addr *
522numeric_to_addr(const char *num)
523{
524 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000525 int err;
526 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000527 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000528#ifdef DEBUG
529 fprintf(stderr, "\nnumeric2addr: %d\n", err);
530#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000531 return (struct in6_addr *)NULL;
532}
533
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000534
535static struct in6_addr *
536host_to_addr(const char *name, unsigned int *naddr)
537{
538 struct addrinfo hints;
539 struct addrinfo *res;
540 static struct in6_addr *addr;
541 int err;
542
543 memset(&hints, 0, sizeof(hints));
544 hints.ai_flags=AI_CANONNAME;
545 hints.ai_family=AF_INET6;
546 hints.ai_socktype=SOCK_RAW;
547 hints.ai_protocol=41;
548 hints.ai_next=NULL;
549
550 *naddr = 0;
551 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
552#ifdef DEBUG
553 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
554#endif
555 return (struct in6_addr *) NULL;
556 } else {
557 if (res->ai_family != AF_INET6 ||
558 res->ai_addrlen != sizeof(struct sockaddr_in6))
559 return (struct in6_addr *) NULL;
560
561#ifdef DEBUG
562 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
563 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
564#endif
565 /* Get the first element of the address-chain */
566 addr = fw_calloc(1, sizeof(struct in6_addr));
567 in6addrcpy(addr, (struct in6_addr *)
568 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
569 freeaddrinfo(res);
570 *naddr = 1;
571 return addr;
572 }
573
574 return (struct in6_addr *) NULL;
575}
576
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000577static char *
578addr_to_host(const struct in6_addr *addr)
579{
580 struct sockaddr_in6 saddr;
581 int err;
582 static char hostname[NI_MAXHOST];
583
584 memset(&saddr, 0, sizeof(struct sockaddr_in6));
585 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000586 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000587
588 if ( (err=getnameinfo((struct sockaddr *)&saddr,
589 sizeof(struct sockaddr_in6),
590 hostname, sizeof(hostname)-1,
591 NULL, 0, 0)) != 0 ){
592#ifdef DEBUG
593 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
594#endif
595 return (char *) NULL;
596 } else {
597#ifdef DEBUG
598 fprintf (stderr, "\naddr2host: %s\n", hostname);
599#endif
600
601 return hostname;
602 }
603
604 return (char *) NULL;
605}
606
Rusty Russell5eed48a2000-06-02 20:12:24 +0000607static char *
608mask_to_numeric(const struct in6_addr *addrp)
609{
Harald Welte8a50ab82003-06-24 18:15:59 +0000610 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000611 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000612 if (l == -1) {
613 strcpy(buf, "/");
614 strcat(buf, addr_to_numeric(addrp));
615 return buf;
616 }
Harald Welte43ac1912002-03-03 09:43:07 +0000617 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000618 return buf;
619}
620
621static struct in6_addr *
622network_to_addr(const char *name)
623{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000624 /* abort();*/
625 /* TODO: not implemented yet, but the exception breaks the
626 * name resolvation */
627 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000628}
629
630static char *
631addr_to_anyname(const struct in6_addr *addr)
632{
633 char *name;
634
635 if ((name = addr_to_host(addr)) != NULL)
636 return name;
637
638 return addr_to_numeric(addr);
639}
640
641/*
642 * All functions starting with "parse" should succeed, otherwise
643 * the program fails.
644 * Most routines return pointers to static data that may change
645 * between calls to the same or other routines with a few exceptions:
646 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
647 * return global static data.
648*/
649
650static struct in6_addr *
651parse_hostnetwork(const char *name, unsigned int *naddrs)
652{
653 struct in6_addr *addrp, *addrptmp;
654
655 if ((addrptmp = numeric_to_addr(name)) != NULL ||
656 (addrptmp = network_to_addr(name)) != NULL) {
657 addrp = fw_malloc(sizeof(struct in6_addr));
658 in6addrcpy(addrp, addrptmp);
659 *naddrs = 1;
660 return addrp;
661 }
662 if ((addrp = host_to_addr(name, naddrs)) != NULL)
663 return addrp;
664
665 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
666}
667
668static struct in6_addr *
669parse_mask(char *mask)
670{
671 static struct in6_addr maskaddr;
672 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000673 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000674
675 if (mask == NULL) {
676 /* no mask at all defaults to 128 bits */
677 memset(&maskaddr, 0xff, sizeof maskaddr);
678 return &maskaddr;
679 }
680 if ((addrp = numeric_to_addr(mask)) != NULL)
681 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000682 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000683 exit_error(PARAMETER_PROBLEM,
684 "invalid mask `%s' specified", mask);
685 if (bits != 0) {
686 char *p = (char *)&maskaddr;
687 memset(p, 0xff, bits / 8);
688 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
689 p[bits / 8] = 0xff << (8 - (bits & 7));
690 return &maskaddr;
691 }
692
693 memset(&maskaddr, 0, sizeof maskaddr);
694 return &maskaddr;
695}
696
Harald Welte43ac1912002-03-03 09:43:07 +0000697void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000698parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
699 struct in6_addr *maskp, unsigned int *naddrs)
700{
701 struct in6_addr *addrp;
702 char buf[256];
703 char *p;
704 int i, j, n;
705
706 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000707 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708 if ((p = strrchr(buf, '/')) != NULL) {
709 *p = '\0';
710 addrp = parse_mask(p + 1);
711 } else
712 addrp = parse_mask(NULL);
713 in6addrcpy(maskp, addrp);
714
715 /* if a null mask is given, the name is ignored, like in "any/0" */
716 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
717 strcpy(buf, "::");
718
719 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
720 n = *naddrs;
721 for (i = 0, j = 0; i < n; i++) {
722 int k;
723 for (k = 0; k < 4; k++)
724 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
725 j++;
726 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000727 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 (*naddrs)--;
729 j--;
730 break;
731 }
732 }
733 }
734}
735
736struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000737find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738{
739 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000740 const char *icmp6 = "icmp6";
741 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000742
743 /* This is ugly as hell. Nonetheless, there is no way of changing
744 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000745 if ( (strcmp(match_name,"icmpv6") == 0) ||
746 (strcmp(match_name,"ipv6-icmp") == 0) ||
747 (strcmp(match_name,"icmp6") == 0) )
748 name = icmp6;
749 else
750 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000751
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000752 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
753 if (strcmp(name, ptr->name) == 0) {
754 struct ip6tables_match *clone;
755
756 /* First match of this type: */
757 if (ptr->m == NULL)
758 break;
759
760 /* Second and subsequent clones */
761 clone = fw_malloc(sizeof(struct ip6tables_match));
762 memcpy(clone, ptr, sizeof(struct ip6tables_match));
763 clone->mflags = 0;
764 /* This is a clone: */
765 clone->next = clone;
766
767 ptr = clone;
768 break;
769 }
770 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771
Harald Welte3efb6ea2001-08-06 18:50:21 +0000772#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000773 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000774 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000775 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000776 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777 if (dlopen(path, RTLD_NOW)) {
778 /* Found library. If it didn't register itself,
779 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000780 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781
782 if (!ptr)
783 exit_error(PARAMETER_PROBLEM,
784 "Couldn't load match `%s'\n",
785 name);
786 } else if (tryload == LOAD_MUST_SUCCEED)
787 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000788 "Couldn't load match `%s':%s\n",
789 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000791#else
792 if (ptr && !ptr->loaded) {
793 if (tryload != DONT_LOAD)
794 ptr->loaded = 1;
795 else
796 ptr = NULL;
797 }
Marc Boucher067477b2002-03-24 15:09:31 +0000798 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
799 exit_error(PARAMETER_PROBLEM,
800 "Couldn't find match `%s'\n", name);
801 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000802#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000804 if (ptr && matches) {
805 struct ip6tables_rule_match **i;
806 struct ip6tables_rule_match *newentry;
807
808 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
809
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000810 for (i = matches; *i; i = &(*i)->next) {
811 if (strcmp(name, (*i)->match->name) == 0)
812 (*i)->completed = 1;
813 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000814 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000815 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000816 newentry->next = NULL;
817 *i = newentry;
818 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000819
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 return ptr;
821}
822
823/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
824static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000825find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826{
Harald Welteed498492001-07-23 01:24:22 +0000827 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828
Harald Welte43ac1912002-03-03 09:43:07 +0000829 if (string_to_number(pname, 0, 255, &proto) != -1) {
830 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831
Harald Welte43ac1912002-03-03 09:43:07 +0000832 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000833 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000834 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000835 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000836
837 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000838}
839
Harald Welte43ac1912002-03-03 09:43:07 +0000840u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841parse_protocol(const char *s)
842{
Harald Welteed498492001-07-23 01:24:22 +0000843 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844
Harald Welteed498492001-07-23 01:24:22 +0000845 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846 struct protoent *pent;
847
Harald Weltecbe1ec72006-02-11 09:50:11 +0000848 /* first deal with the special case of 'all' to prevent
849 * people from being able to redefine 'all' in nsswitch
850 * and/or provoke expensive [not working] ldap/nis/...
851 * lookups */
852 if (!strcmp(s, "all"))
853 return 0;
854
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 if ((pent = getprotobyname(s)))
856 proto = pent->p_proto;
857 else {
858 unsigned int i;
859 for (i = 0;
860 i < sizeof(chain_protos)/sizeof(struct pprot);
861 i++) {
862 if (strcmp(s, chain_protos[i].name) == 0) {
863 proto = chain_protos[i].num;
864 break;
865 }
866 }
867 if (i == sizeof(chain_protos)/sizeof(struct pprot))
868 exit_error(PARAMETER_PROBLEM,
869 "unknown protocol `%s' specified",
870 s);
871 }
872 }
873
874 return (u_int16_t)proto;
875}
876
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000877/* proto means IPv6 extension header ? */
878static int is_exthdr(u_int16_t proto)
879{
880 return (proto == IPPROTO_HOPOPTS ||
881 proto == IPPROTO_ROUTING ||
882 proto == IPPROTO_FRAGMENT ||
883 proto == IPPROTO_ESP ||
884 proto == IPPROTO_AH ||
885 proto == IPPROTO_DSTOPTS);
886}
887
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000888void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889{
890 int vialen = strlen(arg);
891 unsigned int i;
892
893 memset(mask, 0, IFNAMSIZ);
894 memset(vianame, 0, IFNAMSIZ);
895
896 if (vialen + 1 > IFNAMSIZ)
897 exit_error(PARAMETER_PROBLEM,
898 "interface name `%s' must be shorter than IFNAMSIZ"
899 " (%i)", arg, IFNAMSIZ-1);
900
901 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000902 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 memset(mask, 0, IFNAMSIZ);
904 else if (vianame[vialen - 1] == '+') {
905 memset(mask, 0xFF, vialen - 1);
906 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000907 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 } else {
909 /* Include nul-terminator in match */
910 memset(mask, 0xFF, vialen + 1);
911 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000912 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000913 if (vianame[i] == ':' ||
914 vianame[i] == '!' ||
915 vianame[i] == '*') {
916 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000917 " `%s' (No aliases, :, ! or *).\n",
918 vianame);
919 break;
920 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000921 }
922 }
923}
924
925/* Can't be zero. */
926static int
927parse_rulenumber(const char *rule)
928{
Harald Welte43ac1912002-03-03 09:43:07 +0000929 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000930
Harald Welte43ac1912002-03-03 09:43:07 +0000931 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000932 exit_error(PARAMETER_PROBLEM,
933 "Invalid rule number `%s'", rule);
934
935 return rulenum;
936}
937
938static const char *
939parse_target(const char *targetname)
940{
941 const char *ptr;
942
943 if (strlen(targetname) < 1)
944 exit_error(PARAMETER_PROBLEM,
945 "Invalid target name (too short)");
946
947 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
948 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000949 "Invalid target name `%s' (%u chars max)",
950 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000951
952 for (ptr = targetname; *ptr; ptr++)
953 if (isspace(*ptr))
954 exit_error(PARAMETER_PROBLEM,
955 "Invalid target name `%s'", targetname);
956 return targetname;
957}
958
959int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000960string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
961 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000962{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000963 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964 char *end;
965
966 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000967 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000968 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000969 if (*end == '\0' && end != s) {
970 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000971 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000972 *ret = number;
973 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000974 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000975 }
976 return -1;
977}
978
Martin Josefssonb105bc92004-05-26 15:54:49 +0000979int
980string_to_number_l(const char *s, unsigned long min, unsigned long max,
981 unsigned long *ret)
982{
983 int result;
984 unsigned long long number;
985
986 result = string_to_number_ll(s, min, max, &number);
987 *ret = (unsigned long)number;
988
989 return result;
990}
991
992int string_to_number(const char *s, unsigned int min, unsigned int max,
993 unsigned int *ret)
994{
995 int result;
996 unsigned long number;
997
998 result = string_to_number_l(s, min, max, &number);
999 *ret = (unsigned int)number;
1000
1001 return result;
1002}
1003
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004static void
1005set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1006 int invert)
1007{
1008 if (*options & option)
1009 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1010 opt2char(option));
1011 *options |= option;
1012
1013 if (invert) {
1014 unsigned int i;
1015 for (i = 0; 1 << i != option; i++);
1016
1017 if (!inverse_for_options[i])
1018 exit_error(PARAMETER_PROBLEM,
1019 "cannot have ! before -%c",
1020 opt2char(option));
1021 *invflg |= inverse_for_options[i];
1022 }
1023}
1024
1025struct ip6tables_target *
1026find_target(const char *name, enum ip6t_tryload tryload)
1027{
1028 struct ip6tables_target *ptr;
1029
1030 /* Standard target? */
1031 if (strcmp(name, "") == 0
1032 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1033 || strcmp(name, IP6TC_LABEL_DROP) == 0
1034 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1035 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1036 name = "standard";
1037
1038 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1039 if (strcmp(name, ptr->name) == 0)
1040 break;
1041 }
1042
Harald Welte3efb6ea2001-08-06 18:50:21 +00001043#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001044 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001045 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001046 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001047 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 if (dlopen(path, RTLD_NOW)) {
1049 /* Found library. If it didn't register itself,
1050 maybe they specified match as a target. */
1051 ptr = find_target(name, DONT_LOAD);
1052 if (!ptr)
1053 exit_error(PARAMETER_PROBLEM,
1054 "Couldn't load target `%s'\n",
1055 name);
1056 } else if (tryload == LOAD_MUST_SUCCEED)
1057 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001058 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001059 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001060 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001061#else
1062 if (ptr && !ptr->loaded) {
1063 if (tryload != DONT_LOAD)
1064 ptr->loaded = 1;
1065 else
1066 ptr = NULL;
1067 }
Marc Boucher067477b2002-03-24 15:09:31 +00001068 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1069 exit_error(PARAMETER_PROBLEM,
1070 "Couldn't find target `%s'\n", name);
1071 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001072#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001073
Harald Welte43ac1912002-03-03 09:43:07 +00001074 if (ptr)
1075 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001076
Rusty Russell5eed48a2000-06-02 20:12:24 +00001077 return ptr;
1078}
1079
1080static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001081merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001082 unsigned int *option_offset)
1083{
1084 unsigned int num_old, num_new, i;
1085 struct option *merge;
1086
1087 for (num_old = 0; oldopts[num_old].name; num_old++);
1088 for (num_new = 0; newopts[num_new].name; num_new++);
1089
1090 global_option_offset += OPTION_OFFSET;
1091 *option_offset = global_option_offset;
1092
1093 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1094 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001095 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001096 for (i = 0; i < num_new; i++) {
1097 merge[num_old + i] = newopts[i];
1098 merge[num_old + i].val += *option_offset;
1099 }
1100 memset(merge + num_old + num_new, 0, sizeof(struct option));
1101
1102 return merge;
1103}
1104
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001105static int compatible_revision(const char *name, u_int8_t revision, int opt)
1106{
1107 struct ip6t_get_revision rev;
1108 socklen_t s = sizeof(rev);
1109 int max_rev, sockfd;
1110
1111 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1112 if (sockfd < 0) {
1113 fprintf(stderr, "Could not open socket to kernel: %s\n",
1114 strerror(errno));
1115 exit(1);
1116 }
1117
1118 strcpy(rev.name, name);
1119 rev.revision = revision;
1120
1121 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1122 if (max_rev < 0) {
1123 /* Definitely don't support this? */
1124 if (errno == EPROTONOSUPPORT) {
1125 close(sockfd);
1126 return 0;
1127 } else if (errno == ENOPROTOOPT) {
1128 close(sockfd);
1129 /* Assume only revision 0 support (old kernel) */
1130 return (revision == 0);
1131 } else {
1132 fprintf(stderr, "getsockopt failed strangely: %s\n",
1133 strerror(errno));
1134 exit(1);
1135 }
1136 }
1137 close(sockfd);
1138 return 1;
1139}
1140
1141static int compatible_match_revision(const char *name, u_int8_t revision)
1142{
1143 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1144}
1145
Rusty Russell5eed48a2000-06-02 20:12:24 +00001146void
1147register_match6(struct ip6tables_match *me)
1148{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001149 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001150
Rusty Russell5eed48a2000-06-02 20:12:24 +00001151 if (strcmp(me->version, program_version) != 0) {
1152 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1153 program_name, me->name, me->version, program_version);
1154 exit(1);
1155 }
1156
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001157 /* Revision field stole a char from name. */
1158 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1159 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001160 program_name, me->name);
1161 exit(1);
1162 }
1163
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001164 old = find_match(me->name, DURING_LOAD, NULL);
1165 if (old) {
1166 if (old->revision == me->revision) {
1167 fprintf(stderr,
1168 "%s: match `%s' already registered.\n",
1169 program_name, me->name);
1170 exit(1);
1171 }
1172
1173 /* Now we have two (or more) options, check compatibility. */
1174 if (compatible_match_revision(old->name, old->revision)
1175 && old->revision > me->revision)
1176 return;
1177
1178 /* Replace if compatible. */
1179 if (!compatible_match_revision(me->name, me->revision))
1180 return;
1181
1182 /* Delete old one. */
1183 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1184 *i = old->next;
1185 }
1186
Harald Welte43ac1912002-03-03 09:43:07 +00001187 if (me->size != IP6T_ALIGN(me->size)) {
1188 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001189 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001190 exit(1);
1191 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001192
Harald Welte43ac1912002-03-03 09:43:07 +00001193 /* Append to list. */
1194 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1195 me->next = NULL;
1196 *i = me;
1197
Rusty Russell5eed48a2000-06-02 20:12:24 +00001198 me->m = NULL;
1199 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001200}
1201
1202void
1203register_target6(struct ip6tables_target *me)
1204{
1205 if (strcmp(me->version, program_version) != 0) {
1206 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1207 program_name, me->name, me->version, program_version);
1208 exit(1);
1209 }
1210
Jones Desougif5b86e62005-12-22 03:33:50 +00001211 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001212 fprintf(stderr, "%s: target `%s' already registered.\n",
1213 program_name, me->name);
1214 exit(1);
1215 }
1216
Harald Welte43ac1912002-03-03 09:43:07 +00001217 if (me->size != IP6T_ALIGN(me->size)) {
1218 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001219 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001220 exit(1);
1221 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001222
Rusty Russell5eed48a2000-06-02 20:12:24 +00001223 /* Prepend to list. */
1224 me->next = ip6tables_targets;
1225 ip6tables_targets = me;
1226 me->t = NULL;
1227 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001228}
1229
1230static void
1231print_num(u_int64_t number, unsigned int format)
1232{
Harald Welte43ac1912002-03-03 09:43:07 +00001233 if (format & FMT_KILOMEGAGIGA) {
1234 if (number > 99999) {
1235 number = (number + 500) / 1000;
1236 if (number > 9999) {
1237 number = (number + 500) / 1000;
1238 if (number > 9999) {
1239 number = (number + 500) / 1000;
1240 if (number > 9999) {
1241 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001242 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001243 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001244 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001245 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001246 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001247 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001248 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001249 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001250 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001251 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001252 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001253}
1254
Harald Welte43ac1912002-03-03 09:43:07 +00001255
Rusty Russell5eed48a2000-06-02 20:12:24 +00001256static void
1257print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1258{
1259 struct ip6t_counters counters;
1260 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1261 printf("Chain %s", chain);
1262 if (pol) {
1263 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001264 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001265 fputc(' ', stdout);
1266 print_num(counters.pcnt, (format|FMT_NOTABLE));
1267 fputs("packets, ", stdout);
1268 print_num(counters.bcnt, (format|FMT_NOTABLE));
1269 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001270 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001271 printf(")\n");
1272 } else {
1273 unsigned int refs;
1274 if (!ip6tc_get_references(&refs, chain, handle))
1275 printf(" (ERROR obtaining refs)\n");
1276 else
1277 printf(" (%u references)\n", refs);
1278 }
1279
1280 if (format & FMT_LINENUMBERS)
1281 printf(FMT("%-4s ", "%s "), "num");
1282 if (!(format & FMT_NOCOUNTS)) {
1283 if (format & FMT_KILOMEGAGIGA) {
1284 printf(FMT("%5s ","%s "), "pkts");
1285 printf(FMT("%5s ","%s "), "bytes");
1286 } else {
1287 printf(FMT("%8s ","%s "), "pkts");
1288 printf(FMT("%10s ","%s "), "bytes");
1289 }
1290 }
1291 if (!(format & FMT_NOTARGET))
1292 printf(FMT("%-9s ","%s "), "target");
1293 fputs(" prot ", stdout);
1294 if (format & FMT_OPTIONS)
1295 fputs("opt", stdout);
1296 if (format & FMT_VIA) {
1297 printf(FMT(" %-6s ","%s "), "in");
1298 printf(FMT("%-6s ","%s "), "out");
1299 }
1300 printf(FMT(" %-19s ","%s "), "source");
1301 printf(FMT(" %-19s "," %s "), "destination");
1302 printf("\n");
1303}
1304
Harald Welte43ac1912002-03-03 09:43:07 +00001305
Rusty Russell5eed48a2000-06-02 20:12:24 +00001306static int
1307print_match(const struct ip6t_entry_match *m,
1308 const struct ip6t_ip6 *ip,
1309 int numeric)
1310{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001311 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001312
1313 if (match) {
1314 if (match->print)
1315 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001316 else
1317 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001318 } else {
1319 if (m->u.user.name[0])
1320 printf("UNKNOWN match `%s' ", m->u.user.name);
1321 }
1322 /* Don't stop iterating. */
1323 return 0;
1324}
1325
1326/* e is called `fw' here for hysterical raisins */
1327static void
1328print_firewall(const struct ip6t_entry *fw,
1329 const char *targname,
1330 unsigned int num,
1331 unsigned int format,
1332 const ip6tc_handle_t handle)
1333{
1334 struct ip6tables_target *target = NULL;
1335 const struct ip6t_entry_target *t;
1336 u_int8_t flags;
1337 char buf[BUFSIZ];
1338
Rusty Russell5eed48a2000-06-02 20:12:24 +00001339 if (!ip6tc_is_chain(targname, handle))
1340 target = find_target(targname, TRY_LOAD);
1341 else
1342 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1343
1344 t = ip6t_get_target((struct ip6t_entry *)fw);
1345 flags = fw->ipv6.flags;
1346
1347 if (format & FMT_LINENUMBERS)
1348 printf(FMT("%-4u ", "%u "), num+1);
1349
1350 if (!(format & FMT_NOCOUNTS)) {
1351 print_num(fw->counters.pcnt, format);
1352 print_num(fw->counters.bcnt, format);
1353 }
1354
1355 if (!(format & FMT_NOTARGET))
1356 printf(FMT("%-9s ", "%s "), targname);
1357
1358 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1359 {
Harald Welte43ac1912002-03-03 09:43:07 +00001360 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001361 if (pname)
1362 printf(FMT("%-5s", "%s "), pname);
1363 else
1364 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1365 }
1366
1367 if (format & FMT_OPTIONS) {
1368 if (format & FMT_NOTABLE)
1369 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001370 fputc(' ', stdout); /* Invert flag of FRAG */
1371 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001372 fputc(' ', stdout);
1373 }
1374
1375 if (format & FMT_VIA) {
1376 char iface[IFNAMSIZ+2];
1377
1378 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1379 iface[0] = '!';
1380 iface[1] = '\0';
1381 }
1382 else iface[0] = '\0';
1383
1384 if (fw->ipv6.iniface[0] != '\0') {
1385 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001386 }
1387 else if (format & FMT_NUMERIC) strcat(iface, "*");
1388 else strcat(iface, "any");
1389 printf(FMT(" %-6s ","in %s "), iface);
1390
1391 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1392 iface[0] = '!';
1393 iface[1] = '\0';
1394 }
1395 else iface[0] = '\0';
1396
1397 if (fw->ipv6.outiface[0] != '\0') {
1398 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001399 }
1400 else if (format & FMT_NUMERIC) strcat(iface, "*");
1401 else strcat(iface, "any");
1402 printf(FMT("%-6s ","out %s "), iface);
1403 }
1404
1405 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1406 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1407 && !(format & FMT_NUMERIC))
1408 printf(FMT("%-19s ","%s "), "anywhere");
1409 else {
1410 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001411 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001412 else
Harald Welte43ac1912002-03-03 09:43:07 +00001413 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1415 printf(FMT("%-19s ","%s "), buf);
1416 }
1417
1418 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1419 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1420 && !(format & FMT_NUMERIC))
1421 printf(FMT("%-19s","-> %s"), "anywhere");
1422 else {
1423 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001424 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001425 else
Harald Welte43ac1912002-03-03 09:43:07 +00001426 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1428 printf(FMT("%-19s","-> %s"), buf);
1429 }
1430
1431 if (format & FMT_NOTABLE)
1432 fputs(" ", stdout);
1433
1434 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1435
1436 if (target) {
1437 if (target->print)
1438 /* Print the target information. */
1439 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1440 } else if (t->u.target_size != sizeof(*t))
1441 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001442 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443
1444 if (!(format & FMT_NONEWLINE))
1445 fputc('\n', stdout);
1446}
1447
1448static void
1449print_firewall_line(const struct ip6t_entry *fw,
1450 const ip6tc_handle_t h)
1451{
1452 struct ip6t_entry_target *t;
1453
1454 t = ip6t_get_target((struct ip6t_entry *)fw);
1455 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1456}
1457
1458static int
1459append_entry(const ip6t_chainlabel chain,
1460 struct ip6t_entry *fw,
1461 unsigned int nsaddrs,
1462 const struct in6_addr saddrs[],
1463 unsigned int ndaddrs,
1464 const struct in6_addr daddrs[],
1465 int verbose,
1466 ip6tc_handle_t *handle)
1467{
1468 unsigned int i, j;
1469 int ret = 1;
1470
1471 for (i = 0; i < nsaddrs; i++) {
1472 fw->ipv6.src = saddrs[i];
1473 for (j = 0; j < ndaddrs; j++) {
1474 fw->ipv6.dst = daddrs[j];
1475 if (verbose)
1476 print_firewall_line(fw, *handle);
1477 ret &= ip6tc_append_entry(chain, fw, handle);
1478 }
1479 }
1480
1481 return ret;
1482}
1483
1484static int
1485replace_entry(const ip6t_chainlabel chain,
1486 struct ip6t_entry *fw,
1487 unsigned int rulenum,
1488 const struct in6_addr *saddr,
1489 const struct in6_addr *daddr,
1490 int verbose,
1491 ip6tc_handle_t *handle)
1492{
1493 fw->ipv6.src = *saddr;
1494 fw->ipv6.dst = *daddr;
1495
1496 if (verbose)
1497 print_firewall_line(fw, *handle);
1498 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1499}
1500
1501static int
1502insert_entry(const ip6t_chainlabel chain,
1503 struct ip6t_entry *fw,
1504 unsigned int rulenum,
1505 unsigned int nsaddrs,
1506 const struct in6_addr saddrs[],
1507 unsigned int ndaddrs,
1508 const struct in6_addr daddrs[],
1509 int verbose,
1510 ip6tc_handle_t *handle)
1511{
1512 unsigned int i, j;
1513 int ret = 1;
1514
1515 for (i = 0; i < nsaddrs; i++) {
1516 fw->ipv6.src = saddrs[i];
1517 for (j = 0; j < ndaddrs; j++) {
1518 fw->ipv6.dst = daddrs[j];
1519 if (verbose)
1520 print_firewall_line(fw, *handle);
1521 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1522 }
1523 }
1524
1525 return ret;
1526}
1527
1528static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001529make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530{
1531 /* Establish mask for comparison */
1532 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001533 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 unsigned char *mask, *mptr;
1535
1536 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001537 for (matchp = matches; matchp; matchp = matchp->next)
1538 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001539
1540 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001541 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001542 + ip6tables_targets->size);
1543
1544 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1545 mptr = mask + sizeof(struct ip6t_entry);
1546
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001547 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001548 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001549 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001550 + matchp->match->userspacesize);
1551 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001552 }
1553
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001554 memset(mptr, 0xFF,
1555 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1556 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001557
1558 return mask;
1559}
1560
1561static int
1562delete_entry(const ip6t_chainlabel chain,
1563 struct ip6t_entry *fw,
1564 unsigned int nsaddrs,
1565 const struct in6_addr saddrs[],
1566 unsigned int ndaddrs,
1567 const struct in6_addr daddrs[],
1568 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001569 ip6tc_handle_t *handle,
1570 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001571{
1572 unsigned int i, j;
1573 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 unsigned char *mask;
1575
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001576 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001577 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001578 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001579 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001580 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581 if (verbose)
1582 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001583 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001584 }
1585 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001586 free(mask);
1587
Rusty Russell5eed48a2000-06-02 20:12:24 +00001588 return ret;
1589}
1590
András Kis-Szabó764316a2001-02-26 17:31:20 +00001591int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001592for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1593 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1594{
1595 int ret = 1;
1596 const char *chain;
1597 char *chains;
1598 unsigned int i, chaincount = 0;
1599
1600 chain = ip6tc_first_chain(handle);
1601 while (chain) {
1602 chaincount++;
1603 chain = ip6tc_next_chain(handle);
1604 }
1605
1606 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1607 i = 0;
1608 chain = ip6tc_first_chain(handle);
1609 while (chain) {
1610 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1611 i++;
1612 chain = ip6tc_next_chain(handle);
1613 }
1614
1615 for (i = 0; i < chaincount; i++) {
1616 if (!builtinstoo
1617 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001618 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001619 continue;
1620 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1621 }
1622
1623 free(chains);
1624 return ret;
1625}
1626
András Kis-Szabó764316a2001-02-26 17:31:20 +00001627int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001628flush_entries(const ip6t_chainlabel chain, int verbose,
1629 ip6tc_handle_t *handle)
1630{
1631 if (!chain)
1632 return for_each_chain(flush_entries, verbose, 1, handle);
1633
1634 if (verbose)
1635 fprintf(stdout, "Flushing chain `%s'\n", chain);
1636 return ip6tc_flush_entries(chain, handle);
1637}
1638
1639static int
1640zero_entries(const ip6t_chainlabel chain, int verbose,
1641 ip6tc_handle_t *handle)
1642{
1643 if (!chain)
1644 return for_each_chain(zero_entries, verbose, 1, handle);
1645
1646 if (verbose)
1647 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1648 return ip6tc_zero_entries(chain, handle);
1649}
1650
András Kis-Szabó764316a2001-02-26 17:31:20 +00001651int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001652delete_chain(const ip6t_chainlabel chain, int verbose,
1653 ip6tc_handle_t *handle)
1654{
1655 if (!chain)
1656 return for_each_chain(delete_chain, verbose, 0, handle);
1657
1658 if (verbose)
1659 fprintf(stdout, "Deleting chain `%s'\n", chain);
1660 return ip6tc_delete_chain(chain, handle);
1661}
1662
1663static int
1664list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1665 int expanded, int linenumbers, ip6tc_handle_t *handle)
1666{
1667 int found = 0;
1668 unsigned int format;
1669 const char *this;
1670
1671 format = FMT_OPTIONS;
1672 if (!verbose)
1673 format |= FMT_NOCOUNTS;
1674 else
1675 format |= FMT_VIA;
1676
1677 if (numeric)
1678 format |= FMT_NUMERIC;
1679
1680 if (!expanded)
1681 format |= FMT_KILOMEGAGIGA;
1682
1683 if (linenumbers)
1684 format |= FMT_LINENUMBERS;
1685
1686 for (this = ip6tc_first_chain(handle);
1687 this;
1688 this = ip6tc_next_chain(handle)) {
1689 const struct ip6t_entry *i;
1690 unsigned int num;
1691
1692 if (chain && strcmp(chain, this) != 0)
1693 continue;
1694
1695 if (found) printf("\n");
1696
1697 print_header(format, this, handle);
1698 i = ip6tc_first_rule(this, handle);
1699
1700 num = 0;
1701 while (i) {
1702 print_firewall(i,
1703 ip6tc_get_target(i, handle),
1704 num++,
1705 format,
1706 *handle);
1707 i = ip6tc_next_rule(i, handle);
1708 }
1709 found = 1;
1710 }
1711
1712 errno = ENOENT;
1713 return found;
1714}
1715
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001716static char *get_modprobe(void)
1717{
Harald Welte43ac1912002-03-03 09:43:07 +00001718 int procfile;
1719 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001720
Harald Welte10f7f142004-10-22 08:14:07 +00001721#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001722 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1723 if (procfile < 0)
1724 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001725
Harald Welte10f7f142004-10-22 08:14:07 +00001726 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001727 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001728 memset(ret, 0, PROCFILE_BUFSIZ);
1729 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001730 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001731 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001732 }
1733 if (ret[strlen(ret)-1]=='\n')
1734 ret[strlen(ret)-1]=0;
1735 close(procfile);
1736 return ret;
1737 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001738 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001739 free(ret);
1740 close(procfile);
1741 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001742}
1743
Harald Welte58918652001-06-16 18:25:25 +00001744int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001745{
Harald Welte43ac1912002-03-03 09:43:07 +00001746 char *buf = NULL;
1747 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001748 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001749
Harald Welte43ac1912002-03-03 09:43:07 +00001750 /* If they don't explicitly set it, read out of kernel */
1751 if (!modprobe) {
1752 buf = get_modprobe();
1753 if (!buf)
1754 return -1;
1755 modprobe = buf;
1756 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001757
Harald Welte43ac1912002-03-03 09:43:07 +00001758 switch (fork()) {
1759 case 0:
1760 argv[0] = (char *)modprobe;
1761 argv[1] = (char *)modname;
1762 argv[2] = NULL;
1763 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001764
Harald Welte43ac1912002-03-03 09:43:07 +00001765 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001766 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001767 case -1:
1768 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001769
Harald Welte43ac1912002-03-03 09:43:07 +00001770 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001771 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001772 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001773
Harald Welte43ac1912002-03-03 09:43:07 +00001774 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001775 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1776 return 0;
1777 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001778}
1779
Rusty Russell5eed48a2000-06-02 20:12:24 +00001780static struct ip6t_entry *
1781generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001782 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001783 struct ip6t_entry_target *target)
1784{
1785 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001786 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001787 struct ip6t_entry *e;
1788
1789 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001790 for (matchp = matches; matchp; matchp = matchp->next)
1791 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001792
1793 e = fw_malloc(size + target->u.target_size);
1794 *e = *fw;
1795 e->target_offset = size;
1796 e->next_offset = size + target->u.target_size;
1797
1798 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001799 for (matchp = matches; matchp; matchp = matchp->next) {
1800 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1801 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001802 }
1803 memcpy(e->elems + size, target, target->u.target_size);
1804
1805 return e;
1806}
1807
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001808void clear_rule_matches(struct ip6tables_rule_match **matches)
1809{
1810 struct ip6tables_rule_match *matchp, *tmp;
1811
1812 for (matchp = *matches; matchp;) {
1813 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001814 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001815 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001816 matchp->match->m = NULL;
1817 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001818 if (matchp->match == matchp->match->next) {
1819 free(matchp->match);
1820 matchp->match = NULL;
1821 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001822 free(matchp);
1823 matchp = tmp;
1824 }
1825
1826 *matches = NULL;
1827}
1828
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001829static void set_revision(char *name, u_int8_t revision)
1830{
1831 /* Old kernel sources don't have ".revision" field,
1832 but we stole a byte from name. */
1833 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1834 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1835}
1836
Rusty Russell5eed48a2000-06-02 20:12:24 +00001837int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1838{
1839 struct ip6t_entry fw, *e = NULL;
1840 int invert = 0;
1841 unsigned int nsaddrs = 0, ndaddrs = 0;
1842 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1843
1844 int c, verbose = 0;
1845 const char *chain = NULL;
1846 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1847 const char *policy = NULL, *newname = NULL;
1848 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001849 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001850 int ret = 1;
1851 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001852 struct ip6tables_rule_match *matches = NULL;
1853 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001854 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001855 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001856 const char *jumpto = "";
1857 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001858 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001859 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860
1861 memset(&fw, 0, sizeof(fw));
1862
Harald Welte43ac1912002-03-03 09:43:07 +00001863 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001864 * a second time */
1865 optind = 0;
1866
Harald Welte43ac1912002-03-03 09:43:07 +00001867 /* clear mflags in case do_command gets called a second time
1868 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001869 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001870 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001871
Harald Welte43ac1912002-03-03 09:43:07 +00001872 for (t = ip6tables_targets; t; t = t->next) {
1873 t->tflags = 0;
1874 t->used = 0;
1875 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001876
Rusty Russell5eed48a2000-06-02 20:12:24 +00001877 /* Suppress error messages: we may add new options if we
1878 demand-load a protocol. */
1879 opterr = 0;
1880
1881 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001882 "-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 +00001883 opts, NULL)) != -1) {
1884 switch (c) {
1885 /*
1886 * Command selection
1887 */
1888 case 'A':
1889 add_command(&command, CMD_APPEND, CMD_NONE,
1890 invert);
1891 chain = optarg;
1892 break;
1893
1894 case 'D':
1895 add_command(&command, CMD_DELETE, CMD_NONE,
1896 invert);
1897 chain = optarg;
1898 if (optind < argc && argv[optind][0] != '-'
1899 && argv[optind][0] != '!') {
1900 rulenum = parse_rulenumber(argv[optind++]);
1901 command = CMD_DELETE_NUM;
1902 }
1903 break;
1904
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905 case 'R':
1906 add_command(&command, CMD_REPLACE, CMD_NONE,
1907 invert);
1908 chain = optarg;
1909 if (optind < argc && argv[optind][0] != '-'
1910 && argv[optind][0] != '!')
1911 rulenum = parse_rulenumber(argv[optind++]);
1912 else
1913 exit_error(PARAMETER_PROBLEM,
1914 "-%c requires a rule number",
1915 cmd2char(CMD_REPLACE));
1916 break;
1917
1918 case 'I':
1919 add_command(&command, CMD_INSERT, CMD_NONE,
1920 invert);
1921 chain = optarg;
1922 if (optind < argc && argv[optind][0] != '-'
1923 && argv[optind][0] != '!')
1924 rulenum = parse_rulenumber(argv[optind++]);
1925 else rulenum = 1;
1926 break;
1927
1928 case 'L':
1929 add_command(&command, CMD_LIST, CMD_ZERO,
1930 invert);
1931 if (optarg) chain = optarg;
1932 else if (optind < argc && argv[optind][0] != '-'
1933 && argv[optind][0] != '!')
1934 chain = argv[optind++];
1935 break;
1936
1937 case 'F':
1938 add_command(&command, CMD_FLUSH, CMD_NONE,
1939 invert);
1940 if (optarg) chain = optarg;
1941 else if (optind < argc && argv[optind][0] != '-'
1942 && argv[optind][0] != '!')
1943 chain = argv[optind++];
1944 break;
1945
1946 case 'Z':
1947 add_command(&command, CMD_ZERO, CMD_LIST,
1948 invert);
1949 if (optarg) chain = optarg;
1950 else if (optind < argc && argv[optind][0] != '-'
1951 && argv[optind][0] != '!')
1952 chain = argv[optind++];
1953 break;
1954
1955 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001956 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001957 exit_error(PARAMETER_PROBLEM,
1958 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001959 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001960 if (find_target(optarg, TRY_LOAD))
1961 exit_error(PARAMETER_PROBLEM,
1962 "chain name may not clash "
1963 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001964 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1965 invert);
1966 chain = optarg;
1967 break;
1968
1969 case 'X':
1970 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1971 invert);
1972 if (optarg) chain = optarg;
1973 else if (optind < argc && argv[optind][0] != '-'
1974 && argv[optind][0] != '!')
1975 chain = argv[optind++];
1976 break;
1977
1978 case 'E':
1979 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1980 invert);
1981 chain = optarg;
1982 if (optind < argc && argv[optind][0] != '-'
1983 && argv[optind][0] != '!')
1984 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001985 else
1986 exit_error(PARAMETER_PROBLEM,
1987 "-%c requires old-chain-name and "
1988 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001989 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 break;
1991
1992 case 'P':
1993 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1994 invert);
1995 chain = optarg;
1996 if (optind < argc && argv[optind][0] != '-'
1997 && argv[optind][0] != '!')
1998 policy = argv[optind++];
1999 else
2000 exit_error(PARAMETER_PROBLEM,
2001 "-%c requires a chain and a policy",
2002 cmd2char(CMD_SET_POLICY));
2003 break;
2004
2005 case 'h':
2006 if (!optarg)
2007 optarg = argv[optind];
2008
Jonas Berlin1b91e592005-04-01 06:58:38 +00002009 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002010 if (!matches && protocol)
2011 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002012
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002013 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002014
2015 /*
2016 * Option selection
2017 */
2018 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002019 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002020 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2021 invert);
2022
2023 /* Canonicalize into lower case */
2024 for (protocol = argv[optind-1]; *protocol; protocol++)
2025 *protocol = tolower(*protocol);
2026
2027 protocol = argv[optind-1];
2028 fw.ipv6.proto = parse_protocol(protocol);
2029 fw.ipv6.flags |= IP6T_F_PROTO;
2030
2031 if (fw.ipv6.proto == 0
2032 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2033 exit_error(PARAMETER_PROBLEM,
2034 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00002035
2036 if (fw.ipv6.proto != IPPROTO_ESP &&
2037 is_exthdr(fw.ipv6.proto))
2038 printf("Warning: never matched protocol: %s. "
2039 "use exension match instead.", protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002040 break;
2041
2042 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002043 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002044 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2045 invert);
2046 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002047 break;
2048
2049 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002050 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002051 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2052 invert);
2053 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00002054 break;
2055
2056 case 'j':
2057 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2058 invert);
2059 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00002060 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 target = find_target(jumpto, TRY_LOAD);
2062
2063 if (target) {
2064 size_t size;
2065
Harald Welte43ac1912002-03-03 09:43:07 +00002066 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2067 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002068
2069 target->t = fw_calloc(1, size);
2070 target->t->u.target_size = size;
2071 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002072 if (target->init != NULL)
2073 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002074 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002075 }
2076 break;
2077
2078
2079 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002080 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002081 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2082 invert);
2083 parse_interface(argv[optind-1],
2084 fw.ipv6.iniface,
2085 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002086 break;
2087
2088 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002089 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002090 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2091 invert);
2092 parse_interface(argv[optind-1],
2093 fw.ipv6.outiface,
2094 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002095 break;
2096
2097 case 'v':
2098 if (!verbose)
2099 set_option(&options, OPT_VERBOSE,
2100 &fw.ipv6.invflags, invert);
2101 verbose++;
2102 break;
2103
2104 case 'm': {
2105 size_t size;
2106
2107 if (invert)
2108 exit_error(PARAMETER_PROBLEM,
2109 "unexpected ! flag before --match");
2110
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002111 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002112 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2113 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002114 m->m = fw_calloc(1, size);
2115 m->m->u.match_size = size;
2116 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002117 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002118 if (m->init != NULL)
2119 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002120 if (m != m->next)
2121 /* Merge options for non-cloned matches */
2122 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002123 }
2124 break;
2125
2126 case 'n':
2127 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2128 invert);
2129 break;
2130
2131 case 't':
2132 if (invert)
2133 exit_error(PARAMETER_PROBLEM,
2134 "unexpected ! flag before --table");
2135 *table = argv[optind-1];
2136 break;
2137
2138 case 'x':
2139 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2140 invert);
2141 break;
2142
2143 case 'V':
2144 if (invert)
2145 printf("Not %s ;-)\n", program_version);
2146 else
2147 printf("%s v%s\n",
2148 program_name, program_version);
2149 exit(0);
2150
2151 case '0':
2152 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2153 invert);
2154 break;
2155
Harald Welte43ac1912002-03-03 09:43:07 +00002156 case 'M':
2157 modprobe = optarg;
2158 break;
2159
2160 case 'c':
2161
2162 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2163 invert);
2164 pcnt = optarg;
2165 if (optind < argc && argv[optind][0] != '-'
2166 && argv[optind][0] != '!')
2167 bcnt = argv[optind++];
2168 else
2169 exit_error(PARAMETER_PROBLEM,
2170 "-%c requires packet and byte counter",
2171 opt2char(OPT_COUNTERS));
2172
Martin Josefssona28d4952004-05-26 16:04:48 +00002173 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002174 exit_error(PARAMETER_PROBLEM,
2175 "-%c packet counter not numeric",
2176 opt2char(OPT_COUNTERS));
2177
Martin Josefssona28d4952004-05-26 16:04:48 +00002178 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002179 exit_error(PARAMETER_PROBLEM,
2180 "-%c byte counter not numeric",
2181 opt2char(OPT_COUNTERS));
2182
2183 break;
2184
2185
Rusty Russell5eed48a2000-06-02 20:12:24 +00002186 case 1: /* non option */
2187 if (optarg[0] == '!' && optarg[1] == '\0') {
2188 if (invert)
2189 exit_error(PARAMETER_PROBLEM,
2190 "multiple consecutive ! not"
2191 " allowed");
2192 invert = TRUE;
2193 optarg[0] = '\0';
2194 continue;
2195 }
2196 printf("Bad argument `%s'\n", optarg);
2197 exit_tryhelp(2);
2198
2199 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002200 if (!target
2201 || !(target->parse(c - target->option_offset,
2202 argv, invert,
2203 &target->tflags,
2204 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002205 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002206 if (matchp->completed)
2207 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002208 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002209 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002210 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002211 &fw,
2212 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002213 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002214 break;
2215 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002216 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002217
2218 /* If you listen carefully, you can
2219 actually hear this code suck. */
2220
2221 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002222 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002223 * parameter, that has not been parsed yet,
2224 * it's not an option of an explicitly loaded
2225 * match or a target. However, we support
2226 * implicit loading of the protocol match
2227 * extension. '-p tcp' means 'l4 proto 6' and
2228 * at the same time 'load tcp protocol match on
2229 * demand if we specify --dport'.
2230 *
2231 * To make this work, we need to make sure:
2232 * - the parameter has not been parsed by
2233 * a match (m above)
2234 * - a protocol has been specified
2235 * - the protocol extension has not been
2236 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002237 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002238 * - the protocol extension can be successively
2239 * loaded
2240 */
2241 if (m == NULL
2242 && protocol
2243 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002244 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002245 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002246 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002247 && (proto_used == 0))
2248 )
2249 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002250 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002251 /* Try loading protocol */
2252 size_t size;
2253
2254 proto_used = 1;
2255
2256 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2257 + m->size;
2258
2259 m->m = fw_calloc(1, size);
2260 m->m->u.match_size = size;
2261 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002262 set_revision(m->m->u.user.name,
2263 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002264 if (m->init != NULL)
2265 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002266
2267 opts = merge_options(opts,
2268 m->extra_opts, &m->option_offset);
2269
2270 optind--;
2271 continue;
2272 }
2273
Rusty Russell5eed48a2000-06-02 20:12:24 +00002274 if (!m)
2275 exit_error(PARAMETER_PROBLEM,
2276 "Unknown arg `%s'",
2277 argv[optind-1]);
2278 }
2279 }
2280 invert = FALSE;
2281 }
2282
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002283 for (matchp = matches; matchp; matchp = matchp->next)
2284 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002285
Harald Welte43ac1912002-03-03 09:43:07 +00002286 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002287 target->final_check(target->tflags);
2288
2289 /* Fix me: must put inverse options checking here --MN */
2290
2291 if (optind < argc)
2292 exit_error(PARAMETER_PROBLEM,
2293 "unknown arguments found on commandline");
2294 if (!command)
2295 exit_error(PARAMETER_PROBLEM, "no command specified");
2296 if (invert)
2297 exit_error(PARAMETER_PROBLEM,
2298 "nothing appropriate following !");
2299
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002300 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002301 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002302 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002303 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002304 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002305 }
2306
2307 if (shostnetworkmask)
2308 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2309 &(fw.ipv6.smsk), &nsaddrs);
2310
2311 if (dhostnetworkmask)
2312 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2313 &(fw.ipv6.dmsk), &ndaddrs);
2314
2315 if ((nsaddrs > 1 || ndaddrs > 1) &&
2316 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2317 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2318 " source or destination IP addresses");
2319
Rusty Russell5eed48a2000-06-02 20:12:24 +00002320 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2321 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2322 "specify a unique address");
2323
2324 generic_opt_check(command, options);
2325
2326 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2327 exit_error(PARAMETER_PROBLEM,
2328 "chain name `%s' too long (must be under %i chars)",
2329 chain, IP6T_FUNCTION_MAXNAMELEN);
2330
Harald Welte43ac1912002-03-03 09:43:07 +00002331 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002332 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002333 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002334
Rusty Russell8beb0492004-12-22 00:37:10 +00002335 /* try to insmod the module if iptc_init failed */
2336 if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002337 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002338
Harald Welte43ac1912002-03-03 09:43:07 +00002339 if (!*handle)
2340 exit_error(VERSION_PROBLEM,
2341 "can't initialize ip6tables table `%s': %s",
2342 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002343
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002344 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002345 || command == CMD_DELETE
2346 || command == CMD_INSERT
2347 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002348 if (strcmp(chain, "PREROUTING") == 0
2349 || strcmp(chain, "INPUT") == 0) {
2350 /* -o not valid with incoming packets. */
2351 if (options & OPT_VIANAMEOUT)
2352 exit_error(PARAMETER_PROBLEM,
2353 "Can't use -%c with %s\n",
2354 opt2char(OPT_VIANAMEOUT),
2355 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002356 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002357
Harald Welte43ac1912002-03-03 09:43:07 +00002358 if (strcmp(chain, "POSTROUTING") == 0
2359 || strcmp(chain, "OUTPUT") == 0) {
2360 /* -i not valid with outgoing packets */
2361 if (options & OPT_VIANAMEIN)
2362 exit_error(PARAMETER_PROBLEM,
2363 "Can't use -%c with %s\n",
2364 opt2char(OPT_VIANAMEIN),
2365 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002366 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002367
2368 if (target && ip6tc_is_chain(jumpto, *handle)) {
2369 printf("Warning: using chain %s, not extension\n",
2370 jumpto);
2371
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002372 if (target->t)
2373 free(target->t);
2374
Rusty Russell5eed48a2000-06-02 20:12:24 +00002375 target = NULL;
2376 }
2377
2378 /* If they didn't specify a target, or it's a chain
2379 name, use standard. */
2380 if (!target
2381 && (strlen(jumpto) == 0
2382 || ip6tc_is_chain(jumpto, *handle))) {
2383 size_t size;
2384
2385 target = find_target(IP6T_STANDARD_TARGET,
2386 LOAD_MUST_SUCCEED);
2387
2388 size = sizeof(struct ip6t_entry_target)
2389 + target->size;
2390 target->t = fw_calloc(1, size);
2391 target->t->u.target_size = size;
2392 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002393 if (target->init != NULL)
2394 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002395 }
2396
2397 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002398 /* it is no chain, and we can't load a plugin.
2399 * We cannot know if the plugin is corrupt, non
2400 * existant OR if the user just misspelled a
2401 * chain. */
2402 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002403 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002404 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002405 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002406 }
2407 }
2408
2409 switch (command) {
2410 case CMD_APPEND:
2411 ret = append_entry(chain, e,
2412 nsaddrs, saddrs, ndaddrs, daddrs,
2413 options&OPT_VERBOSE,
2414 handle);
2415 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002416 case CMD_DELETE:
2417 ret = delete_entry(chain, e,
2418 nsaddrs, saddrs, ndaddrs, daddrs,
2419 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002420 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002421 break;
2422 case CMD_DELETE_NUM:
2423 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2424 break;
2425 case CMD_REPLACE:
2426 ret = replace_entry(chain, e, rulenum - 1,
2427 saddrs, daddrs, options&OPT_VERBOSE,
2428 handle);
2429 break;
2430 case CMD_INSERT:
2431 ret = insert_entry(chain, e, rulenum - 1,
2432 nsaddrs, saddrs, ndaddrs, daddrs,
2433 options&OPT_VERBOSE,
2434 handle);
2435 break;
2436 case CMD_LIST:
2437 ret = list_entries(chain,
2438 options&OPT_VERBOSE,
2439 options&OPT_NUMERIC,
2440 options&OPT_EXPANDED,
2441 options&OPT_LINENUMBERS,
2442 handle);
2443 break;
2444 case CMD_FLUSH:
2445 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2446 break;
2447 case CMD_ZERO:
2448 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2449 break;
2450 case CMD_LIST|CMD_ZERO:
2451 ret = list_entries(chain,
2452 options&OPT_VERBOSE,
2453 options&OPT_NUMERIC,
2454 options&OPT_EXPANDED,
2455 options&OPT_LINENUMBERS,
2456 handle);
2457 if (ret)
2458 ret = zero_entries(chain,
2459 options&OPT_VERBOSE, handle);
2460 break;
2461 case CMD_NEW_CHAIN:
2462 ret = ip6tc_create_chain(chain, handle);
2463 break;
2464 case CMD_DELETE_CHAIN:
2465 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2466 break;
2467 case CMD_RENAME_CHAIN:
2468 ret = ip6tc_rename_chain(chain, newname, handle);
2469 break;
2470 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002471 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002472 break;
2473 default:
2474 /* We should never reach this... */
2475 exit_tryhelp(2);
2476 }
2477
2478 if (verbose > 1)
2479 dump_entries6(*handle);
2480
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002481 clear_rule_matches(&matches);
2482
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002483 if (e != NULL) {
2484 free(e);
2485 e = NULL;
2486 }
2487
2488 for (c = 0; c < nsaddrs; c++)
2489 free(&saddrs[c]);
2490
2491 for (c = 0; c < ndaddrs; c++)
2492 free(&daddrs[c]);
2493
Pablo Neiradfdcd642005-05-29 19:05:23 +00002494 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002495
Rusty Russell5eed48a2000-06-02 20:12:24 +00002496 return ret;
2497}