blob: aac953167cba17f4c0a3f02fd3ce1d38086d1eaa [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
53#ifndef IP6T_LIB_DIR
54#define IP6T_LIB_DIR "/usr/local/lib/iptables"
55#endif
56
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000057#ifndef PROC_SYS_MODPROBE
58#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
59#endif
60
Rusty Russell5eed48a2000-06-02 20:12:24 +000061#define FMT_NUMERIC 0x0001
62#define FMT_NOCOUNTS 0x0002
63#define FMT_KILOMEGAGIGA 0x0004
64#define FMT_OPTIONS 0x0008
65#define FMT_NOTABLE 0x0010
66#define FMT_NOTARGET 0x0020
67#define FMT_VIA 0x0040
68#define FMT_NONEWLINE 0x0080
69#define FMT_LINENUMBERS 0x0100
70
71#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
72 | FMT_NUMERIC | FMT_NOTABLE)
73#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
74
75
76#define CMD_NONE 0x0000U
77#define CMD_INSERT 0x0001U
78#define CMD_DELETE 0x0002U
79#define CMD_DELETE_NUM 0x0004U
80#define CMD_REPLACE 0x0008U
81#define CMD_APPEND 0x0010U
82#define CMD_LIST 0x0020U
83#define CMD_FLUSH 0x0040U
84#define CMD_ZERO 0x0080U
85#define CMD_NEW_CHAIN 0x0100U
86#define CMD_DELETE_CHAIN 0x0200U
87#define CMD_SET_POLICY 0x0400U
88#define CMD_CHECK 0x0800U
89#define CMD_RENAME_CHAIN 0x1000U
90#define NUMBER_OF_CMD 13
91static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000092 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94#define OPTION_OFFSET 256
95
96#define OPT_NONE 0x00000U
97#define OPT_NUMERIC 0x00001U
98#define OPT_SOURCE 0x00002U
99#define OPT_DESTINATION 0x00004U
100#define OPT_PROTOCOL 0x00008U
101#define OPT_JUMP 0x00010U
102#define OPT_VERBOSE 0x00020U
103#define OPT_EXPANDED 0x00040U
104#define OPT_VIANAMEIN 0x00080U
105#define OPT_VIANAMEOUT 0x00100U
106#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000107#define OPT_COUNTERS 0x00400U
108#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000109static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000110= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000111
112static struct option original_opts[] = {
113 { "append", 1, 0, 'A' },
114 { "delete", 1, 0, 'D' },
115 { "insert", 1, 0, 'I' },
116 { "replace", 1, 0, 'R' },
117 { "list", 2, 0, 'L' },
118 { "flush", 2, 0, 'F' },
119 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000120 { "new-chain", 1, 0, 'N' },
121 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000122 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000123 { "policy", 1, 0, 'P' },
124 { "source", 1, 0, 's' },
125 { "destination", 1, 0, 'd' },
126 { "src", 1, 0, 's' }, /* synonym */
127 { "dst", 1, 0, 'd' }, /* synonym */
128 { "protocol", 1, 0, 'p' },
129 { "in-interface", 1, 0, 'i' },
130 { "jump", 1, 0, 'j' },
131 { "table", 1, 0, 't' },
132 { "match", 1, 0, 'm' },
133 { "numeric", 0, 0, 'n' },
134 { "out-interface", 1, 0, 'o' },
135 { "verbose", 0, 0, 'v' },
136 { "exact", 0, 0, 'x' },
137 { "version", 0, 0, 'V' },
138 { "help", 2, 0, 'h' },
139 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000140 { "modprobe", 1, 0, 'M' },
141 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000142 { 0 }
143};
144
Harald Weltea8658ca2003-03-05 07:46:15 +0000145/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
146 * current line of the input file, in order to give a more precise error
147 * message. ip6tables itself doesn't need this, so it is initialized to the
148 * magic number of -1 */
149int line = -1;
150
Rusty Russell5eed48a2000-06-02 20:12:24 +0000151static struct option *opts = original_opts;
152static unsigned int global_option_offset = 0;
153
154/* Table of legal combinations of commands and options. If any of the
155 * given commands make an option legal, that option is legal (applies to
156 * CMD_LIST and CMD_ZERO only).
157 * Key:
158 * + compulsory
159 * x illegal
160 * optional
161 */
162
163static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
164/* Well, it's better than "Re: Linux vs FreeBSD" */
165{
166 /* -n -s -d -p -j -v -x -i -o --line */
167/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
168/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
169/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
170/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
171/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
172/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
173/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
174/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
175/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
176/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
177/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000178/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000179/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
180};
181
182static int inverse_for_options[NUMBER_OF_OPT] =
183{
184/* -n */ 0,
185/* -s */ IP6T_INV_SRCIP,
186/* -d */ IP6T_INV_DSTIP,
187/* -p */ IP6T_INV_PROTO,
188/* -j */ 0,
189/* -v */ 0,
190/* -x */ 0,
191/* -i */ IP6T_INV_VIA_IN,
192/* -o */ IP6T_INV_VIA_OUT,
193/*--line*/ 0
194};
195
196const char *program_version;
197const char *program_name;
198
Rusty Russell208d42e2004-12-20 05:29:52 +0000199static char *lib_dir;
200
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201/* Keeping track of external matches and targets: linked lists. */
202struct ip6tables_match *ip6tables_matches = NULL;
203struct ip6tables_target *ip6tables_targets = NULL;
204
205/* Extra debugging from libiptc */
206extern void dump_entries6(const ip6tc_handle_t handle);
207
208/* A few hardcoded protocols for 'all' and in case the user has no
209 /etc/protocols */
210struct pprot {
211 char *name;
212 u_int8_t num;
213};
214
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000215/* Primitive headers... */
216/* defined in netinet/in.h */
217#if 0
218#ifndef IPPROTO_ESP
219#define IPPROTO_ESP 50
220#endif
221#ifndef IPPROTO_AH
222#define IPPROTO_AH 51
223#endif
224#endif
225
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226static const struct pprot chain_protos[] = {
227 { "tcp", IPPROTO_TCP },
228 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000229 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000230 { "esp", IPPROTO_ESP },
231 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232 { "all", 0 },
233};
234
235static char *
236proto_to_name(u_int8_t proto, int nolookup)
237{
238 unsigned int i;
239
240 if (proto && !nolookup) {
241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
253static void
254in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
255{
256 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000257 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000258}
259
260void
261exit_error(enum exittype status, char *msg, ...)
262{
263 va_list args;
264
265 va_start(args, msg);
266 fprintf(stderr, "%s v%s: ", program_name, program_version);
267 vfprintf(stderr, msg, args);
268 va_end(args);
269 fprintf(stderr, "\n");
270 if (status == PARAMETER_PROBLEM)
271 exit_tryhelp(status);
272 if (status == VERSION_PROBLEM)
273 fprintf(stderr,
274 "Perhaps iptables or your kernel needs to be upgraded.\n");
275 exit(status);
276}
277
278void
279exit_tryhelp(int status)
280{
Harald Weltea8658ca2003-03-05 07:46:15 +0000281 if (line != -1)
282 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
284 program_name, program_name );
285 exit(status);
286}
287
288void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000289exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000290{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000291 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000292 struct ip6tables_target *t = NULL;
293
294 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000295"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000296" %s -[RI] chain rulenum rule-specification [options]\n"
297" %s -D chain rulenum [options]\n"
298" %s -[LFZ] [chain] [options]\n"
299" %s -[NX] chain\n"
300" %s -E old-chain-name new-chain-name\n"
301" %s -P chain target [options]\n"
302" %s -h (print this help information)\n\n",
303 program_name, program_version, program_name, program_name,
304 program_name, program_name, program_name, program_name,
305 program_name, program_name);
306
307 printf(
308"Commands:\n"
309"Either long or short options are allowed.\n"
310" --append -A chain Append to chain\n"
311" --delete -D chain Delete matching rule from chain\n"
312" --delete -D chain rulenum\n"
313" Delete rule rulenum (1 = first) from chain\n"
314" --insert -I chain [rulenum]\n"
315" Insert in chain as rulenum (default 1=first)\n"
316" --replace -R chain rulenum\n"
317" Replace rule rulenum (1 = first) in chain\n"
318" --list -L [chain] List the rules in a chain or all chains\n"
319" --flush -F [chain] Delete all rules in chain or all chains\n"
320" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000321" --new -N chain Create a new user-defined chain\n"
322" --delete-chain\n"
323" -X [chain] Delete a user-defined chain\n"
324" --policy -P chain target\n"
325" Change policy on chain to target\n"
326" --rename-chain\n"
327" -E old-chain new-chain\n"
328" Change chain name, (moving any references)\n"
329
330"Options:\n"
331" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
332" --source -s [!] address[/mask]\n"
333" source specification\n"
334" --destination -d [!] address[/mask]\n"
335" destination specification\n"
336" --in-interface -i [!] input name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000339" target for rule (may load target extension)\n"
340" --match -m match\n"
341" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000342" --numeric -n numeric output of addresses and ports\n"
343" --out-interface -o [!] output name[+]\n"
344" network interface name ([+] for wildcard)\n"
345" --table -t table table to manipulate (default: `filter')\n"
346" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000347" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000349/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000350" --modprobe=<command> try to insert modules using this command\n"
351" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000352"[!] --version -V print package version.\n");
353
354 /* Print out any special helps. A user might like to be able to add a --help
355 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 specified matches & targets */
357 for (t = ip6tables_targets; t; t = t->next) {
358 if (t->used) {
359 printf("\n");
360 t->help();
361 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000362 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000363 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000364 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000365 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000366 }
367 exit(0);
368}
369
370static void
371generic_opt_check(int command, int options)
372{
373 int i, j, legal = 0;
374
375 /* Check that commands are valid with options. Complicated by the
376 * fact that if an option is legal with *any* command given, it is
377 * legal overall (ie. -z and -l).
378 */
379 for (i = 0; i < NUMBER_OF_OPT; i++) {
380 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
381
382 for (j = 0; j < NUMBER_OF_CMD; j++) {
383 if (!(command & (1<<j)))
384 continue;
385
386 if (!(options & (1<<i))) {
387 if (commands_v_options[j][i] == '+')
388 exit_error(PARAMETER_PROBLEM,
389 "You need to supply the `-%c' "
390 "option for this command\n",
391 optflags[i]);
392 } else {
393 if (commands_v_options[j][i] != 'x')
394 legal = 1;
395 else if (legal == 0)
396 legal = -1;
397 }
398 }
399 if (legal == -1)
400 exit_error(PARAMETER_PROBLEM,
401 "Illegal option `-%c' with this command\n",
402 optflags[i]);
403 }
404}
405
406static char
407opt2char(int option)
408{
409 const char *ptr;
410 for (ptr = optflags; option > 1; option >>= 1, ptr++);
411
412 return *ptr;
413}
414
415static char
416cmd2char(int option)
417{
418 const char *ptr;
419 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
420
421 return *ptr;
422}
423
424static void
425add_command(int *cmd, const int newcmd, const int othercmds, int invert)
426{
427 if (invert)
428 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
429 if (*cmd & (~othercmds))
430 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
431 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
432 *cmd |= newcmd;
433}
434
435int
Harald Welteb77f1da2002-03-14 11:35:58 +0000436check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437{
438 if (option && strcmp(option, "!") == 0) {
439 if (*invert)
440 exit_error(PARAMETER_PROBLEM,
441 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000442 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000443 if (optind) {
444 *optind = *optind+1;
445 if (argc && *optind > argc)
446 exit_error(PARAMETER_PROBLEM,
447 "no argument following `!'");
448 }
449
Rusty Russell5eed48a2000-06-02 20:12:24 +0000450 return TRUE;
451 }
452 return FALSE;
453}
454
455static void *
456fw_calloc(size_t count, size_t size)
457{
458 void *p;
459
460 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000461 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462 exit(1);
463 }
464 return p;
465}
466
467static void *
468fw_malloc(size_t size)
469{
470 void *p;
471
472 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000473 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474 exit(1);
475 }
476 return p;
477}
478
Rusty Russell5eed48a2000-06-02 20:12:24 +0000479static char *
480addr_to_numeric(const struct in6_addr *addrp)
481{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000482 /* 0000:0000:0000:0000:0000:000.000.000.000
483 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000484 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000485 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486}
487
488static struct in6_addr *
489numeric_to_addr(const char *num)
490{
491 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000492 int err;
493 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000494 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000495#ifdef DEBUG
496 fprintf(stderr, "\nnumeric2addr: %d\n", err);
497#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498 return (struct in6_addr *)NULL;
499}
500
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000501
502static struct in6_addr *
503host_to_addr(const char *name, unsigned int *naddr)
504{
505 struct addrinfo hints;
506 struct addrinfo *res;
507 static struct in6_addr *addr;
508 int err;
509
510 memset(&hints, 0, sizeof(hints));
511 hints.ai_flags=AI_CANONNAME;
512 hints.ai_family=AF_INET6;
513 hints.ai_socktype=SOCK_RAW;
514 hints.ai_protocol=41;
515 hints.ai_next=NULL;
516
517 *naddr = 0;
518 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
519#ifdef DEBUG
520 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
521#endif
522 return (struct in6_addr *) NULL;
523 } else {
524 if (res->ai_family != AF_INET6 ||
525 res->ai_addrlen != sizeof(struct sockaddr_in6))
526 return (struct in6_addr *) NULL;
527
528#ifdef DEBUG
529 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
530 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
531#endif
532 /* Get the first element of the address-chain */
533 addr = fw_calloc(1, sizeof(struct in6_addr));
534 in6addrcpy(addr, (struct in6_addr *)
535 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
536 freeaddrinfo(res);
537 *naddr = 1;
538 return addr;
539 }
540
541 return (struct in6_addr *) NULL;
542}
543
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000544static char *
545addr_to_host(const struct in6_addr *addr)
546{
547 struct sockaddr_in6 saddr;
548 int err;
549 static char hostname[NI_MAXHOST];
550
551 memset(&saddr, 0, sizeof(struct sockaddr_in6));
552 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000553 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000554
555 if ( (err=getnameinfo((struct sockaddr *)&saddr,
556 sizeof(struct sockaddr_in6),
557 hostname, sizeof(hostname)-1,
558 NULL, 0, 0)) != 0 ){
559#ifdef DEBUG
560 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
561#endif
562 return (char *) NULL;
563 } else {
564#ifdef DEBUG
565 fprintf (stderr, "\naddr2host: %s\n", hostname);
566#endif
567
568 return hostname;
569 }
570
571 return (char *) NULL;
572}
573
Rusty Russell5eed48a2000-06-02 20:12:24 +0000574static char *
575mask_to_numeric(const struct in6_addr *addrp)
576{
Harald Welte8a50ab82003-06-24 18:15:59 +0000577 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000579 if (l == -1) {
580 strcpy(buf, "/");
581 strcat(buf, addr_to_numeric(addrp));
582 return buf;
583 }
Harald Welte43ac1912002-03-03 09:43:07 +0000584 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000585 return buf;
586}
587
588static struct in6_addr *
589network_to_addr(const char *name)
590{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000591 /* abort();*/
592 /* TODO: not implemented yet, but the exception breaks the
593 * name resolvation */
594 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000595}
596
597static char *
598addr_to_anyname(const struct in6_addr *addr)
599{
600 char *name;
601
602 if ((name = addr_to_host(addr)) != NULL)
603 return name;
604
605 return addr_to_numeric(addr);
606}
607
608/*
609 * All functions starting with "parse" should succeed, otherwise
610 * the program fails.
611 * Most routines return pointers to static data that may change
612 * between calls to the same or other routines with a few exceptions:
613 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
614 * return global static data.
615*/
616
617static struct in6_addr *
618parse_hostnetwork(const char *name, unsigned int *naddrs)
619{
620 struct in6_addr *addrp, *addrptmp;
621
622 if ((addrptmp = numeric_to_addr(name)) != NULL ||
623 (addrptmp = network_to_addr(name)) != NULL) {
624 addrp = fw_malloc(sizeof(struct in6_addr));
625 in6addrcpy(addrp, addrptmp);
626 *naddrs = 1;
627 return addrp;
628 }
629 if ((addrp = host_to_addr(name, naddrs)) != NULL)
630 return addrp;
631
632 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
633}
634
635static struct in6_addr *
636parse_mask(char *mask)
637{
638 static struct in6_addr maskaddr;
639 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000640 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000641
642 if (mask == NULL) {
643 /* no mask at all defaults to 128 bits */
644 memset(&maskaddr, 0xff, sizeof maskaddr);
645 return &maskaddr;
646 }
647 if ((addrp = numeric_to_addr(mask)) != NULL)
648 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000649 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000650 exit_error(PARAMETER_PROBLEM,
651 "invalid mask `%s' specified", mask);
652 if (bits != 0) {
653 char *p = (char *)&maskaddr;
654 memset(p, 0xff, bits / 8);
655 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
656 p[bits / 8] = 0xff << (8 - (bits & 7));
657 return &maskaddr;
658 }
659
660 memset(&maskaddr, 0, sizeof maskaddr);
661 return &maskaddr;
662}
663
Harald Welte43ac1912002-03-03 09:43:07 +0000664void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000665parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
666 struct in6_addr *maskp, unsigned int *naddrs)
667{
668 struct in6_addr *addrp;
669 char buf[256];
670 char *p;
671 int i, j, n;
672
673 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000674 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000675 if ((p = strrchr(buf, '/')) != NULL) {
676 *p = '\0';
677 addrp = parse_mask(p + 1);
678 } else
679 addrp = parse_mask(NULL);
680 in6addrcpy(maskp, addrp);
681
682 /* if a null mask is given, the name is ignored, like in "any/0" */
683 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
684 strcpy(buf, "::");
685
686 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
687 n = *naddrs;
688 for (i = 0, j = 0; i < n; i++) {
689 int k;
690 for (k = 0; k < 4; k++)
691 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
692 j++;
693 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000694 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000695 (*naddrs)--;
696 j--;
697 break;
698 }
699 }
700 }
701}
702
703struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000704find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000705{
706 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000707 int icmphack = 0;
708
709 /* This is ugly as hell. Nonetheless, there is no way of changing
710 * this without hurting backwards compatibility */
711 if ( (strcmp(name,"icmpv6") == 0) ||
712 (strcmp(name,"ipv6-icmp") == 0) ||
713 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
714
715 if (!icmphack) {
716 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
717 if (strcmp(name, ptr->name) == 0)
718 break;
719 }
720 } else {
721 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
722 if (strcmp("icmp6", ptr->name) == 0)
723 break;
724 }
725 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000726
Harald Welte3efb6ea2001-08-06 18:50:21 +0000727#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000729 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000731 if (!icmphack)
Rusty Russell208d42e2004-12-20 05:29:52 +0000732 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Harald Weltecfaed1f2001-10-04 08:11:44 +0000733 else
Rusty Russell208d42e2004-12-20 05:29:52 +0000734 sprintf(path, "%s/libip6t_%s.so", lib_dir, "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735 if (dlopen(path, RTLD_NOW)) {
736 /* Found library. If it didn't register itself,
737 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000738 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739
740 if (!ptr)
741 exit_error(PARAMETER_PROBLEM,
742 "Couldn't load match `%s'\n",
743 name);
744 } else if (tryload == LOAD_MUST_SUCCEED)
745 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000746 "Couldn't load match `%s':%s\n",
747 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000749#else
750 if (ptr && !ptr->loaded) {
751 if (tryload != DONT_LOAD)
752 ptr->loaded = 1;
753 else
754 ptr = NULL;
755 }
Marc Boucher067477b2002-03-24 15:09:31 +0000756 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
757 exit_error(PARAMETER_PROBLEM,
758 "Couldn't find match `%s'\n", name);
759 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000760#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000762 if (ptr && matches) {
763 struct ip6tables_rule_match **i;
764 struct ip6tables_rule_match *newentry;
765
766 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
767
768 for (i = matches; *i; i = &(*i)->next);
769 newentry->match = ptr;
770 newentry->next = NULL;
771 *i = newentry;
772 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000773
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774 return ptr;
775}
776
777/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
778static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000779find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000780{
Harald Welteed498492001-07-23 01:24:22 +0000781 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782
Harald Welte43ac1912002-03-03 09:43:07 +0000783 if (string_to_number(pname, 0, 255, &proto) != -1) {
784 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000785
Harald Welte43ac1912002-03-03 09:43:07 +0000786 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000787 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000788 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000789 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000790
791 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792}
793
Harald Welte43ac1912002-03-03 09:43:07 +0000794u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795parse_protocol(const char *s)
796{
Harald Welteed498492001-07-23 01:24:22 +0000797 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798
Harald Welteed498492001-07-23 01:24:22 +0000799 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 struct protoent *pent;
801
802 if ((pent = getprotobyname(s)))
803 proto = pent->p_proto;
804 else {
805 unsigned int i;
806 for (i = 0;
807 i < sizeof(chain_protos)/sizeof(struct pprot);
808 i++) {
809 if (strcmp(s, chain_protos[i].name) == 0) {
810 proto = chain_protos[i].num;
811 break;
812 }
813 }
814 if (i == sizeof(chain_protos)/sizeof(struct pprot))
815 exit_error(PARAMETER_PROBLEM,
816 "unknown protocol `%s' specified",
817 s);
818 }
819 }
820
821 return (u_int16_t)proto;
822}
823
824static void
825parse_interface(const char *arg, char *vianame, unsigned char *mask)
826{
827 int vialen = strlen(arg);
828 unsigned int i;
829
830 memset(mask, 0, IFNAMSIZ);
831 memset(vianame, 0, IFNAMSIZ);
832
833 if (vialen + 1 > IFNAMSIZ)
834 exit_error(PARAMETER_PROBLEM,
835 "interface name `%s' must be shorter than IFNAMSIZ"
836 " (%i)", arg, IFNAMSIZ-1);
837
838 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000839 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840 memset(mask, 0, IFNAMSIZ);
841 else if (vianame[vialen - 1] == '+') {
842 memset(mask, 0xFF, vialen - 1);
843 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000844 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000845 } else {
846 /* Include nul-terminator in match */
847 memset(mask, 0xFF, vialen + 1);
848 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000849 for (i = 0; vianame[i]; i++) {
850 if (!isalnum(vianame[i])
851 && vianame[i] != '_'
852 && vianame[i] != '.') {
853 printf("Warning: wierd character in interface"
854 " `%s' (No aliases, :, ! or *).\n",
855 vianame);
856 break;
857 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000858 }
859 }
860}
861
862/* Can't be zero. */
863static int
864parse_rulenumber(const char *rule)
865{
Harald Welte43ac1912002-03-03 09:43:07 +0000866 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000867
Harald Welte43ac1912002-03-03 09:43:07 +0000868 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000869 exit_error(PARAMETER_PROBLEM,
870 "Invalid rule number `%s'", rule);
871
872 return rulenum;
873}
874
875static const char *
876parse_target(const char *targetname)
877{
878 const char *ptr;
879
880 if (strlen(targetname) < 1)
881 exit_error(PARAMETER_PROBLEM,
882 "Invalid target name (too short)");
883
884 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
885 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000886 "Invalid target name `%s' (%u chars max)",
887 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000888
889 for (ptr = targetname; *ptr; ptr++)
890 if (isspace(*ptr))
891 exit_error(PARAMETER_PROBLEM,
892 "Invalid target name `%s'", targetname);
893 return targetname;
894}
895
896int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000897string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
898 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000899{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000900 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000901 char *end;
902
903 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000904 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000905 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906 if (*end == '\0' && end != s) {
907 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000909 *ret = number;
910 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000911 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000912 }
913 return -1;
914}
915
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916int
917string_to_number_l(const char *s, unsigned long min, unsigned long max,
918 unsigned long *ret)
919{
920 int result;
921 unsigned long long number;
922
923 result = string_to_number_ll(s, min, max, &number);
924 *ret = (unsigned long)number;
925
926 return result;
927}
928
929int string_to_number(const char *s, unsigned int min, unsigned int max,
930 unsigned int *ret)
931{
932 int result;
933 unsigned long number;
934
935 result = string_to_number_l(s, min, max, &number);
936 *ret = (unsigned int)number;
937
938 return result;
939}
940
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941static void
942set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
943 int invert)
944{
945 if (*options & option)
946 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
947 opt2char(option));
948 *options |= option;
949
950 if (invert) {
951 unsigned int i;
952 for (i = 0; 1 << i != option; i++);
953
954 if (!inverse_for_options[i])
955 exit_error(PARAMETER_PROBLEM,
956 "cannot have ! before -%c",
957 opt2char(option));
958 *invflg |= inverse_for_options[i];
959 }
960}
961
962struct ip6tables_target *
963find_target(const char *name, enum ip6t_tryload tryload)
964{
965 struct ip6tables_target *ptr;
966
967 /* Standard target? */
968 if (strcmp(name, "") == 0
969 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
970 || strcmp(name, IP6TC_LABEL_DROP) == 0
971 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
972 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
973 name = "standard";
974
975 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
976 if (strcmp(name, ptr->name) == 0)
977 break;
978 }
979
Harald Welte3efb6ea2001-08-06 18:50:21 +0000980#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000981 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000982 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000984 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000985 if (dlopen(path, RTLD_NOW)) {
986 /* Found library. If it didn't register itself,
987 maybe they specified match as a target. */
988 ptr = find_target(name, DONT_LOAD);
989 if (!ptr)
990 exit_error(PARAMETER_PROBLEM,
991 "Couldn't load target `%s'\n",
992 name);
993 } else if (tryload == LOAD_MUST_SUCCEED)
994 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000995 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000996 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000997 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000998#else
999 if (ptr && !ptr->loaded) {
1000 if (tryload != DONT_LOAD)
1001 ptr->loaded = 1;
1002 else
1003 ptr = NULL;
1004 }
Marc Boucher067477b2002-03-24 15:09:31 +00001005 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1006 exit_error(PARAMETER_PROBLEM,
1007 "Couldn't find target `%s'\n", name);
1008 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001009#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001010
Harald Welte43ac1912002-03-03 09:43:07 +00001011 if (ptr)
1012 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001013
Rusty Russell5eed48a2000-06-02 20:12:24 +00001014 return ptr;
1015}
1016
1017static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001018merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001019 unsigned int *option_offset)
1020{
1021 unsigned int num_old, num_new, i;
1022 struct option *merge;
1023
1024 for (num_old = 0; oldopts[num_old].name; num_old++);
1025 for (num_new = 0; newopts[num_new].name; num_new++);
1026
1027 global_option_offset += OPTION_OFFSET;
1028 *option_offset = global_option_offset;
1029
1030 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1031 memcpy(merge, oldopts, num_old * sizeof(struct option));
1032 for (i = 0; i < num_new; i++) {
1033 merge[num_old + i] = newopts[i];
1034 merge[num_old + i].val += *option_offset;
1035 }
1036 memset(merge + num_old + num_new, 0, sizeof(struct option));
1037
1038 return merge;
1039}
1040
1041void
1042register_match6(struct ip6tables_match *me)
1043{
Harald Welte43ac1912002-03-03 09:43:07 +00001044 struct ip6tables_match **i;
1045
Rusty Russell5eed48a2000-06-02 20:12:24 +00001046 if (strcmp(me->version, program_version) != 0) {
1047 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1048 program_name, me->name, me->version, program_version);
1049 exit(1);
1050 }
1051
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001052 if (find_match(me->name, DONT_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001053 fprintf(stderr, "%s: match `%s' already registered.\n",
1054 program_name, me->name);
1055 exit(1);
1056 }
1057
Harald Welte43ac1912002-03-03 09:43:07 +00001058 if (me->size != IP6T_ALIGN(me->size)) {
1059 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001060 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001061 exit(1);
1062 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001063
Harald Welte43ac1912002-03-03 09:43:07 +00001064 /* Append to list. */
1065 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1066 me->next = NULL;
1067 *i = me;
1068
Rusty Russell5eed48a2000-06-02 20:12:24 +00001069 me->m = NULL;
1070 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001071}
1072
1073void
1074register_target6(struct ip6tables_target *me)
1075{
1076 if (strcmp(me->version, program_version) != 0) {
1077 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1078 program_name, me->name, me->version, program_version);
1079 exit(1);
1080 }
1081
1082 if (find_target(me->name, DONT_LOAD)) {
1083 fprintf(stderr, "%s: target `%s' already registered.\n",
1084 program_name, me->name);
1085 exit(1);
1086 }
1087
Harald Welte43ac1912002-03-03 09:43:07 +00001088 if (me->size != IP6T_ALIGN(me->size)) {
1089 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001090 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001091 exit(1);
1092 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001093
Rusty Russell5eed48a2000-06-02 20:12:24 +00001094 /* Prepend to list. */
1095 me->next = ip6tables_targets;
1096 ip6tables_targets = me;
1097 me->t = NULL;
1098 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001099}
1100
1101static void
1102print_num(u_int64_t number, unsigned int format)
1103{
Harald Welte43ac1912002-03-03 09:43:07 +00001104 if (format & FMT_KILOMEGAGIGA) {
1105 if (number > 99999) {
1106 number = (number + 500) / 1000;
1107 if (number > 9999) {
1108 number = (number + 500) / 1000;
1109 if (number > 9999) {
1110 number = (number + 500) / 1000;
1111 if (number > 9999) {
1112 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001113 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001114 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001115 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001116 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001117 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001118 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001119 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001120 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001121 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001122 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001123 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001124}
1125
Harald Welte43ac1912002-03-03 09:43:07 +00001126
Rusty Russell5eed48a2000-06-02 20:12:24 +00001127static void
1128print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1129{
1130 struct ip6t_counters counters;
1131 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1132 printf("Chain %s", chain);
1133 if (pol) {
1134 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001135 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001136 fputc(' ', stdout);
1137 print_num(counters.pcnt, (format|FMT_NOTABLE));
1138 fputs("packets, ", stdout);
1139 print_num(counters.bcnt, (format|FMT_NOTABLE));
1140 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001141 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001142 printf(")\n");
1143 } else {
1144 unsigned int refs;
1145 if (!ip6tc_get_references(&refs, chain, handle))
1146 printf(" (ERROR obtaining refs)\n");
1147 else
1148 printf(" (%u references)\n", refs);
1149 }
1150
1151 if (format & FMT_LINENUMBERS)
1152 printf(FMT("%-4s ", "%s "), "num");
1153 if (!(format & FMT_NOCOUNTS)) {
1154 if (format & FMT_KILOMEGAGIGA) {
1155 printf(FMT("%5s ","%s "), "pkts");
1156 printf(FMT("%5s ","%s "), "bytes");
1157 } else {
1158 printf(FMT("%8s ","%s "), "pkts");
1159 printf(FMT("%10s ","%s "), "bytes");
1160 }
1161 }
1162 if (!(format & FMT_NOTARGET))
1163 printf(FMT("%-9s ","%s "), "target");
1164 fputs(" prot ", stdout);
1165 if (format & FMT_OPTIONS)
1166 fputs("opt", stdout);
1167 if (format & FMT_VIA) {
1168 printf(FMT(" %-6s ","%s "), "in");
1169 printf(FMT("%-6s ","%s "), "out");
1170 }
1171 printf(FMT(" %-19s ","%s "), "source");
1172 printf(FMT(" %-19s "," %s "), "destination");
1173 printf("\n");
1174}
1175
Harald Welte43ac1912002-03-03 09:43:07 +00001176
Rusty Russell5eed48a2000-06-02 20:12:24 +00001177static int
1178print_match(const struct ip6t_entry_match *m,
1179 const struct ip6t_ip6 *ip,
1180 int numeric)
1181{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001182 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001183
1184 if (match) {
1185 if (match->print)
1186 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001187 else
1188 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189 } else {
1190 if (m->u.user.name[0])
1191 printf("UNKNOWN match `%s' ", m->u.user.name);
1192 }
1193 /* Don't stop iterating. */
1194 return 0;
1195}
1196
1197/* e is called `fw' here for hysterical raisins */
1198static void
1199print_firewall(const struct ip6t_entry *fw,
1200 const char *targname,
1201 unsigned int num,
1202 unsigned int format,
1203 const ip6tc_handle_t handle)
1204{
1205 struct ip6tables_target *target = NULL;
1206 const struct ip6t_entry_target *t;
1207 u_int8_t flags;
1208 char buf[BUFSIZ];
1209
Rusty Russell5eed48a2000-06-02 20:12:24 +00001210 if (!ip6tc_is_chain(targname, handle))
1211 target = find_target(targname, TRY_LOAD);
1212 else
1213 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1214
1215 t = ip6t_get_target((struct ip6t_entry *)fw);
1216 flags = fw->ipv6.flags;
1217
1218 if (format & FMT_LINENUMBERS)
1219 printf(FMT("%-4u ", "%u "), num+1);
1220
1221 if (!(format & FMT_NOCOUNTS)) {
1222 print_num(fw->counters.pcnt, format);
1223 print_num(fw->counters.bcnt, format);
1224 }
1225
1226 if (!(format & FMT_NOTARGET))
1227 printf(FMT("%-9s ", "%s "), targname);
1228
1229 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1230 {
Harald Welte43ac1912002-03-03 09:43:07 +00001231 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001232 if (pname)
1233 printf(FMT("%-5s", "%s "), pname);
1234 else
1235 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1236 }
1237
1238 if (format & FMT_OPTIONS) {
1239 if (format & FMT_NOTABLE)
1240 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001241 fputc(' ', stdout); /* Invert flag of FRAG */
1242 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001243 fputc(' ', stdout);
1244 }
1245
1246 if (format & FMT_VIA) {
1247 char iface[IFNAMSIZ+2];
1248
1249 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1250 iface[0] = '!';
1251 iface[1] = '\0';
1252 }
1253 else iface[0] = '\0';
1254
1255 if (fw->ipv6.iniface[0] != '\0') {
1256 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001257 }
1258 else if (format & FMT_NUMERIC) strcat(iface, "*");
1259 else strcat(iface, "any");
1260 printf(FMT(" %-6s ","in %s "), iface);
1261
1262 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1263 iface[0] = '!';
1264 iface[1] = '\0';
1265 }
1266 else iface[0] = '\0';
1267
1268 if (fw->ipv6.outiface[0] != '\0') {
1269 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001270 }
1271 else if (format & FMT_NUMERIC) strcat(iface, "*");
1272 else strcat(iface, "any");
1273 printf(FMT("%-6s ","out %s "), iface);
1274 }
1275
1276 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1277 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1278 && !(format & FMT_NUMERIC))
1279 printf(FMT("%-19s ","%s "), "anywhere");
1280 else {
1281 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001282 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001283 else
Harald Welte43ac1912002-03-03 09:43:07 +00001284 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001285 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1286 printf(FMT("%-19s ","%s "), buf);
1287 }
1288
1289 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1290 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1291 && !(format & FMT_NUMERIC))
1292 printf(FMT("%-19s","-> %s"), "anywhere");
1293 else {
1294 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001295 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001296 else
Harald Welte43ac1912002-03-03 09:43:07 +00001297 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001298 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1299 printf(FMT("%-19s","-> %s"), buf);
1300 }
1301
1302 if (format & FMT_NOTABLE)
1303 fputs(" ", stdout);
1304
1305 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1306
1307 if (target) {
1308 if (target->print)
1309 /* Print the target information. */
1310 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1311 } else if (t->u.target_size != sizeof(*t))
1312 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001313 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001314
1315 if (!(format & FMT_NONEWLINE))
1316 fputc('\n', stdout);
1317}
1318
1319static void
1320print_firewall_line(const struct ip6t_entry *fw,
1321 const ip6tc_handle_t h)
1322{
1323 struct ip6t_entry_target *t;
1324
1325 t = ip6t_get_target((struct ip6t_entry *)fw);
1326 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1327}
1328
1329static int
1330append_entry(const ip6t_chainlabel chain,
1331 struct ip6t_entry *fw,
1332 unsigned int nsaddrs,
1333 const struct in6_addr saddrs[],
1334 unsigned int ndaddrs,
1335 const struct in6_addr daddrs[],
1336 int verbose,
1337 ip6tc_handle_t *handle)
1338{
1339 unsigned int i, j;
1340 int ret = 1;
1341
1342 for (i = 0; i < nsaddrs; i++) {
1343 fw->ipv6.src = saddrs[i];
1344 for (j = 0; j < ndaddrs; j++) {
1345 fw->ipv6.dst = daddrs[j];
1346 if (verbose)
1347 print_firewall_line(fw, *handle);
1348 ret &= ip6tc_append_entry(chain, fw, handle);
1349 }
1350 }
1351
1352 return ret;
1353}
1354
1355static int
1356replace_entry(const ip6t_chainlabel chain,
1357 struct ip6t_entry *fw,
1358 unsigned int rulenum,
1359 const struct in6_addr *saddr,
1360 const struct in6_addr *daddr,
1361 int verbose,
1362 ip6tc_handle_t *handle)
1363{
1364 fw->ipv6.src = *saddr;
1365 fw->ipv6.dst = *daddr;
1366
1367 if (verbose)
1368 print_firewall_line(fw, *handle);
1369 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1370}
1371
1372static int
1373insert_entry(const ip6t_chainlabel chain,
1374 struct ip6t_entry *fw,
1375 unsigned int rulenum,
1376 unsigned int nsaddrs,
1377 const struct in6_addr saddrs[],
1378 unsigned int ndaddrs,
1379 const struct in6_addr daddrs[],
1380 int verbose,
1381 ip6tc_handle_t *handle)
1382{
1383 unsigned int i, j;
1384 int ret = 1;
1385
1386 for (i = 0; i < nsaddrs; i++) {
1387 fw->ipv6.src = saddrs[i];
1388 for (j = 0; j < ndaddrs; j++) {
1389 fw->ipv6.dst = daddrs[j];
1390 if (verbose)
1391 print_firewall_line(fw, *handle);
1392 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1393 }
1394 }
1395
1396 return ret;
1397}
1398
1399static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001400make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001401{
1402 /* Establish mask for comparison */
1403 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001404 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405 unsigned char *mask, *mptr;
1406
1407 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001408 for (matchp = matches; matchp; matchp = matchp->next)
1409 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410
1411 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001412 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 + ip6tables_targets->size);
1414
1415 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1416 mptr = mask + sizeof(struct ip6t_entry);
1417
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001418 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001420 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001421 + matchp->match->userspacesize);
1422 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423 }
1424
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001425 memset(mptr, 0xFF,
1426 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1427 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001428
1429 return mask;
1430}
1431
1432static int
1433delete_entry(const ip6t_chainlabel chain,
1434 struct ip6t_entry *fw,
1435 unsigned int nsaddrs,
1436 const struct in6_addr saddrs[],
1437 unsigned int ndaddrs,
1438 const struct in6_addr daddrs[],
1439 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001440 ip6tc_handle_t *handle,
1441 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001442{
1443 unsigned int i, j;
1444 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001445 unsigned char *mask;
1446
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001447 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001448 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001449 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001450 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001451 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001452 if (verbose)
1453 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001454 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001455 }
1456 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001457 free(mask);
1458
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459 return ret;
1460}
1461
András Kis-Szabó764316a2001-02-26 17:31:20 +00001462int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001463for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1464 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1465{
1466 int ret = 1;
1467 const char *chain;
1468 char *chains;
1469 unsigned int i, chaincount = 0;
1470
1471 chain = ip6tc_first_chain(handle);
1472 while (chain) {
1473 chaincount++;
1474 chain = ip6tc_next_chain(handle);
1475 }
1476
1477 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1478 i = 0;
1479 chain = ip6tc_first_chain(handle);
1480 while (chain) {
1481 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1482 i++;
1483 chain = ip6tc_next_chain(handle);
1484 }
1485
1486 for (i = 0; i < chaincount; i++) {
1487 if (!builtinstoo
1488 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001489 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001490 continue;
1491 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1492 }
1493
1494 free(chains);
1495 return ret;
1496}
1497
András Kis-Szabó764316a2001-02-26 17:31:20 +00001498int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001499flush_entries(const ip6t_chainlabel chain, int verbose,
1500 ip6tc_handle_t *handle)
1501{
1502 if (!chain)
1503 return for_each_chain(flush_entries, verbose, 1, handle);
1504
1505 if (verbose)
1506 fprintf(stdout, "Flushing chain `%s'\n", chain);
1507 return ip6tc_flush_entries(chain, handle);
1508}
1509
1510static int
1511zero_entries(const ip6t_chainlabel chain, int verbose,
1512 ip6tc_handle_t *handle)
1513{
1514 if (!chain)
1515 return for_each_chain(zero_entries, verbose, 1, handle);
1516
1517 if (verbose)
1518 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1519 return ip6tc_zero_entries(chain, handle);
1520}
1521
András Kis-Szabó764316a2001-02-26 17:31:20 +00001522int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001523delete_chain(const ip6t_chainlabel chain, int verbose,
1524 ip6tc_handle_t *handle)
1525{
1526 if (!chain)
1527 return for_each_chain(delete_chain, verbose, 0, handle);
1528
1529 if (verbose)
1530 fprintf(stdout, "Deleting chain `%s'\n", chain);
1531 return ip6tc_delete_chain(chain, handle);
1532}
1533
1534static int
1535list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1536 int expanded, int linenumbers, ip6tc_handle_t *handle)
1537{
1538 int found = 0;
1539 unsigned int format;
1540 const char *this;
1541
1542 format = FMT_OPTIONS;
1543 if (!verbose)
1544 format |= FMT_NOCOUNTS;
1545 else
1546 format |= FMT_VIA;
1547
1548 if (numeric)
1549 format |= FMT_NUMERIC;
1550
1551 if (!expanded)
1552 format |= FMT_KILOMEGAGIGA;
1553
1554 if (linenumbers)
1555 format |= FMT_LINENUMBERS;
1556
1557 for (this = ip6tc_first_chain(handle);
1558 this;
1559 this = ip6tc_next_chain(handle)) {
1560 const struct ip6t_entry *i;
1561 unsigned int num;
1562
1563 if (chain && strcmp(chain, this) != 0)
1564 continue;
1565
1566 if (found) printf("\n");
1567
1568 print_header(format, this, handle);
1569 i = ip6tc_first_rule(this, handle);
1570
1571 num = 0;
1572 while (i) {
1573 print_firewall(i,
1574 ip6tc_get_target(i, handle),
1575 num++,
1576 format,
1577 *handle);
1578 i = ip6tc_next_rule(i, handle);
1579 }
1580 found = 1;
1581 }
1582
1583 errno = ENOENT;
1584 return found;
1585}
1586
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001587static char *get_modprobe(void)
1588{
Harald Welte43ac1912002-03-03 09:43:07 +00001589 int procfile;
1590 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001591
Harald Welte10f7f142004-10-22 08:14:07 +00001592#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001593 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1594 if (procfile < 0)
1595 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001596
Harald Welte10f7f142004-10-22 08:14:07 +00001597 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001598 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001599 memset(ret, 0, PROCFILE_BUFSIZ);
1600 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001601 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001602 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001603 }
1604 if (ret[strlen(ret)-1]=='\n')
1605 ret[strlen(ret)-1]=0;
1606 close(procfile);
1607 return ret;
1608 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001609 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001610 free(ret);
1611 close(procfile);
1612 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001613}
1614
Harald Welte58918652001-06-16 18:25:25 +00001615int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001616{
Harald Welte43ac1912002-03-03 09:43:07 +00001617 char *buf = NULL;
1618 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001619 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001620
Harald Welte43ac1912002-03-03 09:43:07 +00001621 /* If they don't explicitly set it, read out of kernel */
1622 if (!modprobe) {
1623 buf = get_modprobe();
1624 if (!buf)
1625 return -1;
1626 modprobe = buf;
1627 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001628
Harald Welte43ac1912002-03-03 09:43:07 +00001629 switch (fork()) {
1630 case 0:
1631 argv[0] = (char *)modprobe;
1632 argv[1] = (char *)modname;
1633 argv[2] = NULL;
1634 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001635
Harald Welte43ac1912002-03-03 09:43:07 +00001636 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001637 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001638 case -1:
1639 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001640
Harald Welte43ac1912002-03-03 09:43:07 +00001641 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001642 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001643 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001644
Harald Welte43ac1912002-03-03 09:43:07 +00001645 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001646 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1647 return 0;
1648 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001649}
1650
Rusty Russell5eed48a2000-06-02 20:12:24 +00001651static struct ip6t_entry *
1652generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001653 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001654 struct ip6t_entry_target *target)
1655{
1656 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001657 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 struct ip6t_entry *e;
1659
1660 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001661 for (matchp = matches; matchp; matchp = matchp->next)
1662 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001663
1664 e = fw_malloc(size + target->u.target_size);
1665 *e = *fw;
1666 e->target_offset = size;
1667 e->next_offset = size + target->u.target_size;
1668
1669 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001670 for (matchp = matches; matchp; matchp = matchp->next) {
1671 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1672 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673 }
1674 memcpy(e->elems + size, target, target->u.target_size);
1675
1676 return e;
1677}
1678
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001679void clear_rule_matches(struct ip6tables_rule_match **matches)
1680{
1681 struct ip6tables_rule_match *matchp, *tmp;
1682
1683 for (matchp = *matches; matchp;) {
1684 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001685 if (matchp->match->m)
1686 free(matchp->match->m);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001687 free(matchp);
1688 matchp = tmp;
1689 }
1690
1691 *matches = NULL;
1692}
1693
Rusty Russell5eed48a2000-06-02 20:12:24 +00001694int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1695{
1696 struct ip6t_entry fw, *e = NULL;
1697 int invert = 0;
1698 unsigned int nsaddrs = 0, ndaddrs = 0;
1699 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1700
1701 int c, verbose = 0;
1702 const char *chain = NULL;
1703 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1704 const char *policy = NULL, *newname = NULL;
1705 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001706 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707 int ret = 1;
1708 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001709 struct ip6tables_rule_match *matches = NULL;
1710 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001711 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001712 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001713 const char *jumpto = "";
1714 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001715 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001716 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001717 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001718
1719 memset(&fw, 0, sizeof(fw));
1720
Rusty Russell208d42e2004-12-20 05:29:52 +00001721 lib_dir = getenv("IP6TABLES_LIB_DIR");
1722 if (!lib_dir)
1723 lib_dir = IP6T_LIB_DIR;
1724
Harald Welte43ac1912002-03-03 09:43:07 +00001725 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001726 * a second time */
1727 optind = 0;
1728
Harald Welte43ac1912002-03-03 09:43:07 +00001729 /* clear mflags in case do_command gets called a second time
1730 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001731 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001732 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001733
Harald Welte43ac1912002-03-03 09:43:07 +00001734 for (t = ip6tables_targets; t; t = t->next) {
1735 t->tflags = 0;
1736 t->used = 0;
1737 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001738
Rusty Russell5eed48a2000-06-02 20:12:24 +00001739 /* Suppress error messages: we may add new options if we
1740 demand-load a protocol. */
1741 opterr = 0;
1742
1743 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001744 "-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 +00001745 opts, NULL)) != -1) {
1746 switch (c) {
1747 /*
1748 * Command selection
1749 */
1750 case 'A':
1751 add_command(&command, CMD_APPEND, CMD_NONE,
1752 invert);
1753 chain = optarg;
1754 break;
1755
1756 case 'D':
1757 add_command(&command, CMD_DELETE, CMD_NONE,
1758 invert);
1759 chain = optarg;
1760 if (optind < argc && argv[optind][0] != '-'
1761 && argv[optind][0] != '!') {
1762 rulenum = parse_rulenumber(argv[optind++]);
1763 command = CMD_DELETE_NUM;
1764 }
1765 break;
1766
Rusty Russell5eed48a2000-06-02 20:12:24 +00001767 case 'R':
1768 add_command(&command, CMD_REPLACE, CMD_NONE,
1769 invert);
1770 chain = optarg;
1771 if (optind < argc && argv[optind][0] != '-'
1772 && argv[optind][0] != '!')
1773 rulenum = parse_rulenumber(argv[optind++]);
1774 else
1775 exit_error(PARAMETER_PROBLEM,
1776 "-%c requires a rule number",
1777 cmd2char(CMD_REPLACE));
1778 break;
1779
1780 case 'I':
1781 add_command(&command, CMD_INSERT, CMD_NONE,
1782 invert);
1783 chain = optarg;
1784 if (optind < argc && argv[optind][0] != '-'
1785 && argv[optind][0] != '!')
1786 rulenum = parse_rulenumber(argv[optind++]);
1787 else rulenum = 1;
1788 break;
1789
1790 case 'L':
1791 add_command(&command, CMD_LIST, CMD_ZERO,
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 'F':
1800 add_command(&command, CMD_FLUSH, CMD_NONE,
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 'Z':
1809 add_command(&command, CMD_ZERO, CMD_LIST,
1810 invert);
1811 if (optarg) chain = optarg;
1812 else if (optind < argc && argv[optind][0] != '-'
1813 && argv[optind][0] != '!')
1814 chain = argv[optind++];
1815 break;
1816
1817 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001818 if (optarg && *optarg == '-')
1819 exit_error(PARAMETER_PROBLEM,
1820 "chain name not allowed to start "
1821 "with `-'\n");
1822 if (find_target(optarg, TRY_LOAD))
1823 exit_error(PARAMETER_PROBLEM,
1824 "chain name may not clash "
1825 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001826 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1827 invert);
1828 chain = optarg;
1829 break;
1830
1831 case 'X':
1832 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1833 invert);
1834 if (optarg) chain = optarg;
1835 else if (optind < argc && argv[optind][0] != '-'
1836 && argv[optind][0] != '!')
1837 chain = argv[optind++];
1838 break;
1839
1840 case 'E':
1841 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1842 invert);
1843 chain = optarg;
1844 if (optind < argc && argv[optind][0] != '-'
1845 && argv[optind][0] != '!')
1846 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001847 else
1848 exit_error(PARAMETER_PROBLEM,
1849 "-%c requires old-chain-name and "
1850 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001851 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001852 break;
1853
1854 case 'P':
1855 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1856 invert);
1857 chain = optarg;
1858 if (optind < argc && argv[optind][0] != '-'
1859 && argv[optind][0] != '!')
1860 policy = argv[optind++];
1861 else
1862 exit_error(PARAMETER_PROBLEM,
1863 "-%c requires a chain and a policy",
1864 cmd2char(CMD_SET_POLICY));
1865 break;
1866
1867 case 'h':
1868 if (!optarg)
1869 optarg = argv[optind];
1870
1871 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001872 if (!matches && protocol)
1873 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001875 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001876
1877 /*
1878 * Option selection
1879 */
1880 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001881 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1883 invert);
1884
1885 /* Canonicalize into lower case */
1886 for (protocol = argv[optind-1]; *protocol; protocol++)
1887 *protocol = tolower(*protocol);
1888
1889 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001890 if ( strcmp(protocol,"ipv6-icmp") == 0)
1891 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001892 fw.ipv6.proto = parse_protocol(protocol);
1893 fw.ipv6.flags |= IP6T_F_PROTO;
1894
1895 if (fw.ipv6.proto == 0
1896 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1897 exit_error(PARAMETER_PROBLEM,
1898 "rule would never match protocol");
1899 fw.nfcache |= NFC_IP6_PROTO;
1900 break;
1901
1902 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001903 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001904 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1905 invert);
1906 shostnetworkmask = argv[optind-1];
1907 fw.nfcache |= NFC_IP6_SRC;
1908 break;
1909
1910 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001911 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1913 invert);
1914 dhostnetworkmask = argv[optind-1];
1915 fw.nfcache |= NFC_IP6_DST;
1916 break;
1917
1918 case 'j':
1919 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1920 invert);
1921 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001922 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001923 target = find_target(jumpto, TRY_LOAD);
1924
1925 if (target) {
1926 size_t size;
1927
Harald Welte43ac1912002-03-03 09:43:07 +00001928 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1929 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001930
1931 target->t = fw_calloc(1, size);
1932 target->t->u.target_size = size;
1933 strcpy(target->t->u.user.name, jumpto);
1934 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001935 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001936 }
1937 break;
1938
1939
1940 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001941 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1943 invert);
1944 parse_interface(argv[optind-1],
1945 fw.ipv6.iniface,
1946 fw.ipv6.iniface_mask);
1947 fw.nfcache |= NFC_IP6_IF_IN;
1948 break;
1949
1950 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001951 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1953 invert);
1954 parse_interface(argv[optind-1],
1955 fw.ipv6.outiface,
1956 fw.ipv6.outiface_mask);
1957 fw.nfcache |= NFC_IP6_IF_OUT;
1958 break;
1959
1960 case 'v':
1961 if (!verbose)
1962 set_option(&options, OPT_VERBOSE,
1963 &fw.ipv6.invflags, invert);
1964 verbose++;
1965 break;
1966
1967 case 'm': {
1968 size_t size;
1969
1970 if (invert)
1971 exit_error(PARAMETER_PROBLEM,
1972 "unexpected ! flag before --match");
1973
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001974 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001975 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1976 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001977 m->m = fw_calloc(1, size);
1978 m->m->u.match_size = size;
1979 strcpy(m->m->u.user.name, m->name);
1980 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001981 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 }
1983 break;
1984
1985 case 'n':
1986 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1987 invert);
1988 break;
1989
1990 case 't':
1991 if (invert)
1992 exit_error(PARAMETER_PROBLEM,
1993 "unexpected ! flag before --table");
1994 *table = argv[optind-1];
1995 break;
1996
1997 case 'x':
1998 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1999 invert);
2000 break;
2001
2002 case 'V':
2003 if (invert)
2004 printf("Not %s ;-)\n", program_version);
2005 else
2006 printf("%s v%s\n",
2007 program_name, program_version);
2008 exit(0);
2009
2010 case '0':
2011 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2012 invert);
2013 break;
2014
Harald Welte43ac1912002-03-03 09:43:07 +00002015 case 'M':
2016 modprobe = optarg;
2017 break;
2018
2019 case 'c':
2020
2021 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2022 invert);
2023 pcnt = optarg;
2024 if (optind < argc && argv[optind][0] != '-'
2025 && argv[optind][0] != '!')
2026 bcnt = argv[optind++];
2027 else
2028 exit_error(PARAMETER_PROBLEM,
2029 "-%c requires packet and byte counter",
2030 opt2char(OPT_COUNTERS));
2031
Martin Josefssona28d4952004-05-26 16:04:48 +00002032 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002033 exit_error(PARAMETER_PROBLEM,
2034 "-%c packet counter not numeric",
2035 opt2char(OPT_COUNTERS));
2036
Martin Josefssona28d4952004-05-26 16:04:48 +00002037 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002038 exit_error(PARAMETER_PROBLEM,
2039 "-%c byte counter not numeric",
2040 opt2char(OPT_COUNTERS));
2041
2042 break;
2043
2044
Rusty Russell5eed48a2000-06-02 20:12:24 +00002045 case 1: /* non option */
2046 if (optarg[0] == '!' && optarg[1] == '\0') {
2047 if (invert)
2048 exit_error(PARAMETER_PROBLEM,
2049 "multiple consecutive ! not"
2050 " allowed");
2051 invert = TRUE;
2052 optarg[0] = '\0';
2053 continue;
2054 }
2055 printf("Bad argument `%s'\n", optarg);
2056 exit_tryhelp(2);
2057
2058 default:
2059 /* FIXME: This scheme doesn't allow two of the same
2060 matches --RR */
2061 if (!target
2062 || !(target->parse(c - target->option_offset,
2063 argv, invert,
2064 &target->tflags,
2065 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002066 for (matchp = matches; matchp; matchp = matchp->next) {
2067 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002068 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002069 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002070 &fw,
2071 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002072 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073 break;
2074 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002075 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002076
2077 /* If you listen carefully, you can
2078 actually hear this code suck. */
2079
2080 /* some explanations (after four different bugs
2081 * in 3 different releases): If we encountere a
2082 * parameter, that has not been parsed yet,
2083 * it's not an option of an explicitly loaded
2084 * match or a target. However, we support
2085 * implicit loading of the protocol match
2086 * extension. '-p tcp' means 'l4 proto 6' and
2087 * at the same time 'load tcp protocol match on
2088 * demand if we specify --dport'.
2089 *
2090 * To make this work, we need to make sure:
2091 * - the parameter has not been parsed by
2092 * a match (m above)
2093 * - a protocol has been specified
2094 * - the protocol extension has not been
2095 * loaded yet, or is loaded and unused
2096 * [think of iptables-restore!]
2097 * - the protocol extension can be successively
2098 * loaded
2099 */
2100 if (m == NULL
2101 && protocol
2102 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002103 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002104 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002105 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002106 && (proto_used == 0))
2107 )
2108 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002109 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002110 /* Try loading protocol */
2111 size_t size;
2112
2113 proto_used = 1;
2114
2115 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2116 + m->size;
2117
2118 m->m = fw_calloc(1, size);
2119 m->m->u.match_size = size;
2120 strcpy(m->m->u.user.name, m->name);
2121 m->init(m->m, &fw.nfcache);
2122
2123 opts = merge_options(opts,
2124 m->extra_opts, &m->option_offset);
2125
2126 optind--;
2127 continue;
2128 }
2129
Rusty Russell5eed48a2000-06-02 20:12:24 +00002130 if (!m)
2131 exit_error(PARAMETER_PROBLEM,
2132 "Unknown arg `%s'",
2133 argv[optind-1]);
2134 }
2135 }
2136 invert = FALSE;
2137 }
2138
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002139 for (matchp = matches; matchp; matchp = matchp->next)
2140 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002141
Harald Welte43ac1912002-03-03 09:43:07 +00002142 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002143 target->final_check(target->tflags);
2144
2145 /* Fix me: must put inverse options checking here --MN */
2146
2147 if (optind < argc)
2148 exit_error(PARAMETER_PROBLEM,
2149 "unknown arguments found on commandline");
2150 if (!command)
2151 exit_error(PARAMETER_PROBLEM, "no command specified");
2152 if (invert)
2153 exit_error(PARAMETER_PROBLEM,
2154 "nothing appropriate following !");
2155
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002156 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002157 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002158 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002159 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002160 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002161 }
2162
2163 if (shostnetworkmask)
2164 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2165 &(fw.ipv6.smsk), &nsaddrs);
2166
2167 if (dhostnetworkmask)
2168 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2169 &(fw.ipv6.dmsk), &ndaddrs);
2170
2171 if ((nsaddrs > 1 || ndaddrs > 1) &&
2172 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2173 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2174 " source or destination IP addresses");
2175
Rusty Russell5eed48a2000-06-02 20:12:24 +00002176 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2177 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2178 "specify a unique address");
2179
2180 generic_opt_check(command, options);
2181
2182 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2183 exit_error(PARAMETER_PROBLEM,
2184 "chain name `%s' too long (must be under %i chars)",
2185 chain, IP6T_FUNCTION_MAXNAMELEN);
2186
Harald Welte43ac1912002-03-03 09:43:07 +00002187 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002188 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002189 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002190
Rusty Russell8beb0492004-12-22 00:37:10 +00002191 /* try to insmod the module if iptc_init failed */
2192 if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002193 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002194
Harald Welte43ac1912002-03-03 09:43:07 +00002195 if (!*handle)
2196 exit_error(VERSION_PROBLEM,
2197 "can't initialize ip6tables table `%s': %s",
2198 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002199
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002200 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002201 || command == CMD_DELETE
2202 || command == CMD_INSERT
2203 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002204 if (strcmp(chain, "PREROUTING") == 0
2205 || strcmp(chain, "INPUT") == 0) {
2206 /* -o not valid with incoming packets. */
2207 if (options & OPT_VIANAMEOUT)
2208 exit_error(PARAMETER_PROBLEM,
2209 "Can't use -%c with %s\n",
2210 opt2char(OPT_VIANAMEOUT),
2211 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002212 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002213
Harald Welte43ac1912002-03-03 09:43:07 +00002214 if (strcmp(chain, "POSTROUTING") == 0
2215 || strcmp(chain, "OUTPUT") == 0) {
2216 /* -i not valid with outgoing packets */
2217 if (options & OPT_VIANAMEIN)
2218 exit_error(PARAMETER_PROBLEM,
2219 "Can't use -%c with %s\n",
2220 opt2char(OPT_VIANAMEIN),
2221 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002222 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002223
2224 if (target && ip6tc_is_chain(jumpto, *handle)) {
2225 printf("Warning: using chain %s, not extension\n",
2226 jumpto);
2227
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002228 if (target->t)
2229 free(target->t);
2230
Rusty Russell5eed48a2000-06-02 20:12:24 +00002231 target = NULL;
2232 }
2233
2234 /* If they didn't specify a target, or it's a chain
2235 name, use standard. */
2236 if (!target
2237 && (strlen(jumpto) == 0
2238 || ip6tc_is_chain(jumpto, *handle))) {
2239 size_t size;
2240
2241 target = find_target(IP6T_STANDARD_TARGET,
2242 LOAD_MUST_SUCCEED);
2243
2244 size = sizeof(struct ip6t_entry_target)
2245 + target->size;
2246 target->t = fw_calloc(1, size);
2247 target->t->u.target_size = size;
2248 strcpy(target->t->u.user.name, jumpto);
2249 target->init(target->t, &fw.nfcache);
2250 }
2251
2252 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002253 /* it is no chain, and we can't load a plugin.
2254 * We cannot know if the plugin is corrupt, non
2255 * existant OR if the user just misspelled a
2256 * chain. */
2257 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002258 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002259 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002260 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002261 }
2262 }
2263
2264 switch (command) {
2265 case CMD_APPEND:
2266 ret = append_entry(chain, e,
2267 nsaddrs, saddrs, ndaddrs, daddrs,
2268 options&OPT_VERBOSE,
2269 handle);
2270 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002271 case CMD_DELETE:
2272 ret = delete_entry(chain, e,
2273 nsaddrs, saddrs, ndaddrs, daddrs,
2274 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002275 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002276 break;
2277 case CMD_DELETE_NUM:
2278 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2279 break;
2280 case CMD_REPLACE:
2281 ret = replace_entry(chain, e, rulenum - 1,
2282 saddrs, daddrs, options&OPT_VERBOSE,
2283 handle);
2284 break;
2285 case CMD_INSERT:
2286 ret = insert_entry(chain, e, rulenum - 1,
2287 nsaddrs, saddrs, ndaddrs, daddrs,
2288 options&OPT_VERBOSE,
2289 handle);
2290 break;
2291 case CMD_LIST:
2292 ret = list_entries(chain,
2293 options&OPT_VERBOSE,
2294 options&OPT_NUMERIC,
2295 options&OPT_EXPANDED,
2296 options&OPT_LINENUMBERS,
2297 handle);
2298 break;
2299 case CMD_FLUSH:
2300 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2301 break;
2302 case CMD_ZERO:
2303 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2304 break;
2305 case CMD_LIST|CMD_ZERO:
2306 ret = list_entries(chain,
2307 options&OPT_VERBOSE,
2308 options&OPT_NUMERIC,
2309 options&OPT_EXPANDED,
2310 options&OPT_LINENUMBERS,
2311 handle);
2312 if (ret)
2313 ret = zero_entries(chain,
2314 options&OPT_VERBOSE, handle);
2315 break;
2316 case CMD_NEW_CHAIN:
2317 ret = ip6tc_create_chain(chain, handle);
2318 break;
2319 case CMD_DELETE_CHAIN:
2320 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2321 break;
2322 case CMD_RENAME_CHAIN:
2323 ret = ip6tc_rename_chain(chain, newname, handle);
2324 break;
2325 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002326 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002327 break;
2328 default:
2329 /* We should never reach this... */
2330 exit_tryhelp(2);
2331 }
2332
2333 if (verbose > 1)
2334 dump_entries6(*handle);
2335
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002336 clear_rule_matches(&matches);
2337
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002338 if (e != NULL) {
2339 free(e);
2340 e = NULL;
2341 }
2342
2343 for (c = 0; c < nsaddrs; c++)
2344 free(&saddrs[c]);
2345
2346 for (c = 0; c < ndaddrs; c++)
2347 free(&daddrs[c]);
2348
2349 if (opts != original_opts) {
2350 free(opts);
2351 opts = original_opts;
2352 global_option_offset = 0;
2353 }
2354
Rusty Russell5eed48a2000-06-02 20:12:24 +00002355 return ret;
2356}