blob: c438d604ddfb05656251447e467c96332e5bad84 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +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 Welted4ab5ad2002-08-07 09:07:24 +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 *
Marc Bouchere6869a82000-03-20 06:03:29 +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>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
41#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
50#ifndef IPT_LIB_DIR
51#define IPT_LIB_DIR "/usr/local/lib/iptables"
52#endif
53
Harald Welte82dd2ec2000-12-19 05:18:15 +000054#ifndef PROC_SYS_MODPROBE
55#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
56#endif
57
Marc Bouchere6869a82000-03-20 06:03:29 +000058#define FMT_NUMERIC 0x0001
59#define FMT_NOCOUNTS 0x0002
60#define FMT_KILOMEGAGIGA 0x0004
61#define FMT_OPTIONS 0x0008
62#define FMT_NOTABLE 0x0010
63#define FMT_NOTARGET 0x0020
64#define FMT_VIA 0x0040
65#define FMT_NONEWLINE 0x0080
66#define FMT_LINENUMBERS 0x0100
67
68#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
69 | FMT_NUMERIC | FMT_NOTABLE)
70#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
71
72
73#define CMD_NONE 0x0000U
74#define CMD_INSERT 0x0001U
75#define CMD_DELETE 0x0002U
76#define CMD_DELETE_NUM 0x0004U
77#define CMD_REPLACE 0x0008U
78#define CMD_APPEND 0x0010U
79#define CMD_LIST 0x0020U
80#define CMD_FLUSH 0x0040U
81#define CMD_ZERO 0x0080U
82#define CMD_NEW_CHAIN 0x0100U
83#define CMD_DELETE_CHAIN 0x0200U
84#define CMD_SET_POLICY 0x0400U
85#define CMD_CHECK 0x0800U
86#define CMD_RENAME_CHAIN 0x1000U
87#define NUMBER_OF_CMD 13
88static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000089 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000090
91#define OPTION_OFFSET 256
92
93#define OPT_NONE 0x00000U
94#define OPT_NUMERIC 0x00001U
95#define OPT_SOURCE 0x00002U
96#define OPT_DESTINATION 0x00004U
97#define OPT_PROTOCOL 0x00008U
98#define OPT_JUMP 0x00010U
99#define OPT_VERBOSE 0x00020U
100#define OPT_EXPANDED 0x00040U
101#define OPT_VIANAMEIN 0x00080U
102#define OPT_VIANAMEOUT 0x00100U
103#define OPT_FRAGMENT 0x00200U
104#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000105#define OPT_COUNTERS 0x00800U
106#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000107static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000108= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000109
110static struct option original_opts[] = {
111 { "append", 1, 0, 'A' },
112 { "delete", 1, 0, 'D' },
113 { "insert", 1, 0, 'I' },
114 { "replace", 1, 0, 'R' },
115 { "list", 2, 0, 'L' },
116 { "flush", 2, 0, 'F' },
117 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "new-chain", 1, 0, 'N' },
119 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000120 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 { "policy", 1, 0, 'P' },
122 { "source", 1, 0, 's' },
123 { "destination", 1, 0, 'd' },
124 { "src", 1, 0, 's' }, /* synonym */
125 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000126 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 { "in-interface", 1, 0, 'i' },
128 { "jump", 1, 0, 'j' },
129 { "table", 1, 0, 't' },
130 { "match", 1, 0, 'm' },
131 { "numeric", 0, 0, 'n' },
132 { "out-interface", 1, 0, 'o' },
133 { "verbose", 0, 0, 'v' },
134 { "exact", 0, 0, 'x' },
135 { "fragments", 0, 0, 'f' },
136 { "version", 0, 0, 'V' },
137 { "help", 2, 0, 'h' },
138 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000139 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000140 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000141 { 0 }
142};
143
Illes Marci63e90632003-03-03 08:08:37 +0000144/* we need this for iptables-restore. iptables-restore.c sets line to the
145 * current line of the input file, in order to give a more precise error
146 * message. iptables itself doesn't need this, so it is initialized to the
147 * magic number of -1 */
148int line = -1;
149
Rusty Russell4e242f82000-05-31 06:33:50 +0000150#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000151struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000152ipt_get_target(struct ipt_entry *e)
153{
154 return (void *)e + e->target_offset;
155}
156#endif
157
Marc Bouchere6869a82000-03-20 06:03:29 +0000158static struct option *opts = original_opts;
159static unsigned int global_option_offset = 0;
160
161/* Table of legal combinations of commands and options. If any of the
162 * given commands make an option legal, that option is legal (applies to
163 * CMD_LIST and CMD_ZERO only).
164 * Key:
165 * + compulsory
166 * x illegal
167 * optional
168 */
169
170static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
171/* Well, it's better than "Re: Linux vs FreeBSD" */
172{
173 /* -n -s -d -p -j -v -x -i -o -f --line */
174/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
175/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
176/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
177/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
178/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
179/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
180/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
181/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
182/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
183/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
184/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000185/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000186/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
187};
188
189static int inverse_for_options[NUMBER_OF_OPT] =
190{
191/* -n */ 0,
192/* -s */ IPT_INV_SRCIP,
193/* -d */ IPT_INV_DSTIP,
194/* -p */ IPT_INV_PROTO,
195/* -j */ 0,
196/* -v */ 0,
197/* -x */ 0,
198/* -i */ IPT_INV_VIA_IN,
199/* -o */ IPT_INV_VIA_OUT,
200/* -f */ IPT_INV_FRAG,
201/*--line*/ 0
202};
203
204const char *program_version;
205const char *program_name;
206
Rusty Russell2e0a3212000-04-19 11:23:18 +0000207/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000208struct iptables_match *iptables_matches = NULL;
209struct iptables_target *iptables_targets = NULL;
210
211/* Extra debugging from libiptc */
212extern void dump_entries(const iptc_handle_t handle);
213
214/* A few hardcoded protocols for 'all' and in case the user has no
215 /etc/protocols */
216struct pprot {
217 char *name;
218 u_int8_t num;
219};
220
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000221/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000222/* defined in netinet/in.h */
223#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000224#ifndef IPPROTO_ESP
225#define IPPROTO_ESP 50
226#endif
227#ifndef IPPROTO_AH
228#define IPPROTO_AH 51
229#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000230#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000231
Marc Bouchere6869a82000-03-20 06:03:29 +0000232static const struct pprot chain_protos[] = {
233 { "tcp", IPPROTO_TCP },
234 { "udp", IPPROTO_UDP },
235 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000236 { "esp", IPPROTO_ESP },
237 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000238 { "all", 0 },
239};
240
241static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000242proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000243{
244 unsigned int i;
245
Rusty Russell28381a42000-05-10 00:19:50 +0000246 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000247 struct protoent *pent = getprotobynumber(proto);
248 if (pent)
249 return pent->p_name;
250 }
251
252 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
253 if (chain_protos[i].num == proto)
254 return chain_protos[i].name;
255
256 return NULL;
257}
258
259struct in_addr *
260dotted_to_addr(const char *dotted)
261{
262 static struct in_addr addr;
263 unsigned char *addrp;
264 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000265 unsigned int onebyte;
266 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000267 char buf[20];
268
269 /* copy dotted string, because we need to modify it */
270 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000271 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000272 addrp = (unsigned char *) &(addr.s_addr);
273
274 p = buf;
275 for (i = 0; i < 3; i++) {
276 if ((q = strchr(p, '.')) == NULL)
277 return (struct in_addr *) NULL;
278
279 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000280 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000281 return (struct in_addr *) NULL;
282
283 addrp[i] = (unsigned char) onebyte;
284 p = q + 1;
285 }
286
287 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000288 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000289 return (struct in_addr *) NULL;
290
291 addrp[3] = (unsigned char) onebyte;
292
293 return &addr;
294}
295
296static struct in_addr *
297network_to_addr(const char *name)
298{
299 struct netent *net;
300 static struct in_addr addr;
301
302 if ((net = getnetbyname(name)) != NULL) {
303 if (net->n_addrtype != AF_INET)
304 return (struct in_addr *) NULL;
305 addr.s_addr = htonl((unsigned long) net->n_net);
306 return &addr;
307 }
308
309 return (struct in_addr *) NULL;
310}
311
312static void
313inaddrcpy(struct in_addr *dst, struct in_addr *src)
314{
315 /* memcpy(dst, src, sizeof(struct in_addr)); */
316 dst->s_addr = src->s_addr;
317}
318
319void
320exit_error(enum exittype status, char *msg, ...)
321{
322 va_list args;
323
324 va_start(args, msg);
325 fprintf(stderr, "%s v%s: ", program_name, program_version);
326 vfprintf(stderr, msg, args);
327 va_end(args);
328 fprintf(stderr, "\n");
329 if (status == PARAMETER_PROBLEM)
330 exit_tryhelp(status);
331 if (status == VERSION_PROBLEM)
332 fprintf(stderr,
333 "Perhaps iptables or your kernel needs to be upgraded.\n");
334 exit(status);
335}
336
337void
338exit_tryhelp(int status)
339{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000340 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000341 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000342 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
343 program_name, program_name );
344 exit(status);
345}
346
347void
348exit_printhelp(void)
349{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000350 struct iptables_match *m = NULL;
351 struct iptables_target *t = NULL;
352
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000354"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000355" %s -[RI] chain rulenum rule-specification [options]\n"
356" %s -D chain rulenum [options]\n"
357" %s -[LFZ] [chain] [options]\n"
358" %s -[NX] chain\n"
359" %s -E old-chain-name new-chain-name\n"
360" %s -P chain target [options]\n"
361" %s -h (print this help information)\n\n",
362 program_name, program_version, program_name, program_name,
363 program_name, program_name, program_name, program_name,
364 program_name, program_name);
365
366 printf(
367"Commands:\n"
368"Either long or short options are allowed.\n"
369" --append -A chain Append to chain\n"
370" --delete -D chain Delete matching rule from chain\n"
371" --delete -D chain rulenum\n"
372" Delete rule rulenum (1 = first) from chain\n"
373" --insert -I chain [rulenum]\n"
374" Insert in chain as rulenum (default 1=first)\n"
375" --replace -R chain rulenum\n"
376" Replace rule rulenum (1 = first) in chain\n"
377" --list -L [chain] List the rules in a chain or all chains\n"
378" --flush -F [chain] Delete all rules in chain or all chains\n"
379" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000380" --new -N chain Create a new user-defined chain\n"
381" --delete-chain\n"
382" -X [chain] Delete a user-defined chain\n"
383" --policy -P chain target\n"
384" Change policy on chain to target\n"
385" --rename-chain\n"
386" -E old-chain new-chain\n"
387" Change chain name, (moving any references)\n"
388
389"Options:\n"
390" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
391" --source -s [!] address[/mask]\n"
392" source specification\n"
393" --destination -d [!] address[/mask]\n"
394" destination specification\n"
395" --in-interface -i [!] input name[+]\n"
396" network interface name ([+] for wildcard)\n"
397" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000398" target for rule (may load target extension)\n"
399" --match -m match\n"
400" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000401" --numeric -n numeric output of addresses and ports\n"
402" --out-interface -o [!] output name[+]\n"
403" network interface name ([+] for wildcard)\n"
404" --table -t table table to manipulate (default: `filter')\n"
405" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000406" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000407" --exact -x expand numbers (display exact values)\n"
408"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000409" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000410" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000411"[!] --version -V print package version.\n");
412
Rusty Russell363112d2000-08-11 13:49:26 +0000413 /* Print out any special helps. A user might like to be able
414 to add a --help to the commandline, and see expected
415 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000416 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000417 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000418 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000419 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000420 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000421 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000422 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000423 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000424 exit(0);
425}
426
427static void
428generic_opt_check(int command, int options)
429{
430 int i, j, legal = 0;
431
432 /* Check that commands are valid with options. Complicated by the
433 * fact that if an option is legal with *any* command given, it is
434 * legal overall (ie. -z and -l).
435 */
436 for (i = 0; i < NUMBER_OF_OPT; i++) {
437 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
438
439 for (j = 0; j < NUMBER_OF_CMD; j++) {
440 if (!(command & (1<<j)))
441 continue;
442
443 if (!(options & (1<<i))) {
444 if (commands_v_options[j][i] == '+')
445 exit_error(PARAMETER_PROBLEM,
446 "You need to supply the `-%c' "
447 "option for this command\n",
448 optflags[i]);
449 } else {
450 if (commands_v_options[j][i] != 'x')
451 legal = 1;
452 else if (legal == 0)
453 legal = -1;
454 }
455 }
456 if (legal == -1)
457 exit_error(PARAMETER_PROBLEM,
458 "Illegal option `-%c' with this command\n",
459 optflags[i]);
460 }
461}
462
463static char
464opt2char(int option)
465{
466 const char *ptr;
467 for (ptr = optflags; option > 1; option >>= 1, ptr++);
468
469 return *ptr;
470}
471
472static char
473cmd2char(int option)
474{
475 const char *ptr;
476 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
477
478 return *ptr;
479}
480
481static void
482add_command(int *cmd, const int newcmd, const int othercmds, int invert)
483{
484 if (invert)
485 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
486 if (*cmd & (~othercmds))
487 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
488 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
489 *cmd |= newcmd;
490}
491
492int
Harald Welteb77f1da2002-03-14 11:35:58 +0000493check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000494{
495 if (option && strcmp(option, "!") == 0) {
496 if (*invert)
497 exit_error(PARAMETER_PROBLEM,
498 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000499 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000500 if (optind) {
501 *optind = *optind+1;
502 if (argc && *optind > argc)
503 exit_error(PARAMETER_PROBLEM,
504 "no argument following `!'");
505 }
506
Marc Bouchere6869a82000-03-20 06:03:29 +0000507 return TRUE;
508 }
509 return FALSE;
510}
511
512static void *
513fw_calloc(size_t count, size_t size)
514{
515 void *p;
516
517 if ((p = calloc(count, size)) == NULL) {
518 perror("iptables: calloc failed");
519 exit(1);
520 }
521 return p;
522}
523
524static void *
525fw_malloc(size_t size)
526{
527 void *p;
528
529 if ((p = malloc(size)) == NULL) {
530 perror("iptables: malloc failed");
531 exit(1);
532 }
533 return p;
534}
535
536static struct in_addr *
537host_to_addr(const char *name, unsigned int *naddr)
538{
539 struct hostent *host;
540 struct in_addr *addr;
541 unsigned int i;
542
543 *naddr = 0;
544 if ((host = gethostbyname(name)) != NULL) {
545 if (host->h_addrtype != AF_INET ||
546 host->h_length != sizeof(struct in_addr))
547 return (struct in_addr *) NULL;
548
549 while (host->h_addr_list[*naddr] != (char *) NULL)
550 (*naddr)++;
551 addr = fw_calloc(*naddr, sizeof(struct in_addr));
552 for (i = 0; i < *naddr; i++)
553 inaddrcpy(&(addr[i]),
554 (struct in_addr *) host->h_addr_list[i]);
555 return addr;
556 }
557
558 return (struct in_addr *) NULL;
559}
560
561static char *
562addr_to_host(const struct in_addr *addr)
563{
564 struct hostent *host;
565
566 if ((host = gethostbyaddr((char *) addr,
567 sizeof(struct in_addr), AF_INET)) != NULL)
568 return (char *) host->h_name;
569
570 return (char *) NULL;
571}
572
573/*
574 * All functions starting with "parse" should succeed, otherwise
575 * the program fails.
576 * Most routines return pointers to static data that may change
577 * between calls to the same or other routines with a few exceptions:
578 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
579 * return global static data.
580*/
581
582static struct in_addr *
583parse_hostnetwork(const char *name, unsigned int *naddrs)
584{
585 struct in_addr *addrp, *addrptmp;
586
587 if ((addrptmp = dotted_to_addr(name)) != NULL ||
588 (addrptmp = network_to_addr(name)) != NULL) {
589 addrp = fw_malloc(sizeof(struct in_addr));
590 inaddrcpy(addrp, addrptmp);
591 *naddrs = 1;
592 return addrp;
593 }
594 if ((addrp = host_to_addr(name, naddrs)) != NULL)
595 return addrp;
596
597 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
598}
599
600static struct in_addr *
601parse_mask(char *mask)
602{
603 static struct in_addr maskaddr;
604 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000605 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000606
607 if (mask == NULL) {
608 /* no mask at all defaults to 32 bits */
609 maskaddr.s_addr = 0xFFFFFFFF;
610 return &maskaddr;
611 }
612 if ((addrp = dotted_to_addr(mask)) != NULL)
613 /* dotted_to_addr already returns a network byte order addr */
614 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000615 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000616 exit_error(PARAMETER_PROBLEM,
617 "invalid mask `%s' specified", mask);
618 if (bits != 0) {
619 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
620 return &maskaddr;
621 }
622
623 maskaddr.s_addr = 0L;
624 return &maskaddr;
625}
626
Marc Boucherb93c7982001-12-06 14:50:19 +0000627void
Marc Bouchere6869a82000-03-20 06:03:29 +0000628parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
629 struct in_addr *maskp, unsigned int *naddrs)
630{
631 struct in_addr *addrp;
632 char buf[256];
633 char *p;
634 int i, j, k, n;
635
636 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000637 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000638 if ((p = strrchr(buf, '/')) != NULL) {
639 *p = '\0';
640 addrp = parse_mask(p + 1);
641 } else
642 addrp = parse_mask(NULL);
643 inaddrcpy(maskp, addrp);
644
645 /* if a null mask is given, the name is ignored, like in "any/0" */
646 if (maskp->s_addr == 0L)
647 strcpy(buf, "0.0.0.0");
648
649 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
650 n = *naddrs;
651 for (i = 0, j = 0; i < n; i++) {
652 addrp[j++].s_addr &= maskp->s_addr;
653 for (k = 0; k < j - 1; k++) {
654 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
655 (*naddrs)--;
656 j--;
657 break;
658 }
659 }
660 }
661}
662
663struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000664find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000665{
666 struct iptables_match *ptr;
667
668 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
669 if (strcmp(name, ptr->name) == 0)
670 break;
671 }
672
Harald Welte3efb6ea2001-08-06 18:50:21 +0000673#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000674 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000675 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
676 + strlen(name)];
677 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000678 if (dlopen(path, RTLD_NOW)) {
679 /* Found library. If it didn't register itself,
680 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000681 ptr = find_match(name, DONT_LOAD);
682
Rusty Russell9e1d2142000-04-23 09:11:12 +0000683 if (!ptr)
684 exit_error(PARAMETER_PROBLEM,
685 "Couldn't load match `%s'\n",
686 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000687 } else if (tryload == LOAD_MUST_SUCCEED)
688 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000689 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000690 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000691 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000692#else
693 if (ptr && !ptr->loaded) {
694 if (tryload != DONT_LOAD)
695 ptr->loaded = 1;
696 else
697 ptr = NULL;
698 }
Marc Boucher067477b2002-03-24 15:09:31 +0000699 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
700 exit_error(PARAMETER_PROBLEM,
701 "Couldn't find match `%s'\n", name);
702 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000703#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000704
Harald Welteae1ff9f2000-12-01 14:26:20 +0000705 if (ptr)
706 ptr->used = 1;
707
Marc Bouchere6869a82000-03-20 06:03:29 +0000708 return ptr;
709}
710
Rusty Russell28381a42000-05-10 00:19:50 +0000711/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
712static struct iptables_match *
713find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
714{
Harald Welteed498492001-07-23 01:24:22 +0000715 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000716
Harald Welte0b0013a2002-02-18 16:15:31 +0000717 if (string_to_number(pname, 0, 255, &proto) != -1) {
718 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000719
Harald Welte0b0013a2002-02-18 16:15:31 +0000720 if (protoname)
721 return find_match(protoname, tryload);
722 } else
723 return find_match(pname, tryload);
724
725 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000726}
727
Marc Boucherb93c7982001-12-06 14:50:19 +0000728u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000729parse_protocol(const char *s)
730{
Harald Welteed498492001-07-23 01:24:22 +0000731 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000732
Harald Welteed498492001-07-23 01:24:22 +0000733 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000734 struct protoent *pent;
735
736 if ((pent = getprotobyname(s)))
737 proto = pent->p_proto;
738 else {
739 unsigned int i;
740 for (i = 0;
741 i < sizeof(chain_protos)/sizeof(struct pprot);
742 i++) {
743 if (strcmp(s, chain_protos[i].name) == 0) {
744 proto = chain_protos[i].num;
745 break;
746 }
747 }
748 if (i == sizeof(chain_protos)/sizeof(struct pprot))
749 exit_error(PARAMETER_PROBLEM,
750 "unknown protocol `%s' specified",
751 s);
752 }
753 }
754
755 return (u_int16_t)proto;
756}
757
758static void
759parse_interface(const char *arg, char *vianame, unsigned char *mask)
760{
761 int vialen = strlen(arg);
762 unsigned int i;
763
764 memset(mask, 0, IFNAMSIZ);
765 memset(vianame, 0, IFNAMSIZ);
766
767 if (vialen + 1 > IFNAMSIZ)
768 exit_error(PARAMETER_PROBLEM,
769 "interface name `%s' must be shorter than IFNAMSIZ"
770 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000771
Marc Bouchere6869a82000-03-20 06:03:29 +0000772 strcpy(vianame, arg);
773 if (vialen == 0)
774 memset(mask, 0, IFNAMSIZ);
775 else if (vianame[vialen - 1] == '+') {
776 memset(mask, 0xFF, vialen - 1);
777 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000778 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000779 } else {
780 /* Include nul-terminator in match */
781 memset(mask, 0xFF, vialen + 1);
782 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000783 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000784 if (!isalnum(vianame[i])
785 && vianame[i] != '_'
786 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000787 printf("Warning: wierd character in interface"
788 " `%s' (No aliases, :, ! or *).\n",
789 vianame);
790 break;
791 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000792 }
793 }
794}
795
796/* Can't be zero. */
797static int
798parse_rulenumber(const char *rule)
799{
Harald Welteed498492001-07-23 01:24:22 +0000800 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000801
Harald Welteed498492001-07-23 01:24:22 +0000802 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000803 exit_error(PARAMETER_PROBLEM,
804 "Invalid rule number `%s'", rule);
805
806 return rulenum;
807}
808
809static const char *
810parse_target(const char *targetname)
811{
812 const char *ptr;
813
814 if (strlen(targetname) < 1)
815 exit_error(PARAMETER_PROBLEM,
816 "Invalid target name (too short)");
817
818 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
819 exit_error(PARAMETER_PROBLEM,
820 "Invalid target name `%s' (%i chars max)",
821 targetname, sizeof(ipt_chainlabel)-1);
822
823 for (ptr = targetname; *ptr; ptr++)
824 if (isspace(*ptr))
825 exit_error(PARAMETER_PROBLEM,
826 "Invalid target name `%s'", targetname);
827 return targetname;
828}
829
830static char *
831addr_to_network(const struct in_addr *addr)
832{
833 struct netent *net;
834
835 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
836 return (char *) net->n_name;
837
838 return (char *) NULL;
839}
840
841char *
842addr_to_dotted(const struct in_addr *addrp)
843{
844 static char buf[20];
845 const unsigned char *bytep;
846
847 bytep = (const unsigned char *) &(addrp->s_addr);
848 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
849 return buf;
850}
Marc Boucherb93c7982001-12-06 14:50:19 +0000851
852char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000853addr_to_anyname(const struct in_addr *addr)
854{
855 char *name;
856
857 if ((name = addr_to_host(addr)) != NULL ||
858 (name = addr_to_network(addr)) != NULL)
859 return name;
860
861 return addr_to_dotted(addr);
862}
863
Marc Boucherb93c7982001-12-06 14:50:19 +0000864char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000865mask_to_dotted(const struct in_addr *mask)
866{
867 int i;
868 static char buf[20];
869 u_int32_t maskaddr, bits;
870
871 maskaddr = ntohl(mask->s_addr);
872
873 if (maskaddr == 0xFFFFFFFFL)
874 /* we don't want to see "/32" */
875 return "";
876
877 i = 32;
878 bits = 0xFFFFFFFEL;
879 while (--i >= 0 && maskaddr != bits)
880 bits <<= 1;
881 if (i >= 0)
882 sprintf(buf, "/%d", i);
883 else
884 /* mask was not a decent combination of 1's and 0's */
885 sprintf(buf, "/%s", addr_to_dotted(mask));
886
887 return buf;
888}
889
890int
Harald Welteed498492001-07-23 01:24:22 +0000891string_to_number(const char *s, unsigned int min, unsigned int max,
892 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000893{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000894 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000895 char *end;
896
897 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000898 errno = 0;
899 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900 if (*end == '\0' && end != s) {
901 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000902 if (errno != ERANGE && min <= number && number <= max) {
903 *ret = number;
904 return 0;
905 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000906 }
907 return -1;
908}
909
910static void
911set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
912 int invert)
913{
914 if (*options & option)
915 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
916 opt2char(option));
917 *options |= option;
918
919 if (invert) {
920 unsigned int i;
921 for (i = 0; 1 << i != option; i++);
922
923 if (!inverse_for_options[i])
924 exit_error(PARAMETER_PROBLEM,
925 "cannot have ! before -%c",
926 opt2char(option));
927 *invflg |= inverse_for_options[i];
928 }
929}
930
931struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000932find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000933{
934 struct iptables_target *ptr;
935
936 /* Standard target? */
937 if (strcmp(name, "") == 0
938 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
939 || strcmp(name, IPTC_LABEL_DROP) == 0
940 || strcmp(name, IPTC_LABEL_QUEUE) == 0
941 || strcmp(name, IPTC_LABEL_RETURN) == 0)
942 name = "standard";
943
944 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
945 if (strcmp(name, ptr->name) == 0)
946 break;
947 }
948
Harald Welte3efb6ea2001-08-06 18:50:21 +0000949#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000950 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000951 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
952 + strlen(name)];
953 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000954 if (dlopen(path, RTLD_NOW)) {
955 /* Found library. If it didn't register itself,
956 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000957 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000958 if (!ptr)
959 exit_error(PARAMETER_PROBLEM,
960 "Couldn't load target `%s'\n",
961 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000962 } else if (tryload == LOAD_MUST_SUCCEED)
963 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000964 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000965 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000966 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000967#else
968 if (ptr && !ptr->loaded) {
969 if (tryload != DONT_LOAD)
970 ptr->loaded = 1;
971 else
972 ptr = NULL;
973 }
Marc Boucher067477b2002-03-24 15:09:31 +0000974 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
975 exit_error(PARAMETER_PROBLEM,
976 "Couldn't find target `%s'\n", name);
977 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000978#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000979
Harald Welteae1ff9f2000-12-01 14:26:20 +0000980 if (ptr)
981 ptr->used = 1;
982
Marc Bouchere6869a82000-03-20 06:03:29 +0000983 return ptr;
984}
985
986static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000987merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000988 unsigned int *option_offset)
989{
990 unsigned int num_old, num_new, i;
991 struct option *merge;
992
993 for (num_old = 0; oldopts[num_old].name; num_old++);
994 for (num_new = 0; newopts[num_new].name; num_new++);
995
996 global_option_offset += OPTION_OFFSET;
997 *option_offset = global_option_offset;
998
999 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1000 memcpy(merge, oldopts, num_old * sizeof(struct option));
1001 for (i = 0; i < num_new; i++) {
1002 merge[num_old + i] = newopts[i];
1003 merge[num_old + i].val += *option_offset;
1004 }
1005 memset(merge + num_old + num_new, 0, sizeof(struct option));
1006
1007 return merge;
1008}
1009
1010void
1011register_match(struct iptables_match *me)
1012{
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001013 struct iptables_match **i;
1014
Marc Bouchere6869a82000-03-20 06:03:29 +00001015 if (strcmp(me->version, program_version) != 0) {
1016 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1017 program_name, me->name, me->version, program_version);
1018 exit(1);
1019 }
1020
Rusty Russell52a51492000-05-02 16:44:29 +00001021 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001022 fprintf(stderr, "%s: match `%s' already registered.\n",
1023 program_name, me->name);
1024 exit(1);
1025 }
1026
Rusty Russell73f72f52000-07-03 10:17:57 +00001027 if (me->size != IPT_ALIGN(me->size)) {
1028 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1029 program_name, me->name, me->size);
1030 exit(1);
1031 }
1032
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001033 /* Append to list. */
1034 for (i = &iptables_matches; *i; i = &(*i)->next);
1035 me->next = NULL;
1036 *i = me;
1037
Marc Bouchere6869a82000-03-20 06:03:29 +00001038 me->m = NULL;
1039 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001040}
1041
1042void
1043register_target(struct iptables_target *me)
1044{
1045 if (strcmp(me->version, program_version) != 0) {
1046 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1047 program_name, me->name, me->version, program_version);
1048 exit(1);
1049 }
1050
Rusty Russell52a51492000-05-02 16:44:29 +00001051 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001052 fprintf(stderr, "%s: target `%s' already registered.\n",
1053 program_name, me->name);
1054 exit(1);
1055 }
1056
Rusty Russell73f72f52000-07-03 10:17:57 +00001057 if (me->size != IPT_ALIGN(me->size)) {
1058 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1059 program_name, me->name, me->size);
1060 exit(1);
1061 }
1062
Marc Bouchere6869a82000-03-20 06:03:29 +00001063 /* Prepend to list. */
1064 me->next = iptables_targets;
1065 iptables_targets = me;
1066 me->t = NULL;
1067 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001068}
1069
1070static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001071print_num(u_int64_t number, unsigned int format)
1072{
1073 if (format & FMT_KILOMEGAGIGA) {
1074 if (number > 99999) {
1075 number = (number + 500) / 1000;
1076 if (number > 9999) {
1077 number = (number + 500) / 1000;
1078 if (number > 9999) {
1079 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001080 if (number > 9999) {
1081 number = (number + 500) / 1000;
1082 printf(FMT("%4lluT ","%lluT "), number);
1083 }
1084 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001085 }
1086 else printf(FMT("%4lluM ","%lluM "), number);
1087 } else
1088 printf(FMT("%4lluK ","%lluK "), number);
1089 } else
1090 printf(FMT("%5llu ","%llu "), number);
1091 } else
1092 printf(FMT("%8llu ","%llu "), number);
1093}
1094
1095
1096static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001097print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1098{
1099 struct ipt_counters counters;
1100 const char *pol = iptc_get_policy(chain, &counters, handle);
1101 printf("Chain %s", chain);
1102 if (pol) {
1103 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001104 if (!(format & FMT_NOCOUNTS)) {
1105 fputc(' ', stdout);
1106 print_num(counters.pcnt, (format|FMT_NOTABLE));
1107 fputs("packets, ", stdout);
1108 print_num(counters.bcnt, (format|FMT_NOTABLE));
1109 fputs("bytes", stdout);
1110 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001111 printf(")\n");
1112 } else {
1113 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001114 if (!iptc_get_references(&refs, chain, handle))
1115 printf(" (ERROR obtaining refs)\n");
1116 else
1117 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001118 }
1119
1120 if (format & FMT_LINENUMBERS)
1121 printf(FMT("%-4s ", "%s "), "num");
1122 if (!(format & FMT_NOCOUNTS)) {
1123 if (format & FMT_KILOMEGAGIGA) {
1124 printf(FMT("%5s ","%s "), "pkts");
1125 printf(FMT("%5s ","%s "), "bytes");
1126 } else {
1127 printf(FMT("%8s ","%s "), "pkts");
1128 printf(FMT("%10s ","%s "), "bytes");
1129 }
1130 }
1131 if (!(format & FMT_NOTARGET))
1132 printf(FMT("%-9s ","%s "), "target");
1133 fputs(" prot ", stdout);
1134 if (format & FMT_OPTIONS)
1135 fputs("opt", stdout);
1136 if (format & FMT_VIA) {
1137 printf(FMT(" %-6s ","%s "), "in");
1138 printf(FMT("%-6s ","%s "), "out");
1139 }
1140 printf(FMT(" %-19s ","%s "), "source");
1141 printf(FMT(" %-19s "," %s "), "destination");
1142 printf("\n");
1143}
1144
Marc Bouchere6869a82000-03-20 06:03:29 +00001145
1146static int
1147print_match(const struct ipt_entry_match *m,
1148 const struct ipt_ip *ip,
1149 int numeric)
1150{
Rusty Russell52a51492000-05-02 16:44:29 +00001151 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001152
1153 if (match) {
1154 if (match->print)
1155 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001156 else
Rusty Russellb039b022000-09-01 06:04:05 +00001157 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001158 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001159 if (m->u.user.name[0])
1160 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001161 }
1162 /* Don't stop iterating. */
1163 return 0;
1164}
1165
1166/* e is called `fw' here for hysterical raisins */
1167static void
1168print_firewall(const struct ipt_entry *fw,
1169 const char *targname,
1170 unsigned int num,
1171 unsigned int format,
1172 const iptc_handle_t handle)
1173{
1174 struct iptables_target *target = NULL;
1175 const struct ipt_entry_target *t;
1176 u_int8_t flags;
1177 char buf[BUFSIZ];
1178
Marc Bouchere6869a82000-03-20 06:03:29 +00001179 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001180 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001181 else
Rusty Russell52a51492000-05-02 16:44:29 +00001182 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001183
1184 t = ipt_get_target((struct ipt_entry *)fw);
1185 flags = fw->ip.flags;
1186
1187 if (format & FMT_LINENUMBERS)
1188 printf(FMT("%-4u ", "%u "), num+1);
1189
1190 if (!(format & FMT_NOCOUNTS)) {
1191 print_num(fw->counters.pcnt, format);
1192 print_num(fw->counters.bcnt, format);
1193 }
1194
1195 if (!(format & FMT_NOTARGET))
1196 printf(FMT("%-9s ", "%s "), targname);
1197
1198 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1199 {
Rusty Russell28381a42000-05-10 00:19:50 +00001200 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 if (pname)
1202 printf(FMT("%-5s", "%s "), pname);
1203 else
1204 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1205 }
1206
1207 if (format & FMT_OPTIONS) {
1208 if (format & FMT_NOTABLE)
1209 fputs("opt ", stdout);
1210 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1211 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1212 fputc(' ', stdout);
1213 }
1214
1215 if (format & FMT_VIA) {
1216 char iface[IFNAMSIZ+2];
1217
1218 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1219 iface[0] = '!';
1220 iface[1] = '\0';
1221 }
1222 else iface[0] = '\0';
1223
1224 if (fw->ip.iniface[0] != '\0') {
1225 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001226 }
1227 else if (format & FMT_NUMERIC) strcat(iface, "*");
1228 else strcat(iface, "any");
1229 printf(FMT(" %-6s ","in %s "), iface);
1230
1231 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1232 iface[0] = '!';
1233 iface[1] = '\0';
1234 }
1235 else iface[0] = '\0';
1236
1237 if (fw->ip.outiface[0] != '\0') {
1238 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001239 }
1240 else if (format & FMT_NUMERIC) strcat(iface, "*");
1241 else strcat(iface, "any");
1242 printf(FMT("%-6s ","out %s "), iface);
1243 }
1244
1245 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1246 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1247 printf(FMT("%-19s ","%s "), "anywhere");
1248 else {
1249 if (format & FMT_NUMERIC)
1250 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1251 else
1252 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1253 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1254 printf(FMT("%-19s ","%s "), buf);
1255 }
1256
1257 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1258 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001259 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001260 else {
1261 if (format & FMT_NUMERIC)
1262 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1263 else
1264 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1265 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001266 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001267 }
1268
1269 if (format & FMT_NOTABLE)
1270 fputs(" ", stdout);
1271
1272 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1273
1274 if (target) {
1275 if (target->print)
1276 /* Print the target information. */
1277 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001278 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001279 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001280 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001281
1282 if (!(format & FMT_NONEWLINE))
1283 fputc('\n', stdout);
1284}
1285
1286static void
1287print_firewall_line(const struct ipt_entry *fw,
1288 const iptc_handle_t h)
1289{
1290 struct ipt_entry_target *t;
1291
1292 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001293 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001294}
1295
1296static int
1297append_entry(const ipt_chainlabel chain,
1298 struct ipt_entry *fw,
1299 unsigned int nsaddrs,
1300 const struct in_addr saddrs[],
1301 unsigned int ndaddrs,
1302 const struct in_addr daddrs[],
1303 int verbose,
1304 iptc_handle_t *handle)
1305{
1306 unsigned int i, j;
1307 int ret = 1;
1308
1309 for (i = 0; i < nsaddrs; i++) {
1310 fw->ip.src.s_addr = saddrs[i].s_addr;
1311 for (j = 0; j < ndaddrs; j++) {
1312 fw->ip.dst.s_addr = daddrs[j].s_addr;
1313 if (verbose)
1314 print_firewall_line(fw, *handle);
1315 ret &= iptc_append_entry(chain, fw, handle);
1316 }
1317 }
1318
1319 return ret;
1320}
1321
1322static int
1323replace_entry(const ipt_chainlabel chain,
1324 struct ipt_entry *fw,
1325 unsigned int rulenum,
1326 const struct in_addr *saddr,
1327 const struct in_addr *daddr,
1328 int verbose,
1329 iptc_handle_t *handle)
1330{
1331 fw->ip.src.s_addr = saddr->s_addr;
1332 fw->ip.dst.s_addr = daddr->s_addr;
1333
1334 if (verbose)
1335 print_firewall_line(fw, *handle);
1336 return iptc_replace_entry(chain, fw, rulenum, handle);
1337}
1338
1339static int
1340insert_entry(const ipt_chainlabel chain,
1341 struct ipt_entry *fw,
1342 unsigned int rulenum,
1343 unsigned int nsaddrs,
1344 const struct in_addr saddrs[],
1345 unsigned int ndaddrs,
1346 const struct in_addr daddrs[],
1347 int verbose,
1348 iptc_handle_t *handle)
1349{
1350 unsigned int i, j;
1351 int ret = 1;
1352
1353 for (i = 0; i < nsaddrs; i++) {
1354 fw->ip.src.s_addr = saddrs[i].s_addr;
1355 for (j = 0; j < ndaddrs; j++) {
1356 fw->ip.dst.s_addr = daddrs[j].s_addr;
1357 if (verbose)
1358 print_firewall_line(fw, *handle);
1359 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1360 }
1361 }
1362
1363 return ret;
1364}
1365
Rusty Russell2e0a3212000-04-19 11:23:18 +00001366static unsigned char *
1367make_delete_mask(struct ipt_entry *fw)
1368{
1369 /* Establish mask for comparison */
1370 unsigned int size;
1371 struct iptables_match *m;
1372 unsigned char *mask, *mptr;
1373
1374 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001375 for (m = iptables_matches; m; m = m->next) {
1376 if (!m->used)
1377 continue;
1378
Rusty Russell73f72f52000-07-03 10:17:57 +00001379 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001380 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001381
Rusty Russell9e1d2142000-04-23 09:11:12 +00001382 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001383 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001384 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001385
Rusty Russell9e1d2142000-04-23 09:11:12 +00001386 memset(mask, 0xFF, sizeof(struct ipt_entry));
1387 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001388
Sven Kochfb1279a2001-02-27 12:25:12 +00001389 for (m = iptables_matches; m; m = m->next) {
1390 if (!m->used)
1391 continue;
1392
Rusty Russell2e0a3212000-04-19 11:23:18 +00001393 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001394 IPT_ALIGN(sizeof(struct ipt_entry_match))
1395 + m->userspacesize);
1396 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001397 }
1398
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001399 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001400 IPT_ALIGN(sizeof(struct ipt_entry_target))
1401 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001402
1403 return mask;
1404}
1405
Marc Bouchere6869a82000-03-20 06:03:29 +00001406static int
1407delete_entry(const ipt_chainlabel chain,
1408 struct ipt_entry *fw,
1409 unsigned int nsaddrs,
1410 const struct in_addr saddrs[],
1411 unsigned int ndaddrs,
1412 const struct in_addr daddrs[],
1413 int verbose,
1414 iptc_handle_t *handle)
1415{
1416 unsigned int i, j;
1417 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001418 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001419
Rusty Russell2e0a3212000-04-19 11:23:18 +00001420 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001421 for (i = 0; i < nsaddrs; i++) {
1422 fw->ip.src.s_addr = saddrs[i].s_addr;
1423 for (j = 0; j < ndaddrs; j++) {
1424 fw->ip.dst.s_addr = daddrs[j].s_addr;
1425 if (verbose)
1426 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001427 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001428 }
1429 }
1430 return ret;
1431}
1432
Harald Welteae1ff9f2000-12-01 14:26:20 +00001433int
Marc Bouchere6869a82000-03-20 06:03:29 +00001434for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001435 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001436{
1437 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001438 const char *chain;
1439 char *chains;
1440 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001441
Rusty Russell9e1d2142000-04-23 09:11:12 +00001442 chain = iptc_first_chain(handle);
1443 while (chain) {
1444 chaincount++;
1445 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001446 }
1447
Rusty Russell9e1d2142000-04-23 09:11:12 +00001448 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1449 i = 0;
1450 chain = iptc_first_chain(handle);
1451 while (chain) {
1452 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1453 i++;
1454 chain = iptc_next_chain(handle);
1455 }
1456
1457 for (i = 0; i < chaincount; i++) {
1458 if (!builtinstoo
1459 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1460 *handle))
1461 continue;
1462 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1463 }
1464
1465 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001466 return ret;
1467}
1468
Harald Welteae1ff9f2000-12-01 14:26:20 +00001469int
Marc Bouchere6869a82000-03-20 06:03:29 +00001470flush_entries(const ipt_chainlabel chain, int verbose,
1471 iptc_handle_t *handle)
1472{
1473 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001474 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001475
1476 if (verbose)
1477 fprintf(stdout, "Flushing chain `%s'\n", chain);
1478 return iptc_flush_entries(chain, handle);
1479}
Marc Bouchere6869a82000-03-20 06:03:29 +00001480
1481static int
1482zero_entries(const ipt_chainlabel chain, int verbose,
1483 iptc_handle_t *handle)
1484{
1485 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001486 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001487
Marc Bouchere6869a82000-03-20 06:03:29 +00001488 if (verbose)
1489 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1490 return iptc_zero_entries(chain, handle);
1491}
1492
Harald Welteae1ff9f2000-12-01 14:26:20 +00001493int
Marc Bouchere6869a82000-03-20 06:03:29 +00001494delete_chain(const ipt_chainlabel chain, int verbose,
1495 iptc_handle_t *handle)
1496{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001497 if (!chain)
1498 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001499
1500 if (verbose)
1501 fprintf(stdout, "Deleting chain `%s'\n", chain);
1502 return iptc_delete_chain(chain, handle);
1503}
1504
1505static int
1506list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1507 int expanded, int linenumbers, iptc_handle_t *handle)
1508{
1509 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001510 unsigned int format;
1511 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001512
1513 format = FMT_OPTIONS;
1514 if (!verbose)
1515 format |= FMT_NOCOUNTS;
1516 else
1517 format |= FMT_VIA;
1518
1519 if (numeric)
1520 format |= FMT_NUMERIC;
1521
1522 if (!expanded)
1523 format |= FMT_KILOMEGAGIGA;
1524
1525 if (linenumbers)
1526 format |= FMT_LINENUMBERS;
1527
Rusty Russell9e1d2142000-04-23 09:11:12 +00001528 for (this = iptc_first_chain(handle);
1529 this;
1530 this = iptc_next_chain(handle)) {
1531 const struct ipt_entry *i;
1532 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001533
Marc Bouchere6869a82000-03-20 06:03:29 +00001534 if (chain && strcmp(chain, this) != 0)
1535 continue;
1536
1537 if (found) printf("\n");
1538
1539 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001540 i = iptc_first_rule(this, handle);
1541
1542 num = 0;
1543 while (i) {
1544 print_firewall(i,
1545 iptc_get_target(i, handle),
1546 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001547 format,
1548 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001549 i = iptc_next_rule(i, handle);
1550 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001551 found = 1;
1552 }
1553
1554 errno = ENOENT;
1555 return found;
1556}
1557
Harald Welte82dd2ec2000-12-19 05:18:15 +00001558static char *get_modprobe(void)
1559{
1560 int procfile;
1561 char *ret;
1562
1563 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1564 if (procfile < 0)
1565 return NULL;
1566
1567 ret = malloc(1024);
1568 if (ret) {
1569 switch (read(procfile, ret, 1024)) {
1570 case -1: goto fail;
1571 case 1024: goto fail; /* Partial read. Wierd */
1572 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001573 if (ret[strlen(ret)-1]=='\n')
1574 ret[strlen(ret)-1]=0;
1575 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001576 return ret;
1577 }
1578 fail:
1579 free(ret);
1580 close(procfile);
1581 return NULL;
1582}
1583
Harald Welte58918652001-06-16 18:25:25 +00001584int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001585{
1586 char *buf = NULL;
1587 char *argv[3];
1588
1589 /* If they don't explicitly set it, read out of kernel */
1590 if (!modprobe) {
1591 buf = get_modprobe();
1592 if (!buf)
1593 return -1;
1594 modprobe = buf;
1595 }
1596
1597 switch (fork()) {
1598 case 0:
1599 argv[0] = (char *)modprobe;
1600 argv[1] = (char *)modname;
1601 argv[2] = NULL;
1602 execv(argv[0], argv);
1603
1604 /* not usually reached */
1605 exit(0);
1606 case -1:
1607 return -1;
1608
1609 default: /* parent */
1610 wait(NULL);
1611 }
1612
1613 free(buf);
1614 return 0;
1615}
1616
Marc Bouchere6869a82000-03-20 06:03:29 +00001617static struct ipt_entry *
1618generate_entry(const struct ipt_entry *fw,
1619 struct iptables_match *matches,
1620 struct ipt_entry_target *target)
1621{
1622 unsigned int size;
1623 struct iptables_match *m;
1624 struct ipt_entry *e;
1625
1626 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001627 for (m = matches; m; m = m->next) {
1628 if (!m->used)
1629 continue;
1630
Rusty Russell228e98d2000-04-27 10:28:06 +00001631 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001632 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001633
Rusty Russell228e98d2000-04-27 10:28:06 +00001634 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001635 *e = *fw;
1636 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001637 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001638
1639 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001640 for (m = matches; m; m = m->next) {
1641 if (!m->used)
1642 continue;
1643
Rusty Russell228e98d2000-04-27 10:28:06 +00001644 memcpy(e->elems + size, m->m, m->m->u.match_size);
1645 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001646 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001647 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001648
1649 return e;
1650}
1651
1652int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1653{
1654 struct ipt_entry fw, *e = NULL;
1655 int invert = 0;
1656 unsigned int nsaddrs = 0, ndaddrs = 0;
1657 struct in_addr *saddrs = NULL, *daddrs = NULL;
1658
1659 int c, verbose = 0;
1660 const char *chain = NULL;
1661 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1662 const char *policy = NULL, *newname = NULL;
1663 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001664 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001665 int ret = 1;
1666 struct iptables_match *m;
1667 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001668 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001669 const char *jumpto = "";
1670 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001671 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001672 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001673
1674 memset(&fw, 0, sizeof(fw));
1675
Sven Kochfb1279a2001-02-27 12:25:12 +00001676 opts = original_opts;
1677 global_option_offset = 0;
1678
Harald Welteae1ff9f2000-12-01 14:26:20 +00001679 /* re-set optind to 0 in case do_command gets called
1680 * a second time */
1681 optind = 0;
1682
1683 /* clear mflags in case do_command gets called a second time
1684 * (we clear the global list of all matches for security)*/
1685 for (m = iptables_matches; m; m = m->next) {
1686 m->mflags = 0;
1687 m->used = 0;
1688 }
1689
1690 for (t = iptables_targets; t; t = t->next) {
1691 t->tflags = 0;
1692 t->used = 0;
1693 }
1694
Marc Bouchere6869a82000-03-20 06:03:29 +00001695 /* Suppress error messages: we may add new options if we
1696 demand-load a protocol. */
1697 opterr = 0;
1698
1699 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001700 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001701 opts, NULL)) != -1) {
1702 switch (c) {
1703 /*
1704 * Command selection
1705 */
1706 case 'A':
1707 add_command(&command, CMD_APPEND, CMD_NONE,
1708 invert);
1709 chain = optarg;
1710 break;
1711
1712 case 'D':
1713 add_command(&command, CMD_DELETE, CMD_NONE,
1714 invert);
1715 chain = optarg;
1716 if (optind < argc && argv[optind][0] != '-'
1717 && argv[optind][0] != '!') {
1718 rulenum = parse_rulenumber(argv[optind++]);
1719 command = CMD_DELETE_NUM;
1720 }
1721 break;
1722
Marc Bouchere6869a82000-03-20 06:03:29 +00001723 case 'R':
1724 add_command(&command, CMD_REPLACE, CMD_NONE,
1725 invert);
1726 chain = optarg;
1727 if (optind < argc && argv[optind][0] != '-'
1728 && argv[optind][0] != '!')
1729 rulenum = parse_rulenumber(argv[optind++]);
1730 else
1731 exit_error(PARAMETER_PROBLEM,
1732 "-%c requires a rule number",
1733 cmd2char(CMD_REPLACE));
1734 break;
1735
1736 case 'I':
1737 add_command(&command, CMD_INSERT, CMD_NONE,
1738 invert);
1739 chain = optarg;
1740 if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!')
1742 rulenum = parse_rulenumber(argv[optind++]);
1743 else rulenum = 1;
1744 break;
1745
1746 case 'L':
1747 add_command(&command, CMD_LIST, CMD_ZERO,
1748 invert);
1749 if (optarg) chain = optarg;
1750 else if (optind < argc && argv[optind][0] != '-'
1751 && argv[optind][0] != '!')
1752 chain = argv[optind++];
1753 break;
1754
1755 case 'F':
1756 add_command(&command, CMD_FLUSH, CMD_NONE,
1757 invert);
1758 if (optarg) chain = optarg;
1759 else if (optind < argc && argv[optind][0] != '-'
1760 && argv[optind][0] != '!')
1761 chain = argv[optind++];
1762 break;
1763
1764 case 'Z':
1765 add_command(&command, CMD_ZERO, CMD_LIST,
1766 invert);
1767 if (optarg) chain = optarg;
1768 else if (optind < argc && argv[optind][0] != '-'
1769 && argv[optind][0] != '!')
1770 chain = argv[optind++];
1771 break;
1772
1773 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001774 if (optarg && *optarg == '-')
1775 exit_error(PARAMETER_PROBLEM,
1776 "chain name not allowed to start "
1777 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001778 if (find_target(optarg, TRY_LOAD))
1779 exit_error(PARAMETER_PROBLEM,
1780 "chain name may not clash "
1781 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001782 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1783 invert);
1784 chain = optarg;
1785 break;
1786
1787 case 'X':
1788 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1789 invert);
1790 if (optarg) chain = optarg;
1791 else if (optind < argc && argv[optind][0] != '-'
1792 && argv[optind][0] != '!')
1793 chain = argv[optind++];
1794 break;
1795
1796 case 'E':
1797 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1798 invert);
1799 chain = optarg;
1800 if (optind < argc && argv[optind][0] != '-'
1801 && argv[optind][0] != '!')
1802 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001803 else
1804 exit_error(PARAMETER_PROBLEM,
1805 "-%c requires old-chain-name and "
1806 "new-chain-name",
1807 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001808 break;
1809
1810 case 'P':
1811 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1812 invert);
1813 chain = optarg;
1814 if (optind < argc && argv[optind][0] != '-'
1815 && argv[optind][0] != '!')
1816 policy = argv[optind++];
1817 else
1818 exit_error(PARAMETER_PROBLEM,
1819 "-%c requires a chain and a policy",
1820 cmd2char(CMD_SET_POLICY));
1821 break;
1822
1823 case 'h':
1824 if (!optarg)
1825 optarg = argv[optind];
1826
Rusty Russell2e0a3212000-04-19 11:23:18 +00001827 /* iptables -p icmp -h */
1828 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001829 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001830
Marc Bouchere6869a82000-03-20 06:03:29 +00001831 exit_printhelp();
1832
1833 /*
1834 * Option selection
1835 */
1836 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001837 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001838 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1839 invert);
1840
1841 /* Canonicalize into lower case */
1842 for (protocol = argv[optind-1]; *protocol; protocol++)
1843 *protocol = tolower(*protocol);
1844
1845 protocol = argv[optind-1];
1846 fw.ip.proto = parse_protocol(protocol);
1847
1848 if (fw.ip.proto == 0
1849 && (fw.ip.invflags & IPT_INV_PROTO))
1850 exit_error(PARAMETER_PROBLEM,
1851 "rule would never match protocol");
1852 fw.nfcache |= NFC_IP_PROTO;
1853 break;
1854
1855 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001856 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001857 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1858 invert);
1859 shostnetworkmask = argv[optind-1];
1860 fw.nfcache |= NFC_IP_SRC;
1861 break;
1862
1863 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001864 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001865 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1866 invert);
1867 dhostnetworkmask = argv[optind-1];
1868 fw.nfcache |= NFC_IP_DST;
1869 break;
1870
1871 case 'j':
1872 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1873 invert);
1874 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001875 /* TRY_LOAD (may be chain name) */
1876 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001877
1878 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001879 size_t size;
1880
Rusty Russell73f72f52000-07-03 10:17:57 +00001881 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1882 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001883
Rusty Russell2e0a3212000-04-19 11:23:18 +00001884 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001885 target->t->u.target_size = size;
1886 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001887 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001888 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001889 }
1890 break;
1891
1892
1893 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001894 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001895 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1896 invert);
1897 parse_interface(argv[optind-1],
1898 fw.ip.iniface,
1899 fw.ip.iniface_mask);
1900 fw.nfcache |= NFC_IP_IF_IN;
1901 break;
1902
1903 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001904 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1906 invert);
1907 parse_interface(argv[optind-1],
1908 fw.ip.outiface,
1909 fw.ip.outiface_mask);
1910 fw.nfcache |= NFC_IP_IF_OUT;
1911 break;
1912
1913 case 'f':
1914 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1915 invert);
1916 fw.ip.flags |= IPT_F_FRAG;
1917 fw.nfcache |= NFC_IP_FRAG;
1918 break;
1919
1920 case 'v':
1921 if (!verbose)
1922 set_option(&options, OPT_VERBOSE,
1923 &fw.ip.invflags, invert);
1924 verbose++;
1925 break;
1926
Rusty Russell52a51492000-05-02 16:44:29 +00001927 case 'm': {
1928 size_t size;
1929
Marc Bouchere6869a82000-03-20 06:03:29 +00001930 if (invert)
1931 exit_error(PARAMETER_PROBLEM,
1932 "unexpected ! flag before --match");
1933
Rusty Russell52a51492000-05-02 16:44:29 +00001934 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001935 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1936 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001937 m->m = fw_calloc(1, size);
1938 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001939 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001940 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001941 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001942 }
1943 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001944
1945 case 'n':
1946 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1947 invert);
1948 break;
1949
1950 case 't':
1951 if (invert)
1952 exit_error(PARAMETER_PROBLEM,
1953 "unexpected ! flag before --table");
1954 *table = argv[optind-1];
1955 break;
1956
1957 case 'x':
1958 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1959 invert);
1960 break;
1961
1962 case 'V':
1963 if (invert)
1964 printf("Not %s ;-)\n", program_version);
1965 else
1966 printf("%s v%s\n",
1967 program_name, program_version);
1968 exit(0);
1969
1970 case '0':
1971 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1972 invert);
1973 break;
1974
Harald Welte82dd2ec2000-12-19 05:18:15 +00001975 case 'M':
1976 modprobe = optarg;
1977 break;
1978
Harald Welteccd49e52001-01-23 22:54:34 +00001979 case 'c':
1980
1981 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1982 invert);
1983 pcnt = optarg;
1984 if (optind < argc && argv[optind][0] != '-'
1985 && argv[optind][0] != '!')
1986 bcnt = argv[optind++];
1987 else
1988 exit_error(PARAMETER_PROBLEM,
1989 "-%c requires packet and byte counter",
1990 opt2char(OPT_COUNTERS));
1991
1992 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1993 exit_error(PARAMETER_PROBLEM,
1994 "-%c packet counter not numeric",
1995 opt2char(OPT_COUNTERS));
1996
1997 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1998 exit_error(PARAMETER_PROBLEM,
1999 "-%c byte counter not numeric",
2000 opt2char(OPT_COUNTERS));
2001
2002 break;
2003
2004
Marc Bouchere6869a82000-03-20 06:03:29 +00002005 case 1: /* non option */
2006 if (optarg[0] == '!' && optarg[1] == '\0') {
2007 if (invert)
2008 exit_error(PARAMETER_PROBLEM,
2009 "multiple consecutive ! not"
2010 " allowed");
2011 invert = TRUE;
2012 optarg[0] = '\0';
2013 continue;
2014 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002015 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 exit_tryhelp(2);
2017
2018 default:
2019 /* FIXME: This scheme doesn't allow two of the same
2020 matches --RR */
2021 if (!target
2022 || !(target->parse(c - target->option_offset,
2023 argv, invert,
2024 &target->tflags,
2025 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002026 for (m = iptables_matches; m; m = m->next) {
2027 if (!m->used)
2028 continue;
2029
Marc Bouchere6869a82000-03-20 06:03:29 +00002030 if (m->parse(c - m->option_offset,
2031 argv, invert,
2032 &m->mflags,
2033 &fw,
2034 &fw.nfcache,
2035 &m->m))
2036 break;
2037 }
Harald Welte2d86b772002-08-26 12:21:44 +00002038
2039 /* If you listen carefully, you can
2040 actually hear this code suck. */
2041
2042 /* some explanations (after four different bugs
2043 * in 3 different releases): If we encountere a
2044 * parameter, that has not been parsed yet,
2045 * it's not an option of an explicitly loaded
2046 * match or a target. However, we support
2047 * implicit loading of the protocol match
2048 * extension. '-p tcp' means 'l4 proto 6' and
2049 * at the same time 'load tcp protocol match on
2050 * demand if we specify --dport'.
2051 *
2052 * To make this work, we need to make sure:
2053 * - the parameter has not been parsed by
2054 * a match (m above)
2055 * - a protocol has been specified
2056 * - the protocol extension has not been
2057 * loaded yet, or is loaded and unused
2058 * [think of iptables-restore!]
2059 * - the protocol extension can be successively
2060 * loaded
2061 */
2062 if (m == NULL
2063 && protocol
2064 && (!find_proto(protocol, DONT_LOAD,
2065 options&OPT_NUMERIC)
2066 || (find_proto(protocol, DONT_LOAD,
2067 options&OPT_NUMERIC)
2068 && (proto_used == 0))
2069 )
2070 && (m = find_proto(protocol, TRY_LOAD,
2071 options&OPT_NUMERIC))) {
2072 /* Try loading protocol */
2073 size_t size;
2074
2075 proto_used = 1;
2076
2077 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2078 + m->size;
2079
2080 m->m = fw_calloc(1, size);
2081 m->m->u.match_size = size;
2082 strcpy(m->m->u.user.name, m->name);
2083 m->init(m->m, &fw.nfcache);
2084
2085 opts = merge_options(opts,
2086 m->extra_opts, &m->option_offset);
2087
2088 optind--;
2089 continue;
2090 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002091 if (!m)
2092 exit_error(PARAMETER_PROBLEM,
2093 "Unknown arg `%s'",
2094 argv[optind-1]);
2095 }
2096 }
2097 invert = FALSE;
2098 }
2099
Sven Kochfb1279a2001-02-27 12:25:12 +00002100 for (m = iptables_matches; m; m = m->next) {
2101 if (!m->used)
2102 continue;
2103
Marc Bouchere6869a82000-03-20 06:03:29 +00002104 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002105 }
2106
Marc Bouchere6869a82000-03-20 06:03:29 +00002107 if (target)
2108 target->final_check(target->tflags);
2109
2110 /* Fix me: must put inverse options checking here --MN */
2111
2112 if (optind < argc)
2113 exit_error(PARAMETER_PROBLEM,
2114 "unknown arguments found on commandline");
2115 if (!command)
2116 exit_error(PARAMETER_PROBLEM, "no command specified");
2117 if (invert)
2118 exit_error(PARAMETER_PROBLEM,
2119 "nothing appropriate following !");
2120
Harald Welte6336bfd2002-05-07 14:41:43 +00002121 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002122 if (!(options & OPT_DESTINATION))
2123 dhostnetworkmask = "0.0.0.0/0";
2124 if (!(options & OPT_SOURCE))
2125 shostnetworkmask = "0.0.0.0/0";
2126 }
2127
2128 if (shostnetworkmask)
2129 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2130 &(fw.ip.smsk), &nsaddrs);
2131
2132 if (dhostnetworkmask)
2133 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2134 &(fw.ip.dmsk), &ndaddrs);
2135
2136 if ((nsaddrs > 1 || ndaddrs > 1) &&
2137 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2138 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2139 " source or destination IP addresses");
2140
Marc Bouchere6869a82000-03-20 06:03:29 +00002141 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2142 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2143 "specify a unique address");
2144
2145 generic_opt_check(command, options);
2146
2147 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2148 exit_error(PARAMETER_PROBLEM,
2149 "chain name `%s' too long (must be under %i chars)",
2150 chain, IPT_FUNCTION_MAXNAMELEN);
2151
Harald Welteae1ff9f2000-12-01 14:26:20 +00002152 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002153 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002154 *handle = iptc_init(*table);
2155
Harald Welte82dd2ec2000-12-19 05:18:15 +00002156 if (!*handle) {
2157 /* try to insmod the module if iptc_init failed */
2158 iptables_insmod("ip_tables", modprobe);
2159 *handle = iptc_init(*table);
2160 }
2161
Marc Bouchere6869a82000-03-20 06:03:29 +00002162 if (!*handle)
2163 exit_error(VERSION_PROBLEM,
2164 "can't initialize iptables table `%s': %s",
2165 *table, iptc_strerror(errno));
2166
Harald Welte6336bfd2002-05-07 14:41:43 +00002167 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002168 || command == CMD_DELETE
2169 || command == CMD_INSERT
2170 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002171 if (strcmp(chain, "PREROUTING") == 0
2172 || strcmp(chain, "INPUT") == 0) {
2173 /* -o not valid with incoming packets. */
2174 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002175 exit_error(PARAMETER_PROBLEM,
2176 "Can't use -%c with %s\n",
2177 opt2char(OPT_VIANAMEOUT),
2178 chain);
2179 }
2180
Rusty Russella4860fd2000-06-17 16:13:02 +00002181 if (strcmp(chain, "POSTROUTING") == 0
2182 || strcmp(chain, "OUTPUT") == 0) {
2183 /* -i not valid with outgoing packets */
2184 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002185 exit_error(PARAMETER_PROBLEM,
2186 "Can't use -%c with %s\n",
2187 opt2char(OPT_VIANAMEIN),
2188 chain);
2189 }
2190
2191 if (target && iptc_is_chain(jumpto, *handle)) {
2192 printf("Warning: using chain %s, not extension\n",
2193 jumpto);
2194
2195 target = NULL;
2196 }
2197
2198 /* If they didn't specify a target, or it's a chain
2199 name, use standard. */
2200 if (!target
2201 && (strlen(jumpto) == 0
2202 || iptc_is_chain(jumpto, *handle))) {
2203 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002204
Rusty Russell52a51492000-05-02 16:44:29 +00002205 target = find_target(IPT_STANDARD_TARGET,
2206 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002207
2208 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002209 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002210 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002211 target->t->u.target_size = size;
2212 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002213 target->init(target->t, &fw.nfcache);
2214 }
2215
Rusty Russell7e53bf92000-03-20 07:03:28 +00002216 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002217 /* it is no chain, and we can't load a plugin.
2218 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002219 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002220 * chain. */
2221 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002222 } else {
2223 e = generate_entry(&fw, iptables_matches, target->t);
2224 }
2225 }
2226
2227 switch (command) {
2228 case CMD_APPEND:
2229 ret = append_entry(chain, e,
2230 nsaddrs, saddrs, ndaddrs, daddrs,
2231 options&OPT_VERBOSE,
2232 handle);
2233 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002234 case CMD_DELETE:
2235 ret = delete_entry(chain, e,
2236 nsaddrs, saddrs, ndaddrs, daddrs,
2237 options&OPT_VERBOSE,
2238 handle);
2239 break;
2240 case CMD_DELETE_NUM:
2241 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2242 break;
2243 case CMD_REPLACE:
2244 ret = replace_entry(chain, e, rulenum - 1,
2245 saddrs, daddrs, options&OPT_VERBOSE,
2246 handle);
2247 break;
2248 case CMD_INSERT:
2249 ret = insert_entry(chain, e, rulenum - 1,
2250 nsaddrs, saddrs, ndaddrs, daddrs,
2251 options&OPT_VERBOSE,
2252 handle);
2253 break;
2254 case CMD_LIST:
2255 ret = list_entries(chain,
2256 options&OPT_VERBOSE,
2257 options&OPT_NUMERIC,
2258 options&OPT_EXPANDED,
2259 options&OPT_LINENUMBERS,
2260 handle);
2261 break;
2262 case CMD_FLUSH:
2263 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2264 break;
2265 case CMD_ZERO:
2266 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2267 break;
2268 case CMD_LIST|CMD_ZERO:
2269 ret = list_entries(chain,
2270 options&OPT_VERBOSE,
2271 options&OPT_NUMERIC,
2272 options&OPT_EXPANDED,
2273 options&OPT_LINENUMBERS,
2274 handle);
2275 if (ret)
2276 ret = zero_entries(chain,
2277 options&OPT_VERBOSE, handle);
2278 break;
2279 case CMD_NEW_CHAIN:
2280 ret = iptc_create_chain(chain, handle);
2281 break;
2282 case CMD_DELETE_CHAIN:
2283 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2284 break;
2285 case CMD_RENAME_CHAIN:
2286 ret = iptc_rename_chain(chain, newname, handle);
2287 break;
2288 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002289 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002290 break;
2291 default:
2292 /* We should never reach this... */
2293 exit_tryhelp(2);
2294 }
2295
2296 if (verbose > 1)
2297 dump_entries(*handle);
2298
Marc Bouchere6869a82000-03-20 06:03:29 +00002299 return ret;
2300}