blob: 2a06bc04c401554ecb583cb827a7597ddbc3bfaa [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>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
36#include <limits.h>
37#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000039#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000042#include <sys/types.h>
43#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Rusty Russell5eed48a2000-06-02 20:12:24 +000052#define FMT_NUMERIC 0x0001
53#define FMT_NOCOUNTS 0x0002
54#define FMT_KILOMEGAGIGA 0x0004
55#define FMT_OPTIONS 0x0008
56#define FMT_NOTABLE 0x0010
57#define FMT_NOTARGET 0x0020
58#define FMT_VIA 0x0040
59#define FMT_NONEWLINE 0x0080
60#define FMT_LINENUMBERS 0x0100
61
62#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
63 | FMT_NUMERIC | FMT_NOTABLE)
64#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
65
66
67#define CMD_NONE 0x0000U
68#define CMD_INSERT 0x0001U
69#define CMD_DELETE 0x0002U
70#define CMD_DELETE_NUM 0x0004U
71#define CMD_REPLACE 0x0008U
72#define CMD_APPEND 0x0010U
73#define CMD_LIST 0x0020U
74#define CMD_FLUSH 0x0040U
75#define CMD_ZERO 0x0080U
76#define CMD_NEW_CHAIN 0x0100U
77#define CMD_DELETE_CHAIN 0x0200U
78#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000079#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000080#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000082 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000083
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000097#define OPT_COUNTERS 0x00400U
98#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +000099static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000100= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000101
102static struct option original_opts[] = {
103 { "append", 1, 0, 'A' },
104 { "delete", 1, 0, 'D' },
105 { "insert", 1, 0, 'I' },
106 { "replace", 1, 0, 'R' },
107 { "list", 2, 0, 'L' },
108 { "flush", 2, 0, 'F' },
109 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000110 { "new-chain", 1, 0, 'N' },
111 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000112 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000113 { "policy", 1, 0, 'P' },
114 { "source", 1, 0, 's' },
115 { "destination", 1, 0, 'd' },
116 { "src", 1, 0, 's' }, /* synonym */
117 { "dst", 1, 0, 'd' }, /* synonym */
118 { "protocol", 1, 0, 'p' },
119 { "in-interface", 1, 0, 'i' },
120 { "jump", 1, 0, 'j' },
121 { "table", 1, 0, 't' },
122 { "match", 1, 0, 'm' },
123 { "numeric", 0, 0, 'n' },
124 { "out-interface", 1, 0, 'o' },
125 { "verbose", 0, 0, 'v' },
126 { "exact", 0, 0, 'x' },
127 { "version", 0, 0, 'V' },
128 { "help", 2, 0, 'h' },
129 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000130 { "modprobe", 1, 0, 'M' },
131 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000132 { 0 }
133};
134
Harald Weltea8658ca2003-03-05 07:46:15 +0000135/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
136 * current line of the input file, in order to give a more precise error
137 * message. ip6tables itself doesn't need this, so it is initialized to the
138 * magic number of -1 */
139int line = -1;
140
Rusty Russell5eed48a2000-06-02 20:12:24 +0000141static struct option *opts = original_opts;
142static unsigned int global_option_offset = 0;
143
144/* Table of legal combinations of commands and options. If any of the
145 * given commands make an option legal, that option is legal (applies to
146 * CMD_LIST and CMD_ZERO only).
147 * Key:
148 * + compulsory
149 * x illegal
150 * optional
151 */
152
153static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
154/* Well, it's better than "Re: Linux vs FreeBSD" */
155{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000156 /* -n -s -d -p -j -v -x -i -o --line -c */
157/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
158/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
159/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
160/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
161/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
163/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
164/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
166/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000168/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000169};
170
171static int inverse_for_options[NUMBER_OF_OPT] =
172{
173/* -n */ 0,
174/* -s */ IP6T_INV_SRCIP,
175/* -d */ IP6T_INV_DSTIP,
176/* -p */ IP6T_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IP6T_INV_VIA_IN,
181/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000182/*--line*/ 0,
183/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184};
185
186const char *program_version;
187const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000188char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000189
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190/* Keeping track of external matches and targets: linked lists. */
191struct ip6tables_match *ip6tables_matches = NULL;
192struct ip6tables_target *ip6tables_targets = NULL;
193
194/* Extra debugging from libiptc */
195extern void dump_entries6(const ip6tc_handle_t handle);
196
197/* A few hardcoded protocols for 'all' and in case the user has no
198 /etc/protocols */
199struct pprot {
200 char *name;
201 u_int8_t num;
202};
203
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000204/* Primitive headers... */
205/* defined in netinet/in.h */
206#if 0
207#ifndef IPPROTO_ESP
208#define IPPROTO_ESP 50
209#endif
210#ifndef IPPROTO_AH
211#define IPPROTO_AH 51
212#endif
213#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000214#ifndef IPPROTO_MH
215#define IPPROTO_MH 135
216#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000217
Rusty Russell5eed48a2000-06-02 20:12:24 +0000218static const struct pprot chain_protos[] = {
219 { "tcp", IPPROTO_TCP },
220 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000221 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000222 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000223 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000226 { "ipv6-mh", IPPROTO_MH },
227 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000228 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000229};
230
231static char *
232proto_to_name(u_int8_t proto, int nolookup)
233{
234 unsigned int i;
235
236 if (proto && !nolookup) {
237 struct protoent *pent = getprotobynumber(proto);
238 if (pent)
239 return pent->p_name;
240 }
241
242 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
243 if (chain_protos[i].num == proto)
244 return chain_protos[i].name;
245
246 return NULL;
247}
248
Phil Oester58179b12006-07-20 17:00:19 +0000249int
250service_to_port(const char *name, const char *proto)
251{
252 struct servent *service;
253
254 if ((service = getservbyname(name, proto)) != NULL)
255 return ntohs((unsigned short) service->s_port);
256
257 return -1;
258}
259
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000260u_int16_t
261parse_port(const char *port, const char *proto)
262{
263 unsigned int portnum;
264
265 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
266 (portnum = service_to_port(port, proto)) != -1)
267 return (u_int16_t)portnum;
268
269 exit_error(PARAMETER_PROBLEM,
270 "invalid port/service `%s' specified", port);
271}
272
Rusty Russell5eed48a2000-06-02 20:12:24 +0000273static void
274in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
275{
276 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000277 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278}
279
Pablo Neiradfdcd642005-05-29 19:05:23 +0000280static void free_opts(int reset_offset)
281{
282 if (opts != original_opts) {
283 free(opts);
284 opts = original_opts;
285 if (reset_offset)
286 global_option_offset = 0;
287 }
288}
289
Rusty Russell5eed48a2000-06-02 20:12:24 +0000290void
291exit_error(enum exittype status, char *msg, ...)
292{
293 va_list args;
294
295 va_start(args, msg);
296 fprintf(stderr, "%s v%s: ", program_name, program_version);
297 vfprintf(stderr, msg, args);
298 va_end(args);
299 fprintf(stderr, "\n");
300 if (status == PARAMETER_PROBLEM)
301 exit_tryhelp(status);
302 if (status == VERSION_PROBLEM)
303 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000304 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000305 /* On error paths, make sure that we don't leak memory */
306 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000307 exit(status);
308}
309
310void
311exit_tryhelp(int status)
312{
Harald Weltea8658ca2003-03-05 07:46:15 +0000313 if (line != -1)
314 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000315 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
316 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000317 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000318 exit(status);
319}
320
321void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000322exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000323{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000324 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000325 struct ip6tables_target *t = NULL;
326
327 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000328"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000329" %s -[RI] chain rulenum rule-specification [options]\n"
330" %s -D chain rulenum [options]\n"
331" %s -[LFZ] [chain] [options]\n"
332" %s -[NX] chain\n"
333" %s -E old-chain-name new-chain-name\n"
334" %s -P chain target [options]\n"
335" %s -h (print this help information)\n\n",
336 program_name, program_version, program_name, program_name,
337 program_name, program_name, program_name, program_name,
338 program_name, program_name);
339
340 printf(
341"Commands:\n"
342"Either long or short options are allowed.\n"
343" --append -A chain Append to chain\n"
344" --delete -D chain Delete matching rule from chain\n"
345" --delete -D chain rulenum\n"
346" Delete rule rulenum (1 = first) from chain\n"
347" --insert -I chain [rulenum]\n"
348" Insert in chain as rulenum (default 1=first)\n"
349" --replace -R chain rulenum\n"
350" Replace rule rulenum (1 = first) in chain\n"
351" --list -L [chain] List the rules in a chain or all chains\n"
352" --flush -F [chain] Delete all rules in chain or all chains\n"
353" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000354" --new -N chain Create a new user-defined chain\n"
355" --delete-chain\n"
356" -X [chain] Delete a user-defined chain\n"
357" --policy -P chain target\n"
358" Change policy on chain to target\n"
359" --rename-chain\n"
360" -E old-chain new-chain\n"
361" Change chain name, (moving any references)\n"
362
363"Options:\n"
364" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
365" --source -s [!] address[/mask]\n"
366" source specification\n"
367" --destination -d [!] address[/mask]\n"
368" destination specification\n"
369" --in-interface -i [!] input name[+]\n"
370" network interface name ([+] for wildcard)\n"
371" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000372" target for rule (may load target extension)\n"
373" --match -m match\n"
374" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000375" --numeric -n numeric output of addresses and ports\n"
376" --out-interface -o [!] output name[+]\n"
377" network interface name ([+] for wildcard)\n"
378" --table -t table table to manipulate (default: `filter')\n"
379" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000380" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000381" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000382/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000383" --modprobe=<command> try to insert modules using this command\n"
384" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000385"[!] --version -V print package version.\n");
386
387 /* Print out any special helps. A user might like to be able to add a --help
388 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000389 specified matches & targets */
390 for (t = ip6tables_targets; t; t = t->next) {
391 if (t->used) {
392 printf("\n");
393 t->help();
394 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000395 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000396 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000397 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000398 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000399 }
400 exit(0);
401}
402
403static void
404generic_opt_check(int command, int options)
405{
406 int i, j, legal = 0;
407
408 /* Check that commands are valid with options. Complicated by the
409 * fact that if an option is legal with *any* command given, it is
410 * legal overall (ie. -z and -l).
411 */
412 for (i = 0; i < NUMBER_OF_OPT; i++) {
413 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
414
415 for (j = 0; j < NUMBER_OF_CMD; j++) {
416 if (!(command & (1<<j)))
417 continue;
418
419 if (!(options & (1<<i))) {
420 if (commands_v_options[j][i] == '+')
421 exit_error(PARAMETER_PROBLEM,
422 "You need to supply the `-%c' "
423 "option for this command\n",
424 optflags[i]);
425 } else {
426 if (commands_v_options[j][i] != 'x')
427 legal = 1;
428 else if (legal == 0)
429 legal = -1;
430 }
431 }
432 if (legal == -1)
433 exit_error(PARAMETER_PROBLEM,
434 "Illegal option `-%c' with this command\n",
435 optflags[i]);
436 }
437}
438
439static char
440opt2char(int option)
441{
442 const char *ptr;
443 for (ptr = optflags; option > 1; option >>= 1, ptr++);
444
445 return *ptr;
446}
447
448static char
449cmd2char(int option)
450{
451 const char *ptr;
452 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
453
454 return *ptr;
455}
456
457static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000458add_command(unsigned int *cmd, const int newcmd, const int othercmds,
459 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460{
461 if (invert)
462 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
463 if (*cmd & (~othercmds))
464 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
465 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
466 *cmd |= newcmd;
467}
468
469int
Harald Welteb77f1da2002-03-14 11:35:58 +0000470check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471{
472 if (option && strcmp(option, "!") == 0) {
473 if (*invert)
474 exit_error(PARAMETER_PROBLEM,
475 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000476 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000477 if (optind) {
478 *optind = *optind+1;
479 if (argc && *optind > argc)
480 exit_error(PARAMETER_PROBLEM,
481 "no argument following `!'");
482 }
483
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484 return TRUE;
485 }
486 return FALSE;
487}
488
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489static char *
490addr_to_numeric(const struct in6_addr *addrp)
491{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000492 /* 0000:0000:0000:0000:0000:000.000.000.000
493 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000494 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000495 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000496}
497
498static struct in6_addr *
499numeric_to_addr(const char *num)
500{
501 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000502 int err;
503 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000504 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000505#ifdef DEBUG
506 fprintf(stderr, "\nnumeric2addr: %d\n", err);
507#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000508 return (struct in6_addr *)NULL;
509}
510
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000511
512static struct in6_addr *
513host_to_addr(const char *name, unsigned int *naddr)
514{
515 struct addrinfo hints;
516 struct addrinfo *res;
517 static struct in6_addr *addr;
518 int err;
519
520 memset(&hints, 0, sizeof(hints));
521 hints.ai_flags=AI_CANONNAME;
522 hints.ai_family=AF_INET6;
523 hints.ai_socktype=SOCK_RAW;
524 hints.ai_protocol=41;
525 hints.ai_next=NULL;
526
527 *naddr = 0;
528 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
529#ifdef DEBUG
530 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
531#endif
532 return (struct in6_addr *) NULL;
533 } else {
534 if (res->ai_family != AF_INET6 ||
535 res->ai_addrlen != sizeof(struct sockaddr_in6))
536 return (struct in6_addr *) NULL;
537
538#ifdef DEBUG
539 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
540 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
541#endif
542 /* Get the first element of the address-chain */
543 addr = fw_calloc(1, sizeof(struct in6_addr));
544 in6addrcpy(addr, (struct in6_addr *)
545 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
546 freeaddrinfo(res);
547 *naddr = 1;
548 return addr;
549 }
550
551 return (struct in6_addr *) NULL;
552}
553
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000554static char *
555addr_to_host(const struct in6_addr *addr)
556{
557 struct sockaddr_in6 saddr;
558 int err;
559 static char hostname[NI_MAXHOST];
560
561 memset(&saddr, 0, sizeof(struct sockaddr_in6));
562 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000563 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000564
565 if ( (err=getnameinfo((struct sockaddr *)&saddr,
566 sizeof(struct sockaddr_in6),
567 hostname, sizeof(hostname)-1,
568 NULL, 0, 0)) != 0 ){
569#ifdef DEBUG
570 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
571#endif
572 return (char *) NULL;
573 } else {
574#ifdef DEBUG
575 fprintf (stderr, "\naddr2host: %s\n", hostname);
576#endif
577
578 return hostname;
579 }
580
581 return (char *) NULL;
582}
583
Rusty Russell5eed48a2000-06-02 20:12:24 +0000584static char *
585mask_to_numeric(const struct in6_addr *addrp)
586{
Harald Welte8a50ab82003-06-24 18:15:59 +0000587 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000588 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000589 if (l == -1) {
590 strcpy(buf, "/");
591 strcat(buf, addr_to_numeric(addrp));
592 return buf;
593 }
Harald Welte43ac1912002-03-03 09:43:07 +0000594 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000595 return buf;
596}
597
598static struct in6_addr *
599network_to_addr(const char *name)
600{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000601 /* abort();*/
602 /* TODO: not implemented yet, but the exception breaks the
603 * name resolvation */
604 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000605}
606
607static char *
608addr_to_anyname(const struct in6_addr *addr)
609{
610 char *name;
611
612 if ((name = addr_to_host(addr)) != NULL)
613 return name;
614
615 return addr_to_numeric(addr);
616}
617
618/*
619 * All functions starting with "parse" should succeed, otherwise
620 * the program fails.
621 * Most routines return pointers to static data that may change
622 * between calls to the same or other routines with a few exceptions:
623 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
624 * return global static data.
625*/
626
627static struct in6_addr *
628parse_hostnetwork(const char *name, unsigned int *naddrs)
629{
630 struct in6_addr *addrp, *addrptmp;
631
632 if ((addrptmp = numeric_to_addr(name)) != NULL ||
633 (addrptmp = network_to_addr(name)) != NULL) {
634 addrp = fw_malloc(sizeof(struct in6_addr));
635 in6addrcpy(addrp, addrptmp);
636 *naddrs = 1;
637 return addrp;
638 }
639 if ((addrp = host_to_addr(name, naddrs)) != NULL)
640 return addrp;
641
642 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
643}
644
645static struct in6_addr *
646parse_mask(char *mask)
647{
648 static struct in6_addr maskaddr;
649 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000650 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000651
652 if (mask == NULL) {
653 /* no mask at all defaults to 128 bits */
654 memset(&maskaddr, 0xff, sizeof maskaddr);
655 return &maskaddr;
656 }
657 if ((addrp = numeric_to_addr(mask)) != NULL)
658 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000659 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660 exit_error(PARAMETER_PROBLEM,
661 "invalid mask `%s' specified", mask);
662 if (bits != 0) {
663 char *p = (char *)&maskaddr;
664 memset(p, 0xff, bits / 8);
665 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
666 p[bits / 8] = 0xff << (8 - (bits & 7));
667 return &maskaddr;
668 }
669
670 memset(&maskaddr, 0, sizeof maskaddr);
671 return &maskaddr;
672}
673
Harald Welte43ac1912002-03-03 09:43:07 +0000674void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000675parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
676 struct in6_addr *maskp, unsigned int *naddrs)
677{
678 struct in6_addr *addrp;
679 char buf[256];
680 char *p;
681 int i, j, n;
682
683 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000684 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000685 if ((p = strrchr(buf, '/')) != NULL) {
686 *p = '\0';
687 addrp = parse_mask(p + 1);
688 } else
689 addrp = parse_mask(NULL);
690 in6addrcpy(maskp, addrp);
691
692 /* if a null mask is given, the name is ignored, like in "any/0" */
693 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
694 strcpy(buf, "::");
695
696 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
697 n = *naddrs;
698 for (i = 0, j = 0; i < n; i++) {
699 int k;
700 for (k = 0; k < 4; k++)
701 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
702 j++;
703 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000704 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000705 (*naddrs)--;
706 j--;
707 break;
708 }
709 }
710 }
711}
712
713struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000714find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000715{
716 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000717 const char *icmp6 = "icmp6";
718 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000719
720 /* This is ugly as hell. Nonetheless, there is no way of changing
721 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000722 if ( (strcmp(match_name,"icmpv6") == 0) ||
723 (strcmp(match_name,"ipv6-icmp") == 0) ||
724 (strcmp(match_name,"icmp6") == 0) )
725 name = icmp6;
726 else
727 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000728
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000729 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
730 if (strcmp(name, ptr->name) == 0) {
731 struct ip6tables_match *clone;
732
733 /* First match of this type: */
734 if (ptr->m == NULL)
735 break;
736
737 /* Second and subsequent clones */
738 clone = fw_malloc(sizeof(struct ip6tables_match));
739 memcpy(clone, ptr, sizeof(struct ip6tables_match));
740 clone->mflags = 0;
741 /* This is a clone: */
742 clone->next = clone;
743
744 ptr = clone;
745 break;
746 }
747 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748
Harald Welte3efb6ea2001-08-06 18:50:21 +0000749#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000750 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000751 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000752 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000753 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000754 if (dlopen(path, RTLD_NOW)) {
755 /* Found library. If it didn't register itself,
756 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000757 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000758
759 if (!ptr)
760 exit_error(PARAMETER_PROBLEM,
761 "Couldn't load match `%s'\n",
762 name);
763 } else if (tryload == LOAD_MUST_SUCCEED)
764 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000765 "Couldn't load match `%s':%s\n",
766 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000767 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000768#else
769 if (ptr && !ptr->loaded) {
770 if (tryload != DONT_LOAD)
771 ptr->loaded = 1;
772 else
773 ptr = NULL;
774 }
Marc Boucher067477b2002-03-24 15:09:31 +0000775 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
776 exit_error(PARAMETER_PROBLEM,
777 "Couldn't find match `%s'\n", name);
778 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000779#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000780
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000781 if (ptr && matches) {
782 struct ip6tables_rule_match **i;
783 struct ip6tables_rule_match *newentry;
784
785 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
786
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000787 for (i = matches; *i; i = &(*i)->next) {
788 if (strcmp(name, (*i)->match->name) == 0)
789 (*i)->completed = 1;
790 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000791 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000792 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000793 newentry->next = NULL;
794 *i = newentry;
795 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000796
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797 return ptr;
798}
799
800/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
801static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000802find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803{
Harald Welteed498492001-07-23 01:24:22 +0000804 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805
Harald Welte43ac1912002-03-03 09:43:07 +0000806 if (string_to_number(pname, 0, 255, &proto) != -1) {
807 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808
Harald Welte43ac1912002-03-03 09:43:07 +0000809 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000810 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000811 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000812 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000813
814 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000815}
816
Harald Welte43ac1912002-03-03 09:43:07 +0000817u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818parse_protocol(const char *s)
819{
Harald Welteed498492001-07-23 01:24:22 +0000820 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000821
Harald Welteed498492001-07-23 01:24:22 +0000822 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000823 struct protoent *pent;
824
Harald Weltecbe1ec72006-02-11 09:50:11 +0000825 /* first deal with the special case of 'all' to prevent
826 * people from being able to redefine 'all' in nsswitch
827 * and/or provoke expensive [not working] ldap/nis/...
828 * lookups */
829 if (!strcmp(s, "all"))
830 return 0;
831
Rusty Russell5eed48a2000-06-02 20:12:24 +0000832 if ((pent = getprotobyname(s)))
833 proto = pent->p_proto;
834 else {
835 unsigned int i;
836 for (i = 0;
837 i < sizeof(chain_protos)/sizeof(struct pprot);
838 i++) {
839 if (strcmp(s, chain_protos[i].name) == 0) {
840 proto = chain_protos[i].num;
841 break;
842 }
843 }
844 if (i == sizeof(chain_protos)/sizeof(struct pprot))
845 exit_error(PARAMETER_PROBLEM,
846 "unknown protocol `%s' specified",
847 s);
848 }
849 }
850
851 return (u_int16_t)proto;
852}
853
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000854/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000855static int is_exthdr(u_int16_t proto)
856{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000857 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000858 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000859 proto == IPPROTO_AH ||
860 proto == IPPROTO_DSTOPTS);
861}
862
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000863void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864{
865 int vialen = strlen(arg);
866 unsigned int i;
867
868 memset(mask, 0, IFNAMSIZ);
869 memset(vianame, 0, IFNAMSIZ);
870
871 if (vialen + 1 > IFNAMSIZ)
872 exit_error(PARAMETER_PROBLEM,
873 "interface name `%s' must be shorter than IFNAMSIZ"
874 " (%i)", arg, IFNAMSIZ-1);
875
876 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000877 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878 memset(mask, 0, IFNAMSIZ);
879 else if (vianame[vialen - 1] == '+') {
880 memset(mask, 0xFF, vialen - 1);
881 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000882 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000883 } else {
884 /* Include nul-terminator in match */
885 memset(mask, 0xFF, vialen + 1);
886 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000887 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000888 if (vianame[i] == ':' ||
889 vianame[i] == '!' ||
890 vianame[i] == '*') {
891 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000892 " `%s' (No aliases, :, ! or *).\n",
893 vianame);
894 break;
895 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896 }
897 }
898}
899
900/* Can't be zero. */
901static int
902parse_rulenumber(const char *rule)
903{
Harald Welte43ac1912002-03-03 09:43:07 +0000904 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000905
Harald Welte43ac1912002-03-03 09:43:07 +0000906 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000907 exit_error(PARAMETER_PROBLEM,
908 "Invalid rule number `%s'", rule);
909
910 return rulenum;
911}
912
913static const char *
914parse_target(const char *targetname)
915{
916 const char *ptr;
917
918 if (strlen(targetname) < 1)
919 exit_error(PARAMETER_PROBLEM,
920 "Invalid target name (too short)");
921
922 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
923 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000924 "Invalid target name `%s' (%u chars max)",
925 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000926
927 for (ptr = targetname; *ptr; ptr++)
928 if (isspace(*ptr))
929 exit_error(PARAMETER_PROBLEM,
930 "Invalid target name `%s'", targetname);
931 return targetname;
932}
933
934int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000935string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
936 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000937{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000938 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000939 char *end;
940
941 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000942 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000943 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000944 if (*end == '\0' && end != s) {
945 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000946 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000947 *ret = number;
948 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000949 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950 }
951 return -1;
952}
953
Martin Josefssonb105bc92004-05-26 15:54:49 +0000954int
955string_to_number_l(const char *s, unsigned long min, unsigned long max,
956 unsigned long *ret)
957{
958 int result;
959 unsigned long long number;
960
961 result = string_to_number_ll(s, min, max, &number);
962 *ret = (unsigned long)number;
963
964 return result;
965}
966
967int string_to_number(const char *s, unsigned int min, unsigned int max,
968 unsigned int *ret)
969{
970 int result;
971 unsigned long number;
972
973 result = string_to_number_l(s, min, max, &number);
974 *ret = (unsigned int)number;
975
976 return result;
977}
978
Rusty Russell5eed48a2000-06-02 20:12:24 +0000979static void
980set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
981 int invert)
982{
983 if (*options & option)
984 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
985 opt2char(option));
986 *options |= option;
987
988 if (invert) {
989 unsigned int i;
990 for (i = 0; 1 << i != option; i++);
991
992 if (!inverse_for_options[i])
993 exit_error(PARAMETER_PROBLEM,
994 "cannot have ! before -%c",
995 opt2char(option));
996 *invflg |= inverse_for_options[i];
997 }
998}
999
1000struct ip6tables_target *
1001find_target(const char *name, enum ip6t_tryload tryload)
1002{
1003 struct ip6tables_target *ptr;
1004
1005 /* Standard target? */
1006 if (strcmp(name, "") == 0
1007 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1008 || strcmp(name, IP6TC_LABEL_DROP) == 0
1009 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1010 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1011 name = "standard";
1012
1013 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1014 if (strcmp(name, ptr->name) == 0)
1015 break;
1016 }
1017
Harald Welte3efb6ea2001-08-06 18:50:21 +00001018#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001019 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001020 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001021 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001022 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001023 if (dlopen(path, RTLD_NOW)) {
1024 /* Found library. If it didn't register itself,
1025 maybe they specified match as a target. */
1026 ptr = find_target(name, DONT_LOAD);
1027 if (!ptr)
1028 exit_error(PARAMETER_PROBLEM,
1029 "Couldn't load target `%s'\n",
1030 name);
1031 } else if (tryload == LOAD_MUST_SUCCEED)
1032 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001033 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001034 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001035 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001036#else
1037 if (ptr && !ptr->loaded) {
1038 if (tryload != DONT_LOAD)
1039 ptr->loaded = 1;
1040 else
1041 ptr = NULL;
1042 }
Marc Boucher067477b2002-03-24 15:09:31 +00001043 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1044 exit_error(PARAMETER_PROBLEM,
1045 "Couldn't find target `%s'\n", name);
1046 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001047#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048
Harald Welte43ac1912002-03-03 09:43:07 +00001049 if (ptr)
1050 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001051
Rusty Russell5eed48a2000-06-02 20:12:24 +00001052 return ptr;
1053}
1054
1055static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001056merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001057 unsigned int *option_offset)
1058{
1059 unsigned int num_old, num_new, i;
1060 struct option *merge;
1061
1062 for (num_old = 0; oldopts[num_old].name; num_old++);
1063 for (num_new = 0; newopts[num_new].name; num_new++);
1064
1065 global_option_offset += OPTION_OFFSET;
1066 *option_offset = global_option_offset;
1067
1068 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1069 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001070 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001071 for (i = 0; i < num_new; i++) {
1072 merge[num_old + i] = newopts[i];
1073 merge[num_old + i].val += *option_offset;
1074 }
1075 memset(merge + num_old + num_new, 0, sizeof(struct option));
1076
1077 return merge;
1078}
1079
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001080static int compatible_revision(const char *name, u_int8_t revision, int opt)
1081{
1082 struct ip6t_get_revision rev;
1083 socklen_t s = sizeof(rev);
1084 int max_rev, sockfd;
1085
1086 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1087 if (sockfd < 0) {
1088 fprintf(stderr, "Could not open socket to kernel: %s\n",
1089 strerror(errno));
1090 exit(1);
1091 }
1092
1093 strcpy(rev.name, name);
1094 rev.revision = revision;
1095
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001096 load_ip6tables_ko(modprobe, 1);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001097
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001098 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1099 if (max_rev < 0) {
1100 /* Definitely don't support this? */
Patrick McHardy76258722007-06-26 15:29:45 +00001101 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001102 close(sockfd);
1103 return 0;
1104 } else if (errno == ENOPROTOOPT) {
1105 close(sockfd);
1106 /* Assume only revision 0 support (old kernel) */
1107 return (revision == 0);
1108 } else {
1109 fprintf(stderr, "getsockopt failed strangely: %s\n",
1110 strerror(errno));
1111 exit(1);
1112 }
1113 }
1114 close(sockfd);
1115 return 1;
1116}
1117
1118static int compatible_match_revision(const char *name, u_int8_t revision)
1119{
1120 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1121}
1122
Rusty Russell5eed48a2000-06-02 20:12:24 +00001123void
1124register_match6(struct ip6tables_match *me)
1125{
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001126 struct ip6tables_match **i, *old;
Harald Welte43ac1912002-03-03 09:43:07 +00001127
Rusty Russell5eed48a2000-06-02 20:12:24 +00001128 if (strcmp(me->version, program_version) != 0) {
1129 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1130 program_name, me->name, me->version, program_version);
1131 exit(1);
1132 }
1133
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001134 /* Revision field stole a char from name. */
1135 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1136 fprintf(stderr, "%s: target `%s' has invalid name\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001137 program_name, me->name);
1138 exit(1);
1139 }
1140
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001141 old = find_match(me->name, DURING_LOAD, NULL);
1142 if (old) {
1143 if (old->revision == me->revision) {
1144 fprintf(stderr,
1145 "%s: match `%s' already registered.\n",
1146 program_name, me->name);
1147 exit(1);
1148 }
1149
1150 /* Now we have two (or more) options, check compatibility. */
1151 if (compatible_match_revision(old->name, old->revision)
1152 && old->revision > me->revision)
1153 return;
1154
1155 /* Replace if compatible. */
1156 if (!compatible_match_revision(me->name, me->revision))
1157 return;
1158
1159 /* Delete old one. */
1160 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1161 *i = old->next;
1162 }
1163
Harald Welte43ac1912002-03-03 09:43:07 +00001164 if (me->size != IP6T_ALIGN(me->size)) {
1165 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001166 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001167 exit(1);
1168 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001169
Harald Welte43ac1912002-03-03 09:43:07 +00001170 /* Append to list. */
1171 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1172 me->next = NULL;
1173 *i = me;
1174
Rusty Russell5eed48a2000-06-02 20:12:24 +00001175 me->m = NULL;
1176 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001177}
1178
1179void
1180register_target6(struct ip6tables_target *me)
1181{
1182 if (strcmp(me->version, program_version) != 0) {
1183 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1184 program_name, me->name, me->version, program_version);
1185 exit(1);
1186 }
1187
Jones Desougif5b86e62005-12-22 03:33:50 +00001188 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189 fprintf(stderr, "%s: target `%s' already registered.\n",
1190 program_name, me->name);
1191 exit(1);
1192 }
1193
Harald Welte43ac1912002-03-03 09:43:07 +00001194 if (me->size != IP6T_ALIGN(me->size)) {
1195 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001196 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001197 exit(1);
1198 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001199
Rusty Russell5eed48a2000-06-02 20:12:24 +00001200 /* Prepend to list. */
1201 me->next = ip6tables_targets;
1202 ip6tables_targets = me;
1203 me->t = NULL;
1204 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001205}
1206
1207static void
1208print_num(u_int64_t number, unsigned int format)
1209{
Harald Welte43ac1912002-03-03 09:43:07 +00001210 if (format & FMT_KILOMEGAGIGA) {
1211 if (number > 99999) {
1212 number = (number + 500) / 1000;
1213 if (number > 9999) {
1214 number = (number + 500) / 1000;
1215 if (number > 9999) {
1216 number = (number + 500) / 1000;
1217 if (number > 9999) {
1218 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001219 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001220 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001221 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001222 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001223 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001224 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001225 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001226 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001227 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001228 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001229 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001230}
1231
Harald Welte43ac1912002-03-03 09:43:07 +00001232
Rusty Russell5eed48a2000-06-02 20:12:24 +00001233static void
1234print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1235{
1236 struct ip6t_counters counters;
1237 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1238 printf("Chain %s", chain);
1239 if (pol) {
1240 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001241 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001242 fputc(' ', stdout);
1243 print_num(counters.pcnt, (format|FMT_NOTABLE));
1244 fputs("packets, ", stdout);
1245 print_num(counters.bcnt, (format|FMT_NOTABLE));
1246 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001247 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001248 printf(")\n");
1249 } else {
1250 unsigned int refs;
1251 if (!ip6tc_get_references(&refs, chain, handle))
1252 printf(" (ERROR obtaining refs)\n");
1253 else
1254 printf(" (%u references)\n", refs);
1255 }
1256
1257 if (format & FMT_LINENUMBERS)
1258 printf(FMT("%-4s ", "%s "), "num");
1259 if (!(format & FMT_NOCOUNTS)) {
1260 if (format & FMT_KILOMEGAGIGA) {
1261 printf(FMT("%5s ","%s "), "pkts");
1262 printf(FMT("%5s ","%s "), "bytes");
1263 } else {
1264 printf(FMT("%8s ","%s "), "pkts");
1265 printf(FMT("%10s ","%s "), "bytes");
1266 }
1267 }
1268 if (!(format & FMT_NOTARGET))
1269 printf(FMT("%-9s ","%s "), "target");
1270 fputs(" prot ", stdout);
1271 if (format & FMT_OPTIONS)
1272 fputs("opt", stdout);
1273 if (format & FMT_VIA) {
1274 printf(FMT(" %-6s ","%s "), "in");
1275 printf(FMT("%-6s ","%s "), "out");
1276 }
1277 printf(FMT(" %-19s ","%s "), "source");
1278 printf(FMT(" %-19s "," %s "), "destination");
1279 printf("\n");
1280}
1281
Harald Welte43ac1912002-03-03 09:43:07 +00001282
Rusty Russell5eed48a2000-06-02 20:12:24 +00001283static int
1284print_match(const struct ip6t_entry_match *m,
1285 const struct ip6t_ip6 *ip,
1286 int numeric)
1287{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001288 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001289
1290 if (match) {
1291 if (match->print)
1292 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001293 else
1294 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001295 } else {
1296 if (m->u.user.name[0])
1297 printf("UNKNOWN match `%s' ", m->u.user.name);
1298 }
1299 /* Don't stop iterating. */
1300 return 0;
1301}
1302
1303/* e is called `fw' here for hysterical raisins */
1304static void
1305print_firewall(const struct ip6t_entry *fw,
1306 const char *targname,
1307 unsigned int num,
1308 unsigned int format,
1309 const ip6tc_handle_t handle)
1310{
1311 struct ip6tables_target *target = NULL;
1312 const struct ip6t_entry_target *t;
1313 u_int8_t flags;
1314 char buf[BUFSIZ];
1315
Rusty Russell5eed48a2000-06-02 20:12:24 +00001316 if (!ip6tc_is_chain(targname, handle))
1317 target = find_target(targname, TRY_LOAD);
1318 else
1319 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1320
1321 t = ip6t_get_target((struct ip6t_entry *)fw);
1322 flags = fw->ipv6.flags;
1323
1324 if (format & FMT_LINENUMBERS)
1325 printf(FMT("%-4u ", "%u "), num+1);
1326
1327 if (!(format & FMT_NOCOUNTS)) {
1328 print_num(fw->counters.pcnt, format);
1329 print_num(fw->counters.bcnt, format);
1330 }
1331
1332 if (!(format & FMT_NOTARGET))
1333 printf(FMT("%-9s ", "%s "), targname);
1334
1335 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1336 {
Harald Welte43ac1912002-03-03 09:43:07 +00001337 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001338 if (pname)
1339 printf(FMT("%-5s", "%s "), pname);
1340 else
1341 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1342 }
1343
1344 if (format & FMT_OPTIONS) {
1345 if (format & FMT_NOTABLE)
1346 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001347 fputc(' ', stdout); /* Invert flag of FRAG */
1348 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349 fputc(' ', stdout);
1350 }
1351
1352 if (format & FMT_VIA) {
1353 char iface[IFNAMSIZ+2];
1354
1355 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1356 iface[0] = '!';
1357 iface[1] = '\0';
1358 }
1359 else iface[0] = '\0';
1360
1361 if (fw->ipv6.iniface[0] != '\0') {
1362 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001363 }
1364 else if (format & FMT_NUMERIC) strcat(iface, "*");
1365 else strcat(iface, "any");
1366 printf(FMT(" %-6s ","in %s "), iface);
1367
1368 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1369 iface[0] = '!';
1370 iface[1] = '\0';
1371 }
1372 else iface[0] = '\0';
1373
1374 if (fw->ipv6.outiface[0] != '\0') {
1375 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001376 }
1377 else if (format & FMT_NUMERIC) strcat(iface, "*");
1378 else strcat(iface, "any");
1379 printf(FMT("%-6s ","out %s "), iface);
1380 }
1381
1382 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1383 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1384 && !(format & FMT_NUMERIC))
1385 printf(FMT("%-19s ","%s "), "anywhere");
1386 else {
1387 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001388 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001389 else
Harald Welte43ac1912002-03-03 09:43:07 +00001390 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1392 printf(FMT("%-19s ","%s "), buf);
1393 }
1394
1395 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1396 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1397 && !(format & FMT_NUMERIC))
1398 printf(FMT("%-19s","-> %s"), "anywhere");
1399 else {
1400 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001401 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001402 else
Harald Welte43ac1912002-03-03 09:43:07 +00001403 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001404 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1405 printf(FMT("%-19s","-> %s"), buf);
1406 }
1407
1408 if (format & FMT_NOTABLE)
1409 fputs(" ", stdout);
1410
1411 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1412
1413 if (target) {
1414 if (target->print)
1415 /* Print the target information. */
1416 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1417 } else if (t->u.target_size != sizeof(*t))
1418 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001419 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001420
1421 if (!(format & FMT_NONEWLINE))
1422 fputc('\n', stdout);
1423}
1424
1425static void
1426print_firewall_line(const struct ip6t_entry *fw,
1427 const ip6tc_handle_t h)
1428{
1429 struct ip6t_entry_target *t;
1430
1431 t = ip6t_get_target((struct ip6t_entry *)fw);
1432 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1433}
1434
1435static int
1436append_entry(const ip6t_chainlabel chain,
1437 struct ip6t_entry *fw,
1438 unsigned int nsaddrs,
1439 const struct in6_addr saddrs[],
1440 unsigned int ndaddrs,
1441 const struct in6_addr daddrs[],
1442 int verbose,
1443 ip6tc_handle_t *handle)
1444{
1445 unsigned int i, j;
1446 int ret = 1;
1447
1448 for (i = 0; i < nsaddrs; i++) {
1449 fw->ipv6.src = saddrs[i];
1450 for (j = 0; j < ndaddrs; j++) {
1451 fw->ipv6.dst = daddrs[j];
1452 if (verbose)
1453 print_firewall_line(fw, *handle);
1454 ret &= ip6tc_append_entry(chain, fw, handle);
1455 }
1456 }
1457
1458 return ret;
1459}
1460
1461static int
1462replace_entry(const ip6t_chainlabel chain,
1463 struct ip6t_entry *fw,
1464 unsigned int rulenum,
1465 const struct in6_addr *saddr,
1466 const struct in6_addr *daddr,
1467 int verbose,
1468 ip6tc_handle_t *handle)
1469{
1470 fw->ipv6.src = *saddr;
1471 fw->ipv6.dst = *daddr;
1472
1473 if (verbose)
1474 print_firewall_line(fw, *handle);
1475 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1476}
1477
1478static int
1479insert_entry(const ip6t_chainlabel chain,
1480 struct ip6t_entry *fw,
1481 unsigned int rulenum,
1482 unsigned int nsaddrs,
1483 const struct in6_addr saddrs[],
1484 unsigned int ndaddrs,
1485 const struct in6_addr daddrs[],
1486 int verbose,
1487 ip6tc_handle_t *handle)
1488{
1489 unsigned int i, j;
1490 int ret = 1;
1491
1492 for (i = 0; i < nsaddrs; i++) {
1493 fw->ipv6.src = saddrs[i];
1494 for (j = 0; j < ndaddrs; j++) {
1495 fw->ipv6.dst = daddrs[j];
1496 if (verbose)
1497 print_firewall_line(fw, *handle);
1498 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1499 }
1500 }
1501
1502 return ret;
1503}
1504
1505static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001506make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001507{
1508 /* Establish mask for comparison */
1509 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001510 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001511 unsigned char *mask, *mptr;
1512
1513 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001514 for (matchp = matches; matchp; matchp = matchp->next)
1515 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516
1517 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001518 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001519 + ip6tables_targets->size);
1520
1521 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1522 mptr = mask + sizeof(struct ip6t_entry);
1523
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001524 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001525 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001526 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001527 + matchp->match->userspacesize);
1528 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001529 }
1530
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001531 memset(mptr, 0xFF,
1532 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1533 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534
1535 return mask;
1536}
1537
1538static int
1539delete_entry(const ip6t_chainlabel chain,
1540 struct ip6t_entry *fw,
1541 unsigned int nsaddrs,
1542 const struct in6_addr saddrs[],
1543 unsigned int ndaddrs,
1544 const struct in6_addr daddrs[],
1545 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001546 ip6tc_handle_t *handle,
1547 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001548{
1549 unsigned int i, j;
1550 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001551 unsigned char *mask;
1552
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001553 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001554 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001555 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001557 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001558 if (verbose)
1559 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001560 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561 }
1562 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001563 free(mask);
1564
Rusty Russell5eed48a2000-06-02 20:12:24 +00001565 return ret;
1566}
1567
András Kis-Szabó764316a2001-02-26 17:31:20 +00001568int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001569for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1570 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1571{
1572 int ret = 1;
1573 const char *chain;
1574 char *chains;
1575 unsigned int i, chaincount = 0;
1576
1577 chain = ip6tc_first_chain(handle);
1578 while (chain) {
1579 chaincount++;
1580 chain = ip6tc_next_chain(handle);
1581 }
1582
1583 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1584 i = 0;
1585 chain = ip6tc_first_chain(handle);
1586 while (chain) {
1587 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1588 i++;
1589 chain = ip6tc_next_chain(handle);
1590 }
1591
1592 for (i = 0; i < chaincount; i++) {
1593 if (!builtinstoo
1594 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001595 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001596 continue;
1597 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1598 }
1599
1600 free(chains);
1601 return ret;
1602}
1603
András Kis-Szabó764316a2001-02-26 17:31:20 +00001604int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605flush_entries(const ip6t_chainlabel chain, int verbose,
1606 ip6tc_handle_t *handle)
1607{
1608 if (!chain)
1609 return for_each_chain(flush_entries, verbose, 1, handle);
1610
1611 if (verbose)
1612 fprintf(stdout, "Flushing chain `%s'\n", chain);
1613 return ip6tc_flush_entries(chain, handle);
1614}
1615
1616static int
1617zero_entries(const ip6t_chainlabel chain, int verbose,
1618 ip6tc_handle_t *handle)
1619{
1620 if (!chain)
1621 return for_each_chain(zero_entries, verbose, 1, handle);
1622
1623 if (verbose)
1624 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1625 return ip6tc_zero_entries(chain, handle);
1626}
1627
András Kis-Szabó764316a2001-02-26 17:31:20 +00001628int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629delete_chain(const ip6t_chainlabel chain, int verbose,
1630 ip6tc_handle_t *handle)
1631{
1632 if (!chain)
1633 return for_each_chain(delete_chain, verbose, 0, handle);
1634
1635 if (verbose)
1636 fprintf(stdout, "Deleting chain `%s'\n", chain);
1637 return ip6tc_delete_chain(chain, handle);
1638}
1639
1640static int
1641list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1642 int expanded, int linenumbers, ip6tc_handle_t *handle)
1643{
1644 int found = 0;
1645 unsigned int format;
1646 const char *this;
1647
1648 format = FMT_OPTIONS;
1649 if (!verbose)
1650 format |= FMT_NOCOUNTS;
1651 else
1652 format |= FMT_VIA;
1653
1654 if (numeric)
1655 format |= FMT_NUMERIC;
1656
1657 if (!expanded)
1658 format |= FMT_KILOMEGAGIGA;
1659
1660 if (linenumbers)
1661 format |= FMT_LINENUMBERS;
1662
1663 for (this = ip6tc_first_chain(handle);
1664 this;
1665 this = ip6tc_next_chain(handle)) {
1666 const struct ip6t_entry *i;
1667 unsigned int num;
1668
1669 if (chain && strcmp(chain, this) != 0)
1670 continue;
1671
1672 if (found) printf("\n");
1673
1674 print_header(format, this, handle);
1675 i = ip6tc_first_rule(this, handle);
1676
1677 num = 0;
1678 while (i) {
1679 print_firewall(i,
1680 ip6tc_get_target(i, handle),
1681 num++,
1682 format,
1683 *handle);
1684 i = ip6tc_next_rule(i, handle);
1685 }
1686 found = 1;
1687 }
1688
1689 errno = ENOENT;
1690 return found;
1691}
1692
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001693int load_ip6tables_ko(const char *modprobe, int quiet)
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001694{
1695 static int loaded = 0;
1696 static int ret = -1;
1697
1698 if (!loaded) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +00001699 ret = xtables_insmod("ip6_tables", modprobe, quiet);
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001700 loaded = (ret == 0);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001701 }
1702
1703 return ret;
1704}
1705
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706static struct ip6t_entry *
1707generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001708 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001709 struct ip6t_entry_target *target)
1710{
1711 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001712 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001713 struct ip6t_entry *e;
1714
1715 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001716 for (matchp = matches; matchp; matchp = matchp->next)
1717 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001718
1719 e = fw_malloc(size + target->u.target_size);
1720 *e = *fw;
1721 e->target_offset = size;
1722 e->next_offset = size + target->u.target_size;
1723
1724 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001725 for (matchp = matches; matchp; matchp = matchp->next) {
1726 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1727 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001728 }
1729 memcpy(e->elems + size, target, target->u.target_size);
1730
1731 return e;
1732}
1733
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001734void clear_rule_matches(struct ip6tables_rule_match **matches)
1735{
1736 struct ip6tables_rule_match *matchp, *tmp;
1737
1738 for (matchp = *matches; matchp;) {
1739 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001740 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001741 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001742 matchp->match->m = NULL;
1743 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001744 if (matchp->match == matchp->match->next) {
1745 free(matchp->match);
1746 matchp->match = NULL;
1747 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001748 free(matchp);
1749 matchp = tmp;
1750 }
1751
1752 *matches = NULL;
1753}
1754
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001755static void set_revision(char *name, u_int8_t revision)
1756{
1757 /* Old kernel sources don't have ".revision" field,
1758 but we stole a byte from name. */
1759 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1760 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1761}
1762
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1764{
1765 struct ip6t_entry fw, *e = NULL;
1766 int invert = 0;
1767 unsigned int nsaddrs = 0, ndaddrs = 0;
1768 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1769
1770 int c, verbose = 0;
1771 const char *chain = NULL;
1772 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1773 const char *policy = NULL, *newname = NULL;
1774 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001775 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001776 int ret = 1;
1777 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001778 struct ip6tables_rule_match *matches = NULL;
1779 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001780 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001781 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001782 const char *jumpto = "";
1783 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001784 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001785
1786 memset(&fw, 0, sizeof(fw));
1787
Harald Welte43ac1912002-03-03 09:43:07 +00001788 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001789 * a second time */
1790 optind = 0;
1791
Harald Welte43ac1912002-03-03 09:43:07 +00001792 /* clear mflags in case do_command gets called a second time
1793 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001794 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001795 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001796
Harald Welte43ac1912002-03-03 09:43:07 +00001797 for (t = ip6tables_targets; t; t = t->next) {
1798 t->tflags = 0;
1799 t->used = 0;
1800 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001801
Rusty Russell5eed48a2000-06-02 20:12:24 +00001802 /* Suppress error messages: we may add new options if we
1803 demand-load a protocol. */
1804 opterr = 0;
1805
1806 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001807 "-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 +00001808 opts, NULL)) != -1) {
1809 switch (c) {
1810 /*
1811 * Command selection
1812 */
1813 case 'A':
1814 add_command(&command, CMD_APPEND, CMD_NONE,
1815 invert);
1816 chain = optarg;
1817 break;
1818
1819 case 'D':
1820 add_command(&command, CMD_DELETE, CMD_NONE,
1821 invert);
1822 chain = optarg;
1823 if (optind < argc && argv[optind][0] != '-'
1824 && argv[optind][0] != '!') {
1825 rulenum = parse_rulenumber(argv[optind++]);
1826 command = CMD_DELETE_NUM;
1827 }
1828 break;
1829
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 case 'R':
1831 add_command(&command, CMD_REPLACE, CMD_NONE,
1832 invert);
1833 chain = optarg;
1834 if (optind < argc && argv[optind][0] != '-'
1835 && argv[optind][0] != '!')
1836 rulenum = parse_rulenumber(argv[optind++]);
1837 else
1838 exit_error(PARAMETER_PROBLEM,
1839 "-%c requires a rule number",
1840 cmd2char(CMD_REPLACE));
1841 break;
1842
1843 case 'I':
1844 add_command(&command, CMD_INSERT, CMD_NONE,
1845 invert);
1846 chain = optarg;
1847 if (optind < argc && argv[optind][0] != '-'
1848 && argv[optind][0] != '!')
1849 rulenum = parse_rulenumber(argv[optind++]);
1850 else rulenum = 1;
1851 break;
1852
1853 case 'L':
1854 add_command(&command, CMD_LIST, CMD_ZERO,
1855 invert);
1856 if (optarg) chain = optarg;
1857 else if (optind < argc && argv[optind][0] != '-'
1858 && argv[optind][0] != '!')
1859 chain = argv[optind++];
1860 break;
1861
1862 case 'F':
1863 add_command(&command, CMD_FLUSH, CMD_NONE,
1864 invert);
1865 if (optarg) chain = optarg;
1866 else if (optind < argc && argv[optind][0] != '-'
1867 && argv[optind][0] != '!')
1868 chain = argv[optind++];
1869 break;
1870
1871 case 'Z':
1872 add_command(&command, CMD_ZERO, CMD_LIST,
1873 invert);
1874 if (optarg) chain = optarg;
1875 else if (optind < argc && argv[optind][0] != '-'
1876 && argv[optind][0] != '!')
1877 chain = argv[optind++];
1878 break;
1879
1880 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001881 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001882 exit_error(PARAMETER_PROBLEM,
1883 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001884 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001885 if (find_target(optarg, TRY_LOAD))
1886 exit_error(PARAMETER_PROBLEM,
1887 "chain name may not clash "
1888 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001889 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1890 invert);
1891 chain = optarg;
1892 break;
1893
1894 case 'X':
1895 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1896 invert);
1897 if (optarg) chain = optarg;
1898 else if (optind < argc && argv[optind][0] != '-'
1899 && argv[optind][0] != '!')
1900 chain = argv[optind++];
1901 break;
1902
1903 case 'E':
1904 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1905 invert);
1906 chain = optarg;
1907 if (optind < argc && argv[optind][0] != '-'
1908 && argv[optind][0] != '!')
1909 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001910 else
1911 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001912 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001913 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001914 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001915 break;
1916
1917 case 'P':
1918 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1919 invert);
1920 chain = optarg;
1921 if (optind < argc && argv[optind][0] != '-'
1922 && argv[optind][0] != '!')
1923 policy = argv[optind++];
1924 else
1925 exit_error(PARAMETER_PROBLEM,
1926 "-%c requires a chain and a policy",
1927 cmd2char(CMD_SET_POLICY));
1928 break;
1929
1930 case 'h':
1931 if (!optarg)
1932 optarg = argv[optind];
1933
Jonas Berlin1b91e592005-04-01 06:58:38 +00001934 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001935 if (!matches && protocol)
1936 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001937
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001938 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001939
1940 /*
1941 * Option selection
1942 */
1943 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001944 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001945 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1946 invert);
1947
1948 /* Canonicalize into lower case */
1949 for (protocol = argv[optind-1]; *protocol; protocol++)
1950 *protocol = tolower(*protocol);
1951
1952 protocol = argv[optind-1];
1953 fw.ipv6.proto = parse_protocol(protocol);
1954 fw.ipv6.flags |= IP6T_F_PROTO;
1955
1956 if (fw.ipv6.proto == 0
1957 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1958 exit_error(PARAMETER_PROBLEM,
1959 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001960
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001961 if (is_exthdr(fw.ipv6.proto)
1962 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001963 printf("Warning: never matched protocol: %s. "
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001964 "use extension match instead.\n",
1965 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001966 break;
1967
1968 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001969 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001970 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1971 invert);
1972 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001973 break;
1974
1975 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001976 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001977 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1978 invert);
1979 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001980 break;
1981
1982 case 'j':
1983 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1984 invert);
1985 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001986 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001987 target = find_target(jumpto, TRY_LOAD);
1988
1989 if (target) {
1990 size_t size;
1991
Harald Welte43ac1912002-03-03 09:43:07 +00001992 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1993 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001994
1995 target->t = fw_calloc(1, size);
1996 target->t->u.target_size = size;
1997 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001998 if (target->init != NULL)
1999 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002000 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 }
2002 break;
2003
2004
2005 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002006 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2008 invert);
2009 parse_interface(argv[optind-1],
2010 fw.ipv6.iniface,
2011 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002012 break;
2013
2014 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002015 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002016 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2017 invert);
2018 parse_interface(argv[optind-1],
2019 fw.ipv6.outiface,
2020 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002021 break;
2022
2023 case 'v':
2024 if (!verbose)
2025 set_option(&options, OPT_VERBOSE,
2026 &fw.ipv6.invflags, invert);
2027 verbose++;
2028 break;
2029
2030 case 'm': {
2031 size_t size;
2032
2033 if (invert)
2034 exit_error(PARAMETER_PROBLEM,
2035 "unexpected ! flag before --match");
2036
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002037 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002038 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2039 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002040 m->m = fw_calloc(1, size);
2041 m->m->u.match_size = size;
2042 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002043 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002044 if (m->init != NULL)
2045 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002046 if (m != m->next)
2047 /* Merge options for non-cloned matches */
2048 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 }
2050 break;
2051
2052 case 'n':
2053 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2054 invert);
2055 break;
2056
2057 case 't':
2058 if (invert)
2059 exit_error(PARAMETER_PROBLEM,
2060 "unexpected ! flag before --table");
2061 *table = argv[optind-1];
2062 break;
2063
2064 case 'x':
2065 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2066 invert);
2067 break;
2068
2069 case 'V':
2070 if (invert)
2071 printf("Not %s ;-)\n", program_version);
2072 else
2073 printf("%s v%s\n",
2074 program_name, program_version);
2075 exit(0);
2076
2077 case '0':
2078 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2079 invert);
2080 break;
2081
Harald Welte43ac1912002-03-03 09:43:07 +00002082 case 'M':
2083 modprobe = optarg;
2084 break;
2085
2086 case 'c':
2087
2088 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2089 invert);
2090 pcnt = optarg;
2091 if (optind < argc && argv[optind][0] != '-'
2092 && argv[optind][0] != '!')
2093 bcnt = argv[optind++];
2094 else
2095 exit_error(PARAMETER_PROBLEM,
2096 "-%c requires packet and byte counter",
2097 opt2char(OPT_COUNTERS));
2098
Martin Josefssona28d4952004-05-26 16:04:48 +00002099 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002100 exit_error(PARAMETER_PROBLEM,
2101 "-%c packet counter not numeric",
2102 opt2char(OPT_COUNTERS));
2103
Martin Josefssona28d4952004-05-26 16:04:48 +00002104 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002105 exit_error(PARAMETER_PROBLEM,
2106 "-%c byte counter not numeric",
2107 opt2char(OPT_COUNTERS));
2108
2109 break;
2110
2111
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 case 1: /* non option */
2113 if (optarg[0] == '!' && optarg[1] == '\0') {
2114 if (invert)
2115 exit_error(PARAMETER_PROBLEM,
2116 "multiple consecutive ! not"
2117 " allowed");
2118 invert = TRUE;
2119 optarg[0] = '\0';
2120 continue;
2121 }
2122 printf("Bad argument `%s'\n", optarg);
2123 exit_tryhelp(2);
2124
2125 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002126 if (!target
2127 || !(target->parse(c - target->option_offset,
2128 argv, invert,
2129 &target->tflags,
2130 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002131 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002132 if (matchp->completed)
2133 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002134 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002135 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002136 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002137 &fw,
2138 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002139 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002140 break;
2141 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002142 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002143
2144 /* If you listen carefully, you can
2145 actually hear this code suck. */
2146
2147 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002148 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002149 * parameter, that has not been parsed yet,
2150 * it's not an option of an explicitly loaded
2151 * match or a target. However, we support
2152 * implicit loading of the protocol match
2153 * extension. '-p tcp' means 'l4 proto 6' and
2154 * at the same time 'load tcp protocol match on
2155 * demand if we specify --dport'.
2156 *
2157 * To make this work, we need to make sure:
2158 * - the parameter has not been parsed by
2159 * a match (m above)
2160 * - a protocol has been specified
2161 * - the protocol extension has not been
2162 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002163 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002164 * - the protocol extension can be successively
2165 * loaded
2166 */
2167 if (m == NULL
2168 && protocol
2169 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002170 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002171 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002172 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002173 && (proto_used == 0))
2174 )
2175 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002176 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002177 /* Try loading protocol */
2178 size_t size;
2179
2180 proto_used = 1;
2181
2182 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2183 + m->size;
2184
2185 m->m = fw_calloc(1, size);
2186 m->m->u.match_size = size;
2187 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00002188 set_revision(m->m->u.user.name,
2189 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002190 if (m->init != NULL)
2191 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002192
2193 opts = merge_options(opts,
2194 m->extra_opts, &m->option_offset);
2195
2196 optind--;
2197 continue;
2198 }
2199
Rusty Russell5eed48a2000-06-02 20:12:24 +00002200 if (!m)
2201 exit_error(PARAMETER_PROBLEM,
2202 "Unknown arg `%s'",
2203 argv[optind-1]);
2204 }
2205 }
2206 invert = FALSE;
2207 }
2208
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002209 for (matchp = matches; matchp; matchp = matchp->next)
2210 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002211
Harald Welte43ac1912002-03-03 09:43:07 +00002212 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002213 target->final_check(target->tflags);
2214
2215 /* Fix me: must put inverse options checking here --MN */
2216
2217 if (optind < argc)
2218 exit_error(PARAMETER_PROBLEM,
2219 "unknown arguments found on commandline");
2220 if (!command)
2221 exit_error(PARAMETER_PROBLEM, "no command specified");
2222 if (invert)
2223 exit_error(PARAMETER_PROBLEM,
2224 "nothing appropriate following !");
2225
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002226 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002227 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002228 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002229 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002230 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002231 }
2232
2233 if (shostnetworkmask)
2234 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2235 &(fw.ipv6.smsk), &nsaddrs);
2236
2237 if (dhostnetworkmask)
2238 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2239 &(fw.ipv6.dmsk), &ndaddrs);
2240
2241 if ((nsaddrs > 1 || ndaddrs > 1) &&
2242 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2243 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2244 " source or destination IP addresses");
2245
Rusty Russell5eed48a2000-06-02 20:12:24 +00002246 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2247 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2248 "specify a unique address");
2249
2250 generic_opt_check(command, options);
2251
2252 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2253 exit_error(PARAMETER_PROBLEM,
2254 "chain name `%s' too long (must be under %i chars)",
2255 chain, IP6T_FUNCTION_MAXNAMELEN);
2256
Harald Welte43ac1912002-03-03 09:43:07 +00002257 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002258 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002259 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002260
Rusty Russell8beb0492004-12-22 00:37:10 +00002261 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00002262 if (!*handle && load_ip6tables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002263 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002264
Harald Welte43ac1912002-03-03 09:43:07 +00002265 if (!*handle)
2266 exit_error(VERSION_PROBLEM,
2267 "can't initialize ip6tables table `%s': %s",
2268 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002269
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002270 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002271 || command == CMD_DELETE
2272 || command == CMD_INSERT
2273 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002274 if (strcmp(chain, "PREROUTING") == 0
2275 || strcmp(chain, "INPUT") == 0) {
2276 /* -o not valid with incoming packets. */
2277 if (options & OPT_VIANAMEOUT)
2278 exit_error(PARAMETER_PROBLEM,
2279 "Can't use -%c with %s\n",
2280 opt2char(OPT_VIANAMEOUT),
2281 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002282 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002283
Harald Welte43ac1912002-03-03 09:43:07 +00002284 if (strcmp(chain, "POSTROUTING") == 0
2285 || strcmp(chain, "OUTPUT") == 0) {
2286 /* -i not valid with outgoing packets */
2287 if (options & OPT_VIANAMEIN)
2288 exit_error(PARAMETER_PROBLEM,
2289 "Can't use -%c with %s\n",
2290 opt2char(OPT_VIANAMEIN),
2291 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002292 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002293
2294 if (target && ip6tc_is_chain(jumpto, *handle)) {
2295 printf("Warning: using chain %s, not extension\n",
2296 jumpto);
2297
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002298 if (target->t)
2299 free(target->t);
2300
Rusty Russell5eed48a2000-06-02 20:12:24 +00002301 target = NULL;
2302 }
2303
2304 /* If they didn't specify a target, or it's a chain
2305 name, use standard. */
2306 if (!target
2307 && (strlen(jumpto) == 0
2308 || ip6tc_is_chain(jumpto, *handle))) {
2309 size_t size;
2310
2311 target = find_target(IP6T_STANDARD_TARGET,
2312 LOAD_MUST_SUCCEED);
2313
2314 size = sizeof(struct ip6t_entry_target)
2315 + target->size;
2316 target->t = fw_calloc(1, size);
2317 target->t->u.target_size = size;
2318 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002319 if (target->init != NULL)
2320 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002321 }
2322
2323 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002324 /* it is no chain, and we can't load a plugin.
2325 * We cannot know if the plugin is corrupt, non
2326 * existant OR if the user just misspelled a
2327 * chain. */
2328 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002329 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002330 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002331 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002332 }
2333 }
2334
2335 switch (command) {
2336 case CMD_APPEND:
2337 ret = append_entry(chain, e,
2338 nsaddrs, saddrs, ndaddrs, daddrs,
2339 options&OPT_VERBOSE,
2340 handle);
2341 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002342 case CMD_DELETE:
2343 ret = delete_entry(chain, e,
2344 nsaddrs, saddrs, ndaddrs, daddrs,
2345 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002346 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002347 break;
2348 case CMD_DELETE_NUM:
2349 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2350 break;
2351 case CMD_REPLACE:
2352 ret = replace_entry(chain, e, rulenum - 1,
2353 saddrs, daddrs, options&OPT_VERBOSE,
2354 handle);
2355 break;
2356 case CMD_INSERT:
2357 ret = insert_entry(chain, e, rulenum - 1,
2358 nsaddrs, saddrs, ndaddrs, daddrs,
2359 options&OPT_VERBOSE,
2360 handle);
2361 break;
2362 case CMD_LIST:
2363 ret = list_entries(chain,
2364 options&OPT_VERBOSE,
2365 options&OPT_NUMERIC,
2366 options&OPT_EXPANDED,
2367 options&OPT_LINENUMBERS,
2368 handle);
2369 break;
2370 case CMD_FLUSH:
2371 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2372 break;
2373 case CMD_ZERO:
2374 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2375 break;
2376 case CMD_LIST|CMD_ZERO:
2377 ret = list_entries(chain,
2378 options&OPT_VERBOSE,
2379 options&OPT_NUMERIC,
2380 options&OPT_EXPANDED,
2381 options&OPT_LINENUMBERS,
2382 handle);
2383 if (ret)
2384 ret = zero_entries(chain,
2385 options&OPT_VERBOSE, handle);
2386 break;
2387 case CMD_NEW_CHAIN:
2388 ret = ip6tc_create_chain(chain, handle);
2389 break;
2390 case CMD_DELETE_CHAIN:
2391 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2392 break;
2393 case CMD_RENAME_CHAIN:
2394 ret = ip6tc_rename_chain(chain, newname, handle);
2395 break;
2396 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002397 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002398 break;
2399 default:
2400 /* We should never reach this... */
2401 exit_tryhelp(2);
2402 }
2403
2404 if (verbose > 1)
2405 dump_entries6(*handle);
2406
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002407 clear_rule_matches(&matches);
2408
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002409 if (e != NULL) {
2410 free(e);
2411 e = NULL;
2412 }
2413
2414 for (c = 0; c < nsaddrs; c++)
2415 free(&saddrs[c]);
2416
2417 for (c = 0; c < ndaddrs; c++)
2418 free(&daddrs[c]);
2419
Pablo Neiradfdcd642005-05-29 19:05:23 +00002420 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002421
Rusty Russell5eed48a2000-06-02 20:12:24 +00002422 return ret;
2423}