blob: aa1e148bdc2a050c545ceea3ed04e18c94229478 [file] [log] [blame]
Rusty Russell5eed48a2000-06-02 20:12:24 +00001/* Code to take an iptables-style command line and do it. */
2
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
84#define CMD_CHECK 0x0800U
85#define CMD_RENAME_CHAIN 0x1000U
86#define NUMBER_OF_CMD 13
87static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000088 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000089
90#define OPTION_OFFSET 256
91
92#define OPT_NONE 0x00000U
93#define OPT_NUMERIC 0x00001U
94#define OPT_SOURCE 0x00002U
95#define OPT_DESTINATION 0x00004U
96#define OPT_PROTOCOL 0x00008U
97#define OPT_JUMP 0x00010U
98#define OPT_VERBOSE 0x00020U
99#define OPT_EXPANDED 0x00040U
100#define OPT_VIANAMEIN 0x00080U
101#define OPT_VIANAMEOUT 0x00100U
102#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000103#define OPT_COUNTERS 0x00400U
104#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000105static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000106= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000107
108static struct option original_opts[] = {
109 { "append", 1, 0, 'A' },
110 { "delete", 1, 0, 'D' },
111 { "insert", 1, 0, 'I' },
112 { "replace", 1, 0, 'R' },
113 { "list", 2, 0, 'L' },
114 { "flush", 2, 0, 'F' },
115 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000116 { "new-chain", 1, 0, 'N' },
117 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000118 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000119 { "policy", 1, 0, 'P' },
120 { "source", 1, 0, 's' },
121 { "destination", 1, 0, 'd' },
122 { "src", 1, 0, 's' }, /* synonym */
123 { "dst", 1, 0, 'd' }, /* synonym */
124 { "protocol", 1, 0, 'p' },
125 { "in-interface", 1, 0, 'i' },
126 { "jump", 1, 0, 'j' },
127 { "table", 1, 0, 't' },
128 { "match", 1, 0, 'm' },
129 { "numeric", 0, 0, 'n' },
130 { "out-interface", 1, 0, 'o' },
131 { "verbose", 0, 0, 'v' },
132 { "exact", 0, 0, 'x' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000136 { "modprobe", 1, 0, 'M' },
137 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138 { 0 }
139};
140
Harald Weltea8658ca2003-03-05 07:46:15 +0000141/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. ip6tables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
146
Rusty Russell5eed48a2000-06-02 20:12:24 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
162 /* -n -s -d -p -j -v -x -i -o --line */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
176};
177
178static int inverse_for_options[NUMBER_OF_OPT] =
179{
180/* -n */ 0,
181/* -s */ IP6T_INV_SRCIP,
182/* -d */ IP6T_INV_DSTIP,
183/* -p */ IP6T_INV_PROTO,
184/* -j */ 0,
185/* -v */ 0,
186/* -x */ 0,
187/* -i */ IP6T_INV_VIA_IN,
188/* -o */ IP6T_INV_VIA_OUT,
189/*--line*/ 0
190};
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 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000225 { "esp", IPPROTO_ESP },
226 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227 { "all", 0 },
228};
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
248static void
249in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
250{
251 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000252 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000253}
254
255void
256exit_error(enum exittype status, char *msg, ...)
257{
258 va_list args;
259
260 va_start(args, msg);
261 fprintf(stderr, "%s v%s: ", program_name, program_version);
262 vfprintf(stderr, msg, args);
263 va_end(args);
264 fprintf(stderr, "\n");
265 if (status == PARAMETER_PROBLEM)
266 exit_tryhelp(status);
267 if (status == VERSION_PROBLEM)
268 fprintf(stderr,
269 "Perhaps iptables or your kernel needs to be upgraded.\n");
270 exit(status);
271}
272
273void
274exit_tryhelp(int status)
275{
Harald Weltea8658ca2003-03-05 07:46:15 +0000276 if (line != -1)
277 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
279 program_name, program_name );
280 exit(status);
281}
282
283void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000284exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000286 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287 struct ip6tables_target *t = NULL;
288
289 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000290"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000291" %s -[RI] chain rulenum rule-specification [options]\n"
292" %s -D chain rulenum [options]\n"
293" %s -[LFZ] [chain] [options]\n"
294" %s -[NX] chain\n"
295" %s -E old-chain-name new-chain-name\n"
296" %s -P chain target [options]\n"
297" %s -h (print this help information)\n\n",
298 program_name, program_version, program_name, program_name,
299 program_name, program_name, program_name, program_name,
300 program_name, program_name);
301
302 printf(
303"Commands:\n"
304"Either long or short options are allowed.\n"
305" --append -A chain Append to chain\n"
306" --delete -D chain Delete matching rule from chain\n"
307" --delete -D chain rulenum\n"
308" Delete rule rulenum (1 = first) from chain\n"
309" --insert -I chain [rulenum]\n"
310" Insert in chain as rulenum (default 1=first)\n"
311" --replace -R chain rulenum\n"
312" Replace rule rulenum (1 = first) in chain\n"
313" --list -L [chain] List the rules in a chain or all chains\n"
314" --flush -F [chain] Delete all rules in chain or all chains\n"
315" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000316" --new -N chain Create a new user-defined chain\n"
317" --delete-chain\n"
318" -X [chain] Delete a user-defined chain\n"
319" --policy -P chain target\n"
320" Change policy on chain to target\n"
321" --rename-chain\n"
322" -E old-chain new-chain\n"
323" Change chain name, (moving any references)\n"
324
325"Options:\n"
326" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
327" --source -s [!] address[/mask]\n"
328" source specification\n"
329" --destination -d [!] address[/mask]\n"
330" destination specification\n"
331" --in-interface -i [!] input name[+]\n"
332" network interface name ([+] for wildcard)\n"
333" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000334" target for rule (may load target extension)\n"
335" --match -m match\n"
336" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000337" --numeric -n numeric output of addresses and ports\n"
338" --out-interface -o [!] output name[+]\n"
339" network interface name ([+] for wildcard)\n"
340" --table -t table table to manipulate (default: `filter')\n"
341" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000342" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000343" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000344/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000345" --modprobe=<command> try to insert modules using this command\n"
346" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000347"[!] --version -V print package version.\n");
348
349 /* Print out any special helps. A user might like to be able to add a --help
350 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351 specified matches & targets */
352 for (t = ip6tables_targets; t; t = t->next) {
353 if (t->used) {
354 printf("\n");
355 t->help();
356 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000357 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000358 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000359 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000360 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361 }
362 exit(0);
363}
364
365static void
366generic_opt_check(int command, int options)
367{
368 int i, j, legal = 0;
369
370 /* Check that commands are valid with options. Complicated by the
371 * fact that if an option is legal with *any* command given, it is
372 * legal overall (ie. -z and -l).
373 */
374 for (i = 0; i < NUMBER_OF_OPT; i++) {
375 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
376
377 for (j = 0; j < NUMBER_OF_CMD; j++) {
378 if (!(command & (1<<j)))
379 continue;
380
381 if (!(options & (1<<i))) {
382 if (commands_v_options[j][i] == '+')
383 exit_error(PARAMETER_PROBLEM,
384 "You need to supply the `-%c' "
385 "option for this command\n",
386 optflags[i]);
387 } else {
388 if (commands_v_options[j][i] != 'x')
389 legal = 1;
390 else if (legal == 0)
391 legal = -1;
392 }
393 }
394 if (legal == -1)
395 exit_error(PARAMETER_PROBLEM,
396 "Illegal option `-%c' with this command\n",
397 optflags[i]);
398 }
399}
400
401static char
402opt2char(int option)
403{
404 const char *ptr;
405 for (ptr = optflags; option > 1; option >>= 1, ptr++);
406
407 return *ptr;
408}
409
410static char
411cmd2char(int option)
412{
413 const char *ptr;
414 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
415
416 return *ptr;
417}
418
419static void
420add_command(int *cmd, const int newcmd, const int othercmds, int invert)
421{
422 if (invert)
423 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
424 if (*cmd & (~othercmds))
425 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
426 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
427 *cmd |= newcmd;
428}
429
430int
Harald Welteb77f1da2002-03-14 11:35:58 +0000431check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000432{
433 if (option && strcmp(option, "!") == 0) {
434 if (*invert)
435 exit_error(PARAMETER_PROBLEM,
436 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000438 if (optind) {
439 *optind = *optind+1;
440 if (argc && *optind > argc)
441 exit_error(PARAMETER_PROBLEM,
442 "no argument following `!'");
443 }
444
Rusty Russell5eed48a2000-06-02 20:12:24 +0000445 return TRUE;
446 }
447 return FALSE;
448}
449
450static void *
451fw_calloc(size_t count, size_t size)
452{
453 void *p;
454
455 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000456 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000457 exit(1);
458 }
459 return p;
460}
461
462static void *
463fw_malloc(size_t size)
464{
465 void *p;
466
467 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000468 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469 exit(1);
470 }
471 return p;
472}
473
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474static char *
475addr_to_numeric(const struct in6_addr *addrp)
476{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000477 /* 0000:0000:0000:0000:0000:000.000.000.000
478 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000479 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000480 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000481}
482
483static struct in6_addr *
484numeric_to_addr(const char *num)
485{
486 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000487 int err;
488 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000490#ifdef DEBUG
491 fprintf(stderr, "\nnumeric2addr: %d\n", err);
492#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000493 return (struct in6_addr *)NULL;
494}
495
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000496
497static struct in6_addr *
498host_to_addr(const char *name, unsigned int *naddr)
499{
500 struct addrinfo hints;
501 struct addrinfo *res;
502 static struct in6_addr *addr;
503 int err;
504
505 memset(&hints, 0, sizeof(hints));
506 hints.ai_flags=AI_CANONNAME;
507 hints.ai_family=AF_INET6;
508 hints.ai_socktype=SOCK_RAW;
509 hints.ai_protocol=41;
510 hints.ai_next=NULL;
511
512 *naddr = 0;
513 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
514#ifdef DEBUG
515 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
516#endif
517 return (struct in6_addr *) NULL;
518 } else {
519 if (res->ai_family != AF_INET6 ||
520 res->ai_addrlen != sizeof(struct sockaddr_in6))
521 return (struct in6_addr *) NULL;
522
523#ifdef DEBUG
524 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
525 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
526#endif
527 /* Get the first element of the address-chain */
528 addr = fw_calloc(1, sizeof(struct in6_addr));
529 in6addrcpy(addr, (struct in6_addr *)
530 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
531 freeaddrinfo(res);
532 *naddr = 1;
533 return addr;
534 }
535
536 return (struct in6_addr *) NULL;
537}
538
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000539static char *
540addr_to_host(const struct in6_addr *addr)
541{
542 struct sockaddr_in6 saddr;
543 int err;
544 static char hostname[NI_MAXHOST];
545
546 memset(&saddr, 0, sizeof(struct sockaddr_in6));
547 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000548 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000549
550 if ( (err=getnameinfo((struct sockaddr *)&saddr,
551 sizeof(struct sockaddr_in6),
552 hostname, sizeof(hostname)-1,
553 NULL, 0, 0)) != 0 ){
554#ifdef DEBUG
555 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
556#endif
557 return (char *) NULL;
558 } else {
559#ifdef DEBUG
560 fprintf (stderr, "\naddr2host: %s\n", hostname);
561#endif
562
563 return hostname;
564 }
565
566 return (char *) NULL;
567}
568
Rusty Russell5eed48a2000-06-02 20:12:24 +0000569static char *
570mask_to_numeric(const struct in6_addr *addrp)
571{
Harald Welte8a50ab82003-06-24 18:15:59 +0000572 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000573 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000574 if (l == -1) {
575 strcpy(buf, "/");
576 strcat(buf, addr_to_numeric(addrp));
577 return buf;
578 }
Harald Welte43ac1912002-03-03 09:43:07 +0000579 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000580 return buf;
581}
582
583static struct in6_addr *
584network_to_addr(const char *name)
585{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000586 /* abort();*/
587 /* TODO: not implemented yet, but the exception breaks the
588 * name resolvation */
589 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000590}
591
592static char *
593addr_to_anyname(const struct in6_addr *addr)
594{
595 char *name;
596
597 if ((name = addr_to_host(addr)) != NULL)
598 return name;
599
600 return addr_to_numeric(addr);
601}
602
603/*
604 * All functions starting with "parse" should succeed, otherwise
605 * the program fails.
606 * Most routines return pointers to static data that may change
607 * between calls to the same or other routines with a few exceptions:
608 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
609 * return global static data.
610*/
611
612static struct in6_addr *
613parse_hostnetwork(const char *name, unsigned int *naddrs)
614{
615 struct in6_addr *addrp, *addrptmp;
616
617 if ((addrptmp = numeric_to_addr(name)) != NULL ||
618 (addrptmp = network_to_addr(name)) != NULL) {
619 addrp = fw_malloc(sizeof(struct in6_addr));
620 in6addrcpy(addrp, addrptmp);
621 *naddrs = 1;
622 return addrp;
623 }
624 if ((addrp = host_to_addr(name, naddrs)) != NULL)
625 return addrp;
626
627 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
628}
629
630static struct in6_addr *
631parse_mask(char *mask)
632{
633 static struct in6_addr maskaddr;
634 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000635 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000636
637 if (mask == NULL) {
638 /* no mask at all defaults to 128 bits */
639 memset(&maskaddr, 0xff, sizeof maskaddr);
640 return &maskaddr;
641 }
642 if ((addrp = numeric_to_addr(mask)) != NULL)
643 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000644 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000645 exit_error(PARAMETER_PROBLEM,
646 "invalid mask `%s' specified", mask);
647 if (bits != 0) {
648 char *p = (char *)&maskaddr;
649 memset(p, 0xff, bits / 8);
650 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
651 p[bits / 8] = 0xff << (8 - (bits & 7));
652 return &maskaddr;
653 }
654
655 memset(&maskaddr, 0, sizeof maskaddr);
656 return &maskaddr;
657}
658
Harald Welte43ac1912002-03-03 09:43:07 +0000659void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
661 struct in6_addr *maskp, unsigned int *naddrs)
662{
663 struct in6_addr *addrp;
664 char buf[256];
665 char *p;
666 int i, j, n;
667
668 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000669 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000670 if ((p = strrchr(buf, '/')) != NULL) {
671 *p = '\0';
672 addrp = parse_mask(p + 1);
673 } else
674 addrp = parse_mask(NULL);
675 in6addrcpy(maskp, addrp);
676
677 /* if a null mask is given, the name is ignored, like in "any/0" */
678 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
679 strcpy(buf, "::");
680
681 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
682 n = *naddrs;
683 for (i = 0, j = 0; i < n; i++) {
684 int k;
685 for (k = 0; k < 4; k++)
686 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
687 j++;
688 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000689 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000690 (*naddrs)--;
691 j--;
692 break;
693 }
694 }
695 }
696}
697
698struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000699find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700{
701 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000702 int icmphack = 0;
703
704 /* This is ugly as hell. Nonetheless, there is no way of changing
705 * this without hurting backwards compatibility */
706 if ( (strcmp(name,"icmpv6") == 0) ||
707 (strcmp(name,"ipv6-icmp") == 0) ||
708 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
709
710 if (!icmphack) {
711 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
712 if (strcmp(name, ptr->name) == 0)
713 break;
714 }
715 } else {
716 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
717 if (strcmp("icmp6", ptr->name) == 0)
718 break;
719 }
720 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000721
Harald Welte3efb6ea2001-08-06 18:50:21 +0000722#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000723 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000724 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000725 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000726 if (!icmphack)
Rusty Russell208d42e2004-12-20 05:29:52 +0000727 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Harald Weltecfaed1f2001-10-04 08:11:44 +0000728 else
Rusty Russell208d42e2004-12-20 05:29:52 +0000729 sprintf(path, "%s/libip6t_%s.so", lib_dir, "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730 if (dlopen(path, RTLD_NOW)) {
731 /* Found library. If it didn't register itself,
732 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000733 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000734
735 if (!ptr)
736 exit_error(PARAMETER_PROBLEM,
737 "Couldn't load match `%s'\n",
738 name);
739 } else if (tryload == LOAD_MUST_SUCCEED)
740 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000741 "Couldn't load match `%s':%s\n",
742 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000744#else
745 if (ptr && !ptr->loaded) {
746 if (tryload != DONT_LOAD)
747 ptr->loaded = 1;
748 else
749 ptr = NULL;
750 }
Marc Boucher067477b2002-03-24 15:09:31 +0000751 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
752 exit_error(PARAMETER_PROBLEM,
753 "Couldn't find match `%s'\n", name);
754 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000755#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000757 if (ptr && matches) {
758 struct ip6tables_rule_match **i;
759 struct ip6tables_rule_match *newentry;
760
761 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
762
763 for (i = matches; *i; i = &(*i)->next);
764 newentry->match = ptr;
765 newentry->next = NULL;
766 *i = newentry;
767 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000768
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769 return ptr;
770}
771
772/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
773static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000774find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000775{
Harald Welteed498492001-07-23 01:24:22 +0000776 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777
Harald Welte43ac1912002-03-03 09:43:07 +0000778 if (string_to_number(pname, 0, 255, &proto) != -1) {
779 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000780
Harald Welte43ac1912002-03-03 09:43:07 +0000781 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000782 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000783 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000784 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000785
786 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000787}
788
Harald Welte43ac1912002-03-03 09:43:07 +0000789u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790parse_protocol(const char *s)
791{
Harald Welteed498492001-07-23 01:24:22 +0000792 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000793
Harald Welteed498492001-07-23 01:24:22 +0000794 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 struct protoent *pent;
796
797 if ((pent = getprotobyname(s)))
798 proto = pent->p_proto;
799 else {
800 unsigned int i;
801 for (i = 0;
802 i < sizeof(chain_protos)/sizeof(struct pprot);
803 i++) {
804 if (strcmp(s, chain_protos[i].name) == 0) {
805 proto = chain_protos[i].num;
806 break;
807 }
808 }
809 if (i == sizeof(chain_protos)/sizeof(struct pprot))
810 exit_error(PARAMETER_PROBLEM,
811 "unknown protocol `%s' specified",
812 s);
813 }
814 }
815
816 return (u_int16_t)proto;
817}
818
819static void
820parse_interface(const char *arg, char *vianame, unsigned char *mask)
821{
822 int vialen = strlen(arg);
823 unsigned int i;
824
825 memset(mask, 0, IFNAMSIZ);
826 memset(vianame, 0, IFNAMSIZ);
827
828 if (vialen + 1 > IFNAMSIZ)
829 exit_error(PARAMETER_PROBLEM,
830 "interface name `%s' must be shorter than IFNAMSIZ"
831 " (%i)", arg, IFNAMSIZ-1);
832
833 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000834 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000835 memset(mask, 0, IFNAMSIZ);
836 else if (vianame[vialen - 1] == '+') {
837 memset(mask, 0xFF, vialen - 1);
838 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000839 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840 } else {
841 /* Include nul-terminator in match */
842 memset(mask, 0xFF, vialen + 1);
843 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000844 for (i = 0; vianame[i]; i++) {
845 if (!isalnum(vianame[i])
846 && vianame[i] != '_'
847 && vianame[i] != '.') {
848 printf("Warning: wierd character in interface"
849 " `%s' (No aliases, :, ! or *).\n",
850 vianame);
851 break;
852 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853 }
854 }
855}
856
857/* Can't be zero. */
858static int
859parse_rulenumber(const char *rule)
860{
Harald Welte43ac1912002-03-03 09:43:07 +0000861 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862
Harald Welte43ac1912002-03-03 09:43:07 +0000863 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864 exit_error(PARAMETER_PROBLEM,
865 "Invalid rule number `%s'", rule);
866
867 return rulenum;
868}
869
870static const char *
871parse_target(const char *targetname)
872{
873 const char *ptr;
874
875 if (strlen(targetname) < 1)
876 exit_error(PARAMETER_PROBLEM,
877 "Invalid target name (too short)");
878
879 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
880 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000881 "Invalid target name `%s' (%u chars max)",
882 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000883
884 for (ptr = targetname; *ptr; ptr++)
885 if (isspace(*ptr))
886 exit_error(PARAMETER_PROBLEM,
887 "Invalid target name `%s'", targetname);
888 return targetname;
889}
890
891int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000892string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
893 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000894{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000895 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896 char *end;
897
898 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000899 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000900 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000901 if (*end == '\0' && end != s) {
902 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000903 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000904 *ret = number;
905 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000906 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000907 }
908 return -1;
909}
910
Martin Josefssonb105bc92004-05-26 15:54:49 +0000911int
912string_to_number_l(const char *s, unsigned long min, unsigned long max,
913 unsigned long *ret)
914{
915 int result;
916 unsigned long long number;
917
918 result = string_to_number_ll(s, min, max, &number);
919 *ret = (unsigned long)number;
920
921 return result;
922}
923
924int string_to_number(const char *s, unsigned int min, unsigned int max,
925 unsigned int *ret)
926{
927 int result;
928 unsigned long number;
929
930 result = string_to_number_l(s, min, max, &number);
931 *ret = (unsigned int)number;
932
933 return result;
934}
935
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936static void
937set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
938 int invert)
939{
940 if (*options & option)
941 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
942 opt2char(option));
943 *options |= option;
944
945 if (invert) {
946 unsigned int i;
947 for (i = 0; 1 << i != option; i++);
948
949 if (!inverse_for_options[i])
950 exit_error(PARAMETER_PROBLEM,
951 "cannot have ! before -%c",
952 opt2char(option));
953 *invflg |= inverse_for_options[i];
954 }
955}
956
957struct ip6tables_target *
958find_target(const char *name, enum ip6t_tryload tryload)
959{
960 struct ip6tables_target *ptr;
961
962 /* Standard target? */
963 if (strcmp(name, "") == 0
964 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
965 || strcmp(name, IP6TC_LABEL_DROP) == 0
966 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
967 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
968 name = "standard";
969
970 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
971 if (strcmp(name, ptr->name) == 0)
972 break;
973 }
974
Harald Welte3efb6ea2001-08-06 18:50:21 +0000975#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000977 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000979 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 if (dlopen(path, RTLD_NOW)) {
981 /* Found library. If it didn't register itself,
982 maybe they specified match as a target. */
983 ptr = find_target(name, DONT_LOAD);
984 if (!ptr)
985 exit_error(PARAMETER_PROBLEM,
986 "Couldn't load target `%s'\n",
987 name);
988 } else if (tryload == LOAD_MUST_SUCCEED)
989 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000990 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000991 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000992 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000993#else
994 if (ptr && !ptr->loaded) {
995 if (tryload != DONT_LOAD)
996 ptr->loaded = 1;
997 else
998 ptr = NULL;
999 }
Marc Boucher067477b2002-03-24 15:09:31 +00001000 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1001 exit_error(PARAMETER_PROBLEM,
1002 "Couldn't find target `%s'\n", name);
1003 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001004#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001005
Harald Welte43ac1912002-03-03 09:43:07 +00001006 if (ptr)
1007 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001008
Rusty Russell5eed48a2000-06-02 20:12:24 +00001009 return ptr;
1010}
1011
1012static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001013merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001014 unsigned int *option_offset)
1015{
1016 unsigned int num_old, num_new, i;
1017 struct option *merge;
1018
1019 for (num_old = 0; oldopts[num_old].name; num_old++);
1020 for (num_new = 0; newopts[num_new].name; num_new++);
1021
1022 global_option_offset += OPTION_OFFSET;
1023 *option_offset = global_option_offset;
1024
1025 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1026 memcpy(merge, oldopts, num_old * sizeof(struct option));
1027 for (i = 0; i < num_new; i++) {
1028 merge[num_old + i] = newopts[i];
1029 merge[num_old + i].val += *option_offset;
1030 }
1031 memset(merge + num_old + num_new, 0, sizeof(struct option));
1032
1033 return merge;
1034}
1035
1036void
1037register_match6(struct ip6tables_match *me)
1038{
Harald Welte43ac1912002-03-03 09:43:07 +00001039 struct ip6tables_match **i;
1040
Rusty Russell5eed48a2000-06-02 20:12:24 +00001041 if (strcmp(me->version, program_version) != 0) {
1042 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1043 program_name, me->name, me->version, program_version);
1044 exit(1);
1045 }
1046
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001047 if (find_match(me->name, DONT_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 fprintf(stderr, "%s: match `%s' already registered.\n",
1049 program_name, me->name);
1050 exit(1);
1051 }
1052
Harald Welte43ac1912002-03-03 09:43:07 +00001053 if (me->size != IP6T_ALIGN(me->size)) {
1054 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001055 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001056 exit(1);
1057 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001058
Harald Welte43ac1912002-03-03 09:43:07 +00001059 /* Append to list. */
1060 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1061 me->next = NULL;
1062 *i = me;
1063
Rusty Russell5eed48a2000-06-02 20:12:24 +00001064 me->m = NULL;
1065 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001066}
1067
1068void
1069register_target6(struct ip6tables_target *me)
1070{
1071 if (strcmp(me->version, program_version) != 0) {
1072 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1073 program_name, me->name, me->version, program_version);
1074 exit(1);
1075 }
1076
1077 if (find_target(me->name, DONT_LOAD)) {
1078 fprintf(stderr, "%s: target `%s' already registered.\n",
1079 program_name, me->name);
1080 exit(1);
1081 }
1082
Harald Welte43ac1912002-03-03 09:43:07 +00001083 if (me->size != IP6T_ALIGN(me->size)) {
1084 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001085 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001086 exit(1);
1087 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001088
Rusty Russell5eed48a2000-06-02 20:12:24 +00001089 /* Prepend to list. */
1090 me->next = ip6tables_targets;
1091 ip6tables_targets = me;
1092 me->t = NULL;
1093 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001094}
1095
1096static void
1097print_num(u_int64_t number, unsigned int format)
1098{
Harald Welte43ac1912002-03-03 09:43:07 +00001099 if (format & FMT_KILOMEGAGIGA) {
1100 if (number > 99999) {
1101 number = (number + 500) / 1000;
1102 if (number > 9999) {
1103 number = (number + 500) / 1000;
1104 if (number > 9999) {
1105 number = (number + 500) / 1000;
1106 if (number > 9999) {
1107 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001108 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001109 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001110 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001111 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001112 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001113 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001114 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001115 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001116 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001117 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001118 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001119}
1120
Harald Welte43ac1912002-03-03 09:43:07 +00001121
Rusty Russell5eed48a2000-06-02 20:12:24 +00001122static void
1123print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1124{
1125 struct ip6t_counters counters;
1126 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1127 printf("Chain %s", chain);
1128 if (pol) {
1129 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001130 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001131 fputc(' ', stdout);
1132 print_num(counters.pcnt, (format|FMT_NOTABLE));
1133 fputs("packets, ", stdout);
1134 print_num(counters.bcnt, (format|FMT_NOTABLE));
1135 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001136 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001137 printf(")\n");
1138 } else {
1139 unsigned int refs;
1140 if (!ip6tc_get_references(&refs, chain, handle))
1141 printf(" (ERROR obtaining refs)\n");
1142 else
1143 printf(" (%u references)\n", refs);
1144 }
1145
1146 if (format & FMT_LINENUMBERS)
1147 printf(FMT("%-4s ", "%s "), "num");
1148 if (!(format & FMT_NOCOUNTS)) {
1149 if (format & FMT_KILOMEGAGIGA) {
1150 printf(FMT("%5s ","%s "), "pkts");
1151 printf(FMT("%5s ","%s "), "bytes");
1152 } else {
1153 printf(FMT("%8s ","%s "), "pkts");
1154 printf(FMT("%10s ","%s "), "bytes");
1155 }
1156 }
1157 if (!(format & FMT_NOTARGET))
1158 printf(FMT("%-9s ","%s "), "target");
1159 fputs(" prot ", stdout);
1160 if (format & FMT_OPTIONS)
1161 fputs("opt", stdout);
1162 if (format & FMT_VIA) {
1163 printf(FMT(" %-6s ","%s "), "in");
1164 printf(FMT("%-6s ","%s "), "out");
1165 }
1166 printf(FMT(" %-19s ","%s "), "source");
1167 printf(FMT(" %-19s "," %s "), "destination");
1168 printf("\n");
1169}
1170
Harald Welte43ac1912002-03-03 09:43:07 +00001171
Rusty Russell5eed48a2000-06-02 20:12:24 +00001172static int
1173print_match(const struct ip6t_entry_match *m,
1174 const struct ip6t_ip6 *ip,
1175 int numeric)
1176{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001177 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001178
1179 if (match) {
1180 if (match->print)
1181 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001182 else
1183 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001184 } else {
1185 if (m->u.user.name[0])
1186 printf("UNKNOWN match `%s' ", m->u.user.name);
1187 }
1188 /* Don't stop iterating. */
1189 return 0;
1190}
1191
1192/* e is called `fw' here for hysterical raisins */
1193static void
1194print_firewall(const struct ip6t_entry *fw,
1195 const char *targname,
1196 unsigned int num,
1197 unsigned int format,
1198 const ip6tc_handle_t handle)
1199{
1200 struct ip6tables_target *target = NULL;
1201 const struct ip6t_entry_target *t;
1202 u_int8_t flags;
1203 char buf[BUFSIZ];
1204
Rusty Russell5eed48a2000-06-02 20:12:24 +00001205 if (!ip6tc_is_chain(targname, handle))
1206 target = find_target(targname, TRY_LOAD);
1207 else
1208 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1209
1210 t = ip6t_get_target((struct ip6t_entry *)fw);
1211 flags = fw->ipv6.flags;
1212
1213 if (format & FMT_LINENUMBERS)
1214 printf(FMT("%-4u ", "%u "), num+1);
1215
1216 if (!(format & FMT_NOCOUNTS)) {
1217 print_num(fw->counters.pcnt, format);
1218 print_num(fw->counters.bcnt, format);
1219 }
1220
1221 if (!(format & FMT_NOTARGET))
1222 printf(FMT("%-9s ", "%s "), targname);
1223
1224 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1225 {
Harald Welte43ac1912002-03-03 09:43:07 +00001226 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001227 if (pname)
1228 printf(FMT("%-5s", "%s "), pname);
1229 else
1230 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1231 }
1232
1233 if (format & FMT_OPTIONS) {
1234 if (format & FMT_NOTABLE)
1235 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001236 fputc(' ', stdout); /* Invert flag of FRAG */
1237 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001238 fputc(' ', stdout);
1239 }
1240
1241 if (format & FMT_VIA) {
1242 char iface[IFNAMSIZ+2];
1243
1244 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1245 iface[0] = '!';
1246 iface[1] = '\0';
1247 }
1248 else iface[0] = '\0';
1249
1250 if (fw->ipv6.iniface[0] != '\0') {
1251 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001252 }
1253 else if (format & FMT_NUMERIC) strcat(iface, "*");
1254 else strcat(iface, "any");
1255 printf(FMT(" %-6s ","in %s "), iface);
1256
1257 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1258 iface[0] = '!';
1259 iface[1] = '\0';
1260 }
1261 else iface[0] = '\0';
1262
1263 if (fw->ipv6.outiface[0] != '\0') {
1264 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001265 }
1266 else if (format & FMT_NUMERIC) strcat(iface, "*");
1267 else strcat(iface, "any");
1268 printf(FMT("%-6s ","out %s "), iface);
1269 }
1270
1271 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1272 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1273 && !(format & FMT_NUMERIC))
1274 printf(FMT("%-19s ","%s "), "anywhere");
1275 else {
1276 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001277 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001278 else
Harald Welte43ac1912002-03-03 09:43:07 +00001279 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001280 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1281 printf(FMT("%-19s ","%s "), buf);
1282 }
1283
1284 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1285 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1286 && !(format & FMT_NUMERIC))
1287 printf(FMT("%-19s","-> %s"), "anywhere");
1288 else {
1289 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001290 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001291 else
Harald Welte43ac1912002-03-03 09:43:07 +00001292 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1294 printf(FMT("%-19s","-> %s"), buf);
1295 }
1296
1297 if (format & FMT_NOTABLE)
1298 fputs(" ", stdout);
1299
1300 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1301
1302 if (target) {
1303 if (target->print)
1304 /* Print the target information. */
1305 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1306 } else if (t->u.target_size != sizeof(*t))
1307 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001308 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001309
1310 if (!(format & FMT_NONEWLINE))
1311 fputc('\n', stdout);
1312}
1313
1314static void
1315print_firewall_line(const struct ip6t_entry *fw,
1316 const ip6tc_handle_t h)
1317{
1318 struct ip6t_entry_target *t;
1319
1320 t = ip6t_get_target((struct ip6t_entry *)fw);
1321 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1322}
1323
1324static int
1325append_entry(const ip6t_chainlabel chain,
1326 struct ip6t_entry *fw,
1327 unsigned int nsaddrs,
1328 const struct in6_addr saddrs[],
1329 unsigned int ndaddrs,
1330 const struct in6_addr daddrs[],
1331 int verbose,
1332 ip6tc_handle_t *handle)
1333{
1334 unsigned int i, j;
1335 int ret = 1;
1336
1337 for (i = 0; i < nsaddrs; i++) {
1338 fw->ipv6.src = saddrs[i];
1339 for (j = 0; j < ndaddrs; j++) {
1340 fw->ipv6.dst = daddrs[j];
1341 if (verbose)
1342 print_firewall_line(fw, *handle);
1343 ret &= ip6tc_append_entry(chain, fw, handle);
1344 }
1345 }
1346
1347 return ret;
1348}
1349
1350static int
1351replace_entry(const ip6t_chainlabel chain,
1352 struct ip6t_entry *fw,
1353 unsigned int rulenum,
1354 const struct in6_addr *saddr,
1355 const struct in6_addr *daddr,
1356 int verbose,
1357 ip6tc_handle_t *handle)
1358{
1359 fw->ipv6.src = *saddr;
1360 fw->ipv6.dst = *daddr;
1361
1362 if (verbose)
1363 print_firewall_line(fw, *handle);
1364 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1365}
1366
1367static int
1368insert_entry(const ip6t_chainlabel chain,
1369 struct ip6t_entry *fw,
1370 unsigned int rulenum,
1371 unsigned int nsaddrs,
1372 const struct in6_addr saddrs[],
1373 unsigned int ndaddrs,
1374 const struct in6_addr daddrs[],
1375 int verbose,
1376 ip6tc_handle_t *handle)
1377{
1378 unsigned int i, j;
1379 int ret = 1;
1380
1381 for (i = 0; i < nsaddrs; i++) {
1382 fw->ipv6.src = saddrs[i];
1383 for (j = 0; j < ndaddrs; j++) {
1384 fw->ipv6.dst = daddrs[j];
1385 if (verbose)
1386 print_firewall_line(fw, *handle);
1387 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1388 }
1389 }
1390
1391 return ret;
1392}
1393
1394static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001395make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396{
1397 /* Establish mask for comparison */
1398 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001399 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001400 unsigned char *mask, *mptr;
1401
1402 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001403 for (matchp = matches; matchp; matchp = matchp->next)
1404 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405
1406 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001407 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001408 + ip6tables_targets->size);
1409
1410 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1411 mptr = mask + sizeof(struct ip6t_entry);
1412
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001413 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001415 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001416 + matchp->match->userspacesize);
1417 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418 }
1419
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001420 memset(mptr, 0xFF,
1421 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1422 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423
1424 return mask;
1425}
1426
1427static int
1428delete_entry(const ip6t_chainlabel chain,
1429 struct ip6t_entry *fw,
1430 unsigned int nsaddrs,
1431 const struct in6_addr saddrs[],
1432 unsigned int ndaddrs,
1433 const struct in6_addr daddrs[],
1434 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001435 ip6tc_handle_t *handle,
1436 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001437{
1438 unsigned int i, j;
1439 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440 unsigned char *mask;
1441
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001442 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001444 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001445 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001446 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001447 if (verbose)
1448 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001449 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001450 }
1451 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001452 free(mask);
1453
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454 return ret;
1455}
1456
András Kis-Szabó764316a2001-02-26 17:31:20 +00001457int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1459 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1460{
1461 int ret = 1;
1462 const char *chain;
1463 char *chains;
1464 unsigned int i, chaincount = 0;
1465
1466 chain = ip6tc_first_chain(handle);
1467 while (chain) {
1468 chaincount++;
1469 chain = ip6tc_next_chain(handle);
1470 }
1471
1472 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1473 i = 0;
1474 chain = ip6tc_first_chain(handle);
1475 while (chain) {
1476 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1477 i++;
1478 chain = ip6tc_next_chain(handle);
1479 }
1480
1481 for (i = 0; i < chaincount; i++) {
1482 if (!builtinstoo
1483 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001484 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001485 continue;
1486 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1487 }
1488
1489 free(chains);
1490 return ret;
1491}
1492
András Kis-Szabó764316a2001-02-26 17:31:20 +00001493int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494flush_entries(const ip6t_chainlabel chain, int verbose,
1495 ip6tc_handle_t *handle)
1496{
1497 if (!chain)
1498 return for_each_chain(flush_entries, verbose, 1, handle);
1499
1500 if (verbose)
1501 fprintf(stdout, "Flushing chain `%s'\n", chain);
1502 return ip6tc_flush_entries(chain, handle);
1503}
1504
1505static int
1506zero_entries(const ip6t_chainlabel chain, int verbose,
1507 ip6tc_handle_t *handle)
1508{
1509 if (!chain)
1510 return for_each_chain(zero_entries, verbose, 1, handle);
1511
1512 if (verbose)
1513 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1514 return ip6tc_zero_entries(chain, handle);
1515}
1516
András Kis-Szabó764316a2001-02-26 17:31:20 +00001517int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001518delete_chain(const ip6t_chainlabel chain, int verbose,
1519 ip6tc_handle_t *handle)
1520{
1521 if (!chain)
1522 return for_each_chain(delete_chain, verbose, 0, handle);
1523
1524 if (verbose)
1525 fprintf(stdout, "Deleting chain `%s'\n", chain);
1526 return ip6tc_delete_chain(chain, handle);
1527}
1528
1529static int
1530list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1531 int expanded, int linenumbers, ip6tc_handle_t *handle)
1532{
1533 int found = 0;
1534 unsigned int format;
1535 const char *this;
1536
1537 format = FMT_OPTIONS;
1538 if (!verbose)
1539 format |= FMT_NOCOUNTS;
1540 else
1541 format |= FMT_VIA;
1542
1543 if (numeric)
1544 format |= FMT_NUMERIC;
1545
1546 if (!expanded)
1547 format |= FMT_KILOMEGAGIGA;
1548
1549 if (linenumbers)
1550 format |= FMT_LINENUMBERS;
1551
1552 for (this = ip6tc_first_chain(handle);
1553 this;
1554 this = ip6tc_next_chain(handle)) {
1555 const struct ip6t_entry *i;
1556 unsigned int num;
1557
1558 if (chain && strcmp(chain, this) != 0)
1559 continue;
1560
1561 if (found) printf("\n");
1562
1563 print_header(format, this, handle);
1564 i = ip6tc_first_rule(this, handle);
1565
1566 num = 0;
1567 while (i) {
1568 print_firewall(i,
1569 ip6tc_get_target(i, handle),
1570 num++,
1571 format,
1572 *handle);
1573 i = ip6tc_next_rule(i, handle);
1574 }
1575 found = 1;
1576 }
1577
1578 errno = ENOENT;
1579 return found;
1580}
1581
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001582static char *get_modprobe(void)
1583{
Harald Welte43ac1912002-03-03 09:43:07 +00001584 int procfile;
1585 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001586
Harald Welte10f7f142004-10-22 08:14:07 +00001587#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001588 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1589 if (procfile < 0)
1590 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001591
Harald Welte10f7f142004-10-22 08:14:07 +00001592 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001593 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001594 memset(ret, 0, PROCFILE_BUFSIZ);
1595 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001596 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001597 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001598 }
1599 if (ret[strlen(ret)-1]=='\n')
1600 ret[strlen(ret)-1]=0;
1601 close(procfile);
1602 return ret;
1603 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001604 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001605 free(ret);
1606 close(procfile);
1607 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001608}
1609
Harald Welte58918652001-06-16 18:25:25 +00001610int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001611{
Harald Welte43ac1912002-03-03 09:43:07 +00001612 char *buf = NULL;
1613 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001614 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001615
Harald Welte43ac1912002-03-03 09:43:07 +00001616 /* If they don't explicitly set it, read out of kernel */
1617 if (!modprobe) {
1618 buf = get_modprobe();
1619 if (!buf)
1620 return -1;
1621 modprobe = buf;
1622 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623
Harald Welte43ac1912002-03-03 09:43:07 +00001624 switch (fork()) {
1625 case 0:
1626 argv[0] = (char *)modprobe;
1627 argv[1] = (char *)modname;
1628 argv[2] = NULL;
1629 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001630
Harald Welte43ac1912002-03-03 09:43:07 +00001631 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001632 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001633 case -1:
1634 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001635
Harald Welte43ac1912002-03-03 09:43:07 +00001636 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001637 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001638 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001639
Harald Welte43ac1912002-03-03 09:43:07 +00001640 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001641 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1642 return 0;
1643 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001644}
1645
Rusty Russell5eed48a2000-06-02 20:12:24 +00001646static struct ip6t_entry *
1647generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001648 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649 struct ip6t_entry_target *target)
1650{
1651 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001652 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653 struct ip6t_entry *e;
1654
1655 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001656 for (matchp = matches; matchp; matchp = matchp->next)
1657 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658
1659 e = fw_malloc(size + target->u.target_size);
1660 *e = *fw;
1661 e->target_offset = size;
1662 e->next_offset = size + target->u.target_size;
1663
1664 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001665 for (matchp = matches; matchp; matchp = matchp->next) {
1666 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1667 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 }
1669 memcpy(e->elems + size, target, target->u.target_size);
1670
1671 return e;
1672}
1673
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001674void clear_rule_matches(struct ip6tables_rule_match **matches)
1675{
1676 struct ip6tables_rule_match *matchp, *tmp;
1677
1678 for (matchp = *matches; matchp;) {
1679 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001680 if (matchp->match->m)
1681 free(matchp->match->m);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001682 free(matchp);
1683 matchp = tmp;
1684 }
1685
1686 *matches = NULL;
1687}
1688
Rusty Russell5eed48a2000-06-02 20:12:24 +00001689int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1690{
1691 struct ip6t_entry fw, *e = NULL;
1692 int invert = 0;
1693 unsigned int nsaddrs = 0, ndaddrs = 0;
1694 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1695
1696 int c, verbose = 0;
1697 const char *chain = NULL;
1698 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1699 const char *policy = NULL, *newname = NULL;
1700 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001701 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001702 int ret = 1;
1703 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001704 struct ip6tables_rule_match *matches = NULL;
1705 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001707 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001708 const char *jumpto = "";
1709 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001710 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001711 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001712 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001713
1714 memset(&fw, 0, sizeof(fw));
1715
Harald Welte43ac1912002-03-03 09:43:07 +00001716 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001717 * a second time */
1718 optind = 0;
1719
Harald Welte43ac1912002-03-03 09:43:07 +00001720 /* clear mflags in case do_command gets called a second time
1721 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001722 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001723 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001724
Harald Welte43ac1912002-03-03 09:43:07 +00001725 for (t = ip6tables_targets; t; t = t->next) {
1726 t->tflags = 0;
1727 t->used = 0;
1728 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001729
Rusty Russell5eed48a2000-06-02 20:12:24 +00001730 /* Suppress error messages: we may add new options if we
1731 demand-load a protocol. */
1732 opterr = 0;
1733
1734 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001735 "-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 +00001736 opts, NULL)) != -1) {
1737 switch (c) {
1738 /*
1739 * Command selection
1740 */
1741 case 'A':
1742 add_command(&command, CMD_APPEND, CMD_NONE,
1743 invert);
1744 chain = optarg;
1745 break;
1746
1747 case 'D':
1748 add_command(&command, CMD_DELETE, CMD_NONE,
1749 invert);
1750 chain = optarg;
1751 if (optind < argc && argv[optind][0] != '-'
1752 && argv[optind][0] != '!') {
1753 rulenum = parse_rulenumber(argv[optind++]);
1754 command = CMD_DELETE_NUM;
1755 }
1756 break;
1757
Rusty Russell5eed48a2000-06-02 20:12:24 +00001758 case 'R':
1759 add_command(&command, CMD_REPLACE, CMD_NONE,
1760 invert);
1761 chain = optarg;
1762 if (optind < argc && argv[optind][0] != '-'
1763 && argv[optind][0] != '!')
1764 rulenum = parse_rulenumber(argv[optind++]);
1765 else
1766 exit_error(PARAMETER_PROBLEM,
1767 "-%c requires a rule number",
1768 cmd2char(CMD_REPLACE));
1769 break;
1770
1771 case 'I':
1772 add_command(&command, CMD_INSERT, CMD_NONE,
1773 invert);
1774 chain = optarg;
1775 if (optind < argc && argv[optind][0] != '-'
1776 && argv[optind][0] != '!')
1777 rulenum = parse_rulenumber(argv[optind++]);
1778 else rulenum = 1;
1779 break;
1780
1781 case 'L':
1782 add_command(&command, CMD_LIST, CMD_ZERO,
1783 invert);
1784 if (optarg) chain = optarg;
1785 else if (optind < argc && argv[optind][0] != '-'
1786 && argv[optind][0] != '!')
1787 chain = argv[optind++];
1788 break;
1789
1790 case 'F':
1791 add_command(&command, CMD_FLUSH, CMD_NONE,
1792 invert);
1793 if (optarg) chain = optarg;
1794 else if (optind < argc && argv[optind][0] != '-'
1795 && argv[optind][0] != '!')
1796 chain = argv[optind++];
1797 break;
1798
1799 case 'Z':
1800 add_command(&command, CMD_ZERO, CMD_LIST,
1801 invert);
1802 if (optarg) chain = optarg;
1803 else if (optind < argc && argv[optind][0] != '-'
1804 && argv[optind][0] != '!')
1805 chain = argv[optind++];
1806 break;
1807
1808 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001809 if (optarg && *optarg == '-')
1810 exit_error(PARAMETER_PROBLEM,
1811 "chain name not allowed to start "
1812 "with `-'\n");
1813 if (find_target(optarg, TRY_LOAD))
1814 exit_error(PARAMETER_PROBLEM,
1815 "chain name may not clash "
1816 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001817 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1818 invert);
1819 chain = optarg;
1820 break;
1821
1822 case 'X':
1823 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1824 invert);
1825 if (optarg) chain = optarg;
1826 else if (optind < argc && argv[optind][0] != '-'
1827 && argv[optind][0] != '!')
1828 chain = argv[optind++];
1829 break;
1830
1831 case 'E':
1832 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1833 invert);
1834 chain = optarg;
1835 if (optind < argc && argv[optind][0] != '-'
1836 && argv[optind][0] != '!')
1837 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001838 else
1839 exit_error(PARAMETER_PROBLEM,
1840 "-%c requires old-chain-name and "
1841 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001842 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001843 break;
1844
1845 case 'P':
1846 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1847 invert);
1848 chain = optarg;
1849 if (optind < argc && argv[optind][0] != '-'
1850 && argv[optind][0] != '!')
1851 policy = argv[optind++];
1852 else
1853 exit_error(PARAMETER_PROBLEM,
1854 "-%c requires a chain and a policy",
1855 cmd2char(CMD_SET_POLICY));
1856 break;
1857
1858 case 'h':
1859 if (!optarg)
1860 optarg = argv[optind];
1861
1862 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001863 if (!matches && protocol)
1864 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001865
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001866 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001867
1868 /*
1869 * Option selection
1870 */
1871 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001872 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1874 invert);
1875
1876 /* Canonicalize into lower case */
1877 for (protocol = argv[optind-1]; *protocol; protocol++)
1878 *protocol = tolower(*protocol);
1879
1880 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001881 if ( strcmp(protocol,"ipv6-icmp") == 0)
1882 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001883 fw.ipv6.proto = parse_protocol(protocol);
1884 fw.ipv6.flags |= IP6T_F_PROTO;
1885
1886 if (fw.ipv6.proto == 0
1887 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1888 exit_error(PARAMETER_PROBLEM,
1889 "rule would never match protocol");
1890 fw.nfcache |= NFC_IP6_PROTO;
1891 break;
1892
1893 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001894 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001895 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1896 invert);
1897 shostnetworkmask = argv[optind-1];
1898 fw.nfcache |= NFC_IP6_SRC;
1899 break;
1900
1901 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001902 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001903 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1904 invert);
1905 dhostnetworkmask = argv[optind-1];
1906 fw.nfcache |= NFC_IP6_DST;
1907 break;
1908
1909 case 'j':
1910 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1911 invert);
1912 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001913 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001914 target = find_target(jumpto, TRY_LOAD);
1915
1916 if (target) {
1917 size_t size;
1918
Harald Welte43ac1912002-03-03 09:43:07 +00001919 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1920 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921
1922 target->t = fw_calloc(1, size);
1923 target->t->u.target_size = size;
1924 strcpy(target->t->u.user.name, jumpto);
1925 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001926 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 }
1928 break;
1929
1930
1931 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001932 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1934 invert);
1935 parse_interface(argv[optind-1],
1936 fw.ipv6.iniface,
1937 fw.ipv6.iniface_mask);
1938 fw.nfcache |= NFC_IP6_IF_IN;
1939 break;
1940
1941 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001942 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001943 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1944 invert);
1945 parse_interface(argv[optind-1],
1946 fw.ipv6.outiface,
1947 fw.ipv6.outiface_mask);
1948 fw.nfcache |= NFC_IP6_IF_OUT;
1949 break;
1950
1951 case 'v':
1952 if (!verbose)
1953 set_option(&options, OPT_VERBOSE,
1954 &fw.ipv6.invflags, invert);
1955 verbose++;
1956 break;
1957
1958 case 'm': {
1959 size_t size;
1960
1961 if (invert)
1962 exit_error(PARAMETER_PROBLEM,
1963 "unexpected ! flag before --match");
1964
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001965 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001966 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1967 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 m->m = fw_calloc(1, size);
1969 m->m->u.match_size = size;
1970 strcpy(m->m->u.user.name, m->name);
1971 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001972 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001973 }
1974 break;
1975
1976 case 'n':
1977 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1978 invert);
1979 break;
1980
1981 case 't':
1982 if (invert)
1983 exit_error(PARAMETER_PROBLEM,
1984 "unexpected ! flag before --table");
1985 *table = argv[optind-1];
1986 break;
1987
1988 case 'x':
1989 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1990 invert);
1991 break;
1992
1993 case 'V':
1994 if (invert)
1995 printf("Not %s ;-)\n", program_version);
1996 else
1997 printf("%s v%s\n",
1998 program_name, program_version);
1999 exit(0);
2000
2001 case '0':
2002 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2003 invert);
2004 break;
2005
Harald Welte43ac1912002-03-03 09:43:07 +00002006 case 'M':
2007 modprobe = optarg;
2008 break;
2009
2010 case 'c':
2011
2012 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2013 invert);
2014 pcnt = optarg;
2015 if (optind < argc && argv[optind][0] != '-'
2016 && argv[optind][0] != '!')
2017 bcnt = argv[optind++];
2018 else
2019 exit_error(PARAMETER_PROBLEM,
2020 "-%c requires packet and byte counter",
2021 opt2char(OPT_COUNTERS));
2022
Martin Josefssona28d4952004-05-26 16:04:48 +00002023 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002024 exit_error(PARAMETER_PROBLEM,
2025 "-%c packet counter not numeric",
2026 opt2char(OPT_COUNTERS));
2027
Martin Josefssona28d4952004-05-26 16:04:48 +00002028 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002029 exit_error(PARAMETER_PROBLEM,
2030 "-%c byte counter not numeric",
2031 opt2char(OPT_COUNTERS));
2032
2033 break;
2034
2035
Rusty Russell5eed48a2000-06-02 20:12:24 +00002036 case 1: /* non option */
2037 if (optarg[0] == '!' && optarg[1] == '\0') {
2038 if (invert)
2039 exit_error(PARAMETER_PROBLEM,
2040 "multiple consecutive ! not"
2041 " allowed");
2042 invert = TRUE;
2043 optarg[0] = '\0';
2044 continue;
2045 }
2046 printf("Bad argument `%s'\n", optarg);
2047 exit_tryhelp(2);
2048
2049 default:
2050 /* FIXME: This scheme doesn't allow two of the same
2051 matches --RR */
2052 if (!target
2053 || !(target->parse(c - target->option_offset,
2054 argv, invert,
2055 &target->tflags,
2056 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002057 for (matchp = matches; matchp; matchp = matchp->next) {
2058 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002059 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002060 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 &fw,
2062 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002063 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002064 break;
2065 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002066 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002067
2068 /* If you listen carefully, you can
2069 actually hear this code suck. */
2070
2071 /* some explanations (after four different bugs
2072 * in 3 different releases): If we encountere a
2073 * parameter, that has not been parsed yet,
2074 * it's not an option of an explicitly loaded
2075 * match or a target. However, we support
2076 * implicit loading of the protocol match
2077 * extension. '-p tcp' means 'l4 proto 6' and
2078 * at the same time 'load tcp protocol match on
2079 * demand if we specify --dport'.
2080 *
2081 * To make this work, we need to make sure:
2082 * - the parameter has not been parsed by
2083 * a match (m above)
2084 * - a protocol has been specified
2085 * - the protocol extension has not been
2086 * loaded yet, or is loaded and unused
2087 * [think of iptables-restore!]
2088 * - the protocol extension can be successively
2089 * loaded
2090 */
2091 if (m == NULL
2092 && protocol
2093 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002094 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002095 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002096 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002097 && (proto_used == 0))
2098 )
2099 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002100 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002101 /* Try loading protocol */
2102 size_t size;
2103
2104 proto_used = 1;
2105
2106 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2107 + m->size;
2108
2109 m->m = fw_calloc(1, size);
2110 m->m->u.match_size = size;
2111 strcpy(m->m->u.user.name, m->name);
2112 m->init(m->m, &fw.nfcache);
2113
2114 opts = merge_options(opts,
2115 m->extra_opts, &m->option_offset);
2116
2117 optind--;
2118 continue;
2119 }
2120
Rusty Russell5eed48a2000-06-02 20:12:24 +00002121 if (!m)
2122 exit_error(PARAMETER_PROBLEM,
2123 "Unknown arg `%s'",
2124 argv[optind-1]);
2125 }
2126 }
2127 invert = FALSE;
2128 }
2129
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002130 for (matchp = matches; matchp; matchp = matchp->next)
2131 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002132
Harald Welte43ac1912002-03-03 09:43:07 +00002133 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002134 target->final_check(target->tflags);
2135
2136 /* Fix me: must put inverse options checking here --MN */
2137
2138 if (optind < argc)
2139 exit_error(PARAMETER_PROBLEM,
2140 "unknown arguments found on commandline");
2141 if (!command)
2142 exit_error(PARAMETER_PROBLEM, "no command specified");
2143 if (invert)
2144 exit_error(PARAMETER_PROBLEM,
2145 "nothing appropriate following !");
2146
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002147 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002148 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002149 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002150 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002151 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002152 }
2153
2154 if (shostnetworkmask)
2155 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2156 &(fw.ipv6.smsk), &nsaddrs);
2157
2158 if (dhostnetworkmask)
2159 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2160 &(fw.ipv6.dmsk), &ndaddrs);
2161
2162 if ((nsaddrs > 1 || ndaddrs > 1) &&
2163 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2164 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2165 " source or destination IP addresses");
2166
Rusty Russell5eed48a2000-06-02 20:12:24 +00002167 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2168 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2169 "specify a unique address");
2170
2171 generic_opt_check(command, options);
2172
2173 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2174 exit_error(PARAMETER_PROBLEM,
2175 "chain name `%s' too long (must be under %i chars)",
2176 chain, IP6T_FUNCTION_MAXNAMELEN);
2177
Harald Welte43ac1912002-03-03 09:43:07 +00002178 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002179 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002180 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002181
Rusty Russell8beb0492004-12-22 00:37:10 +00002182 /* try to insmod the module if iptc_init failed */
2183 if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002184 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002185
Harald Welte43ac1912002-03-03 09:43:07 +00002186 if (!*handle)
2187 exit_error(VERSION_PROBLEM,
2188 "can't initialize ip6tables table `%s': %s",
2189 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002190
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002191 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002192 || command == CMD_DELETE
2193 || command == CMD_INSERT
2194 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002195 if (strcmp(chain, "PREROUTING") == 0
2196 || strcmp(chain, "INPUT") == 0) {
2197 /* -o not valid with incoming packets. */
2198 if (options & OPT_VIANAMEOUT)
2199 exit_error(PARAMETER_PROBLEM,
2200 "Can't use -%c with %s\n",
2201 opt2char(OPT_VIANAMEOUT),
2202 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002203 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002204
Harald Welte43ac1912002-03-03 09:43:07 +00002205 if (strcmp(chain, "POSTROUTING") == 0
2206 || strcmp(chain, "OUTPUT") == 0) {
2207 /* -i not valid with outgoing packets */
2208 if (options & OPT_VIANAMEIN)
2209 exit_error(PARAMETER_PROBLEM,
2210 "Can't use -%c with %s\n",
2211 opt2char(OPT_VIANAMEIN),
2212 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002213 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002214
2215 if (target && ip6tc_is_chain(jumpto, *handle)) {
2216 printf("Warning: using chain %s, not extension\n",
2217 jumpto);
2218
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002219 if (target->t)
2220 free(target->t);
2221
Rusty Russell5eed48a2000-06-02 20:12:24 +00002222 target = NULL;
2223 }
2224
2225 /* If they didn't specify a target, or it's a chain
2226 name, use standard. */
2227 if (!target
2228 && (strlen(jumpto) == 0
2229 || ip6tc_is_chain(jumpto, *handle))) {
2230 size_t size;
2231
2232 target = find_target(IP6T_STANDARD_TARGET,
2233 LOAD_MUST_SUCCEED);
2234
2235 size = sizeof(struct ip6t_entry_target)
2236 + target->size;
2237 target->t = fw_calloc(1, size);
2238 target->t->u.target_size = size;
2239 strcpy(target->t->u.user.name, jumpto);
2240 target->init(target->t, &fw.nfcache);
2241 }
2242
2243 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002244 /* it is no chain, and we can't load a plugin.
2245 * We cannot know if the plugin is corrupt, non
2246 * existant OR if the user just misspelled a
2247 * chain. */
2248 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002249 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002250 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002251 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002252 }
2253 }
2254
2255 switch (command) {
2256 case CMD_APPEND:
2257 ret = append_entry(chain, e,
2258 nsaddrs, saddrs, ndaddrs, daddrs,
2259 options&OPT_VERBOSE,
2260 handle);
2261 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002262 case CMD_DELETE:
2263 ret = delete_entry(chain, e,
2264 nsaddrs, saddrs, ndaddrs, daddrs,
2265 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002266 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002267 break;
2268 case CMD_DELETE_NUM:
2269 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2270 break;
2271 case CMD_REPLACE:
2272 ret = replace_entry(chain, e, rulenum - 1,
2273 saddrs, daddrs, options&OPT_VERBOSE,
2274 handle);
2275 break;
2276 case CMD_INSERT:
2277 ret = insert_entry(chain, e, rulenum - 1,
2278 nsaddrs, saddrs, ndaddrs, daddrs,
2279 options&OPT_VERBOSE,
2280 handle);
2281 break;
2282 case CMD_LIST:
2283 ret = list_entries(chain,
2284 options&OPT_VERBOSE,
2285 options&OPT_NUMERIC,
2286 options&OPT_EXPANDED,
2287 options&OPT_LINENUMBERS,
2288 handle);
2289 break;
2290 case CMD_FLUSH:
2291 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2292 break;
2293 case CMD_ZERO:
2294 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2295 break;
2296 case CMD_LIST|CMD_ZERO:
2297 ret = list_entries(chain,
2298 options&OPT_VERBOSE,
2299 options&OPT_NUMERIC,
2300 options&OPT_EXPANDED,
2301 options&OPT_LINENUMBERS,
2302 handle);
2303 if (ret)
2304 ret = zero_entries(chain,
2305 options&OPT_VERBOSE, handle);
2306 break;
2307 case CMD_NEW_CHAIN:
2308 ret = ip6tc_create_chain(chain, handle);
2309 break;
2310 case CMD_DELETE_CHAIN:
2311 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2312 break;
2313 case CMD_RENAME_CHAIN:
2314 ret = ip6tc_rename_chain(chain, newname, handle);
2315 break;
2316 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002317 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002318 break;
2319 default:
2320 /* We should never reach this... */
2321 exit_tryhelp(2);
2322 }
2323
2324 if (verbose > 1)
2325 dump_entries6(*handle);
2326
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002327 clear_rule_matches(&matches);
2328
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002329 if (e != NULL) {
2330 free(e);
2331 e = NULL;
2332 }
2333
2334 for (c = 0; c < nsaddrs; c++)
2335 free(&saddrs[c]);
2336
2337 for (c = 0; c < ndaddrs; c++)
2338 free(&daddrs[c]);
2339
2340 if (opts != original_opts) {
2341 free(opts);
2342 opts = original_opts;
2343 global_option_offset = 0;
2344 }
2345
Rusty Russell5eed48a2000-06-02 20:12:24 +00002346 return ret;
2347}