blob: ae6538c049437c3880a089e360286246fb37df3b [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 },
Harald Welte12915232004-02-21 09:20:34 +0000238 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000239 { "all", 0 },
240};
241
242static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000243proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000244{
245 unsigned int i;
246
Rusty Russell28381a42000-05-10 00:19:50 +0000247 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000248 struct protoent *pent = getprotobynumber(proto);
249 if (pent)
250 return pent->p_name;
251 }
252
253 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
254 if (chain_protos[i].num == proto)
255 return chain_protos[i].name;
256
257 return NULL;
258}
259
260struct in_addr *
261dotted_to_addr(const char *dotted)
262{
263 static struct in_addr addr;
264 unsigned char *addrp;
265 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000266 unsigned int onebyte;
267 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000268 char buf[20];
269
270 /* copy dotted string, because we need to modify it */
271 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000272 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000273 addrp = (unsigned char *) &(addr.s_addr);
274
275 p = buf;
276 for (i = 0; i < 3; i++) {
277 if ((q = strchr(p, '.')) == NULL)
278 return (struct in_addr *) NULL;
279
280 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000281 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 return (struct in_addr *) NULL;
283
284 addrp[i] = (unsigned char) onebyte;
285 p = q + 1;
286 }
287
288 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000289 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000290 return (struct in_addr *) NULL;
291
292 addrp[3] = (unsigned char) onebyte;
293
294 return &addr;
295}
296
297static struct in_addr *
298network_to_addr(const char *name)
299{
300 struct netent *net;
301 static struct in_addr addr;
302
303 if ((net = getnetbyname(name)) != NULL) {
304 if (net->n_addrtype != AF_INET)
305 return (struct in_addr *) NULL;
306 addr.s_addr = htonl((unsigned long) net->n_net);
307 return &addr;
308 }
309
310 return (struct in_addr *) NULL;
311}
312
313static void
314inaddrcpy(struct in_addr *dst, struct in_addr *src)
315{
316 /* memcpy(dst, src, sizeof(struct in_addr)); */
317 dst->s_addr = src->s_addr;
318}
319
320void
321exit_error(enum exittype status, char *msg, ...)
322{
323 va_list args;
324
325 va_start(args, msg);
326 fprintf(stderr, "%s v%s: ", program_name, program_version);
327 vfprintf(stderr, msg, args);
328 va_end(args);
329 fprintf(stderr, "\n");
330 if (status == PARAMETER_PROBLEM)
331 exit_tryhelp(status);
332 if (status == VERSION_PROBLEM)
333 fprintf(stderr,
334 "Perhaps iptables or your kernel needs to be upgraded.\n");
335 exit(status);
336}
337
338void
339exit_tryhelp(int status)
340{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000341 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000342 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000343 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
344 program_name, program_name );
345 exit(status);
346}
347
348void
349exit_printhelp(void)
350{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000351 struct iptables_match *m = NULL;
352 struct iptables_target *t = NULL;
353
Marc Bouchere6869a82000-03-20 06:03:29 +0000354 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000355"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000356" %s -[RI] chain rulenum rule-specification [options]\n"
357" %s -D chain rulenum [options]\n"
358" %s -[LFZ] [chain] [options]\n"
359" %s -[NX] chain\n"
360" %s -E old-chain-name new-chain-name\n"
361" %s -P chain target [options]\n"
362" %s -h (print this help information)\n\n",
363 program_name, program_version, program_name, program_name,
364 program_name, program_name, program_name, program_name,
365 program_name, program_name);
366
367 printf(
368"Commands:\n"
369"Either long or short options are allowed.\n"
370" --append -A chain Append to chain\n"
371" --delete -D chain Delete matching rule from chain\n"
372" --delete -D chain rulenum\n"
373" Delete rule rulenum (1 = first) from chain\n"
374" --insert -I chain [rulenum]\n"
375" Insert in chain as rulenum (default 1=first)\n"
376" --replace -R chain rulenum\n"
377" Replace rule rulenum (1 = first) in chain\n"
378" --list -L [chain] List the rules in a chain or all chains\n"
379" --flush -F [chain] Delete all rules in chain or all chains\n"
380" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000381" --new -N chain Create a new user-defined chain\n"
382" --delete-chain\n"
383" -X [chain] Delete a user-defined chain\n"
384" --policy -P chain target\n"
385" Change policy on chain to target\n"
386" --rename-chain\n"
387" -E old-chain new-chain\n"
388" Change chain name, (moving any references)\n"
389
390"Options:\n"
391" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
392" --source -s [!] address[/mask]\n"
393" source specification\n"
394" --destination -d [!] address[/mask]\n"
395" destination specification\n"
396" --in-interface -i [!] input name[+]\n"
397" network interface name ([+] for wildcard)\n"
398" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000399" target for rule (may load target extension)\n"
400" --match -m match\n"
401" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000402" --numeric -n numeric output of addresses and ports\n"
403" --out-interface -o [!] output name[+]\n"
404" network interface name ([+] for wildcard)\n"
405" --table -t table table to manipulate (default: `filter')\n"
406" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000407" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000408" --exact -x expand numbers (display exact values)\n"
409"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000410" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000411" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000412"[!] --version -V print package version.\n");
413
Rusty Russell363112d2000-08-11 13:49:26 +0000414 /* Print out any special helps. A user might like to be able
415 to add a --help to the commandline, and see expected
416 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000417 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000418 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000419 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000420 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000421 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000422 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000423 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000424 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000425 exit(0);
426}
427
428static void
429generic_opt_check(int command, int options)
430{
431 int i, j, legal = 0;
432
433 /* Check that commands are valid with options. Complicated by the
434 * fact that if an option is legal with *any* command given, it is
435 * legal overall (ie. -z and -l).
436 */
437 for (i = 0; i < NUMBER_OF_OPT; i++) {
438 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
439
440 for (j = 0; j < NUMBER_OF_CMD; j++) {
441 if (!(command & (1<<j)))
442 continue;
443
444 if (!(options & (1<<i))) {
445 if (commands_v_options[j][i] == '+')
446 exit_error(PARAMETER_PROBLEM,
447 "You need to supply the `-%c' "
448 "option for this command\n",
449 optflags[i]);
450 } else {
451 if (commands_v_options[j][i] != 'x')
452 legal = 1;
453 else if (legal == 0)
454 legal = -1;
455 }
456 }
457 if (legal == -1)
458 exit_error(PARAMETER_PROBLEM,
459 "Illegal option `-%c' with this command\n",
460 optflags[i]);
461 }
462}
463
464static char
465opt2char(int option)
466{
467 const char *ptr;
468 for (ptr = optflags; option > 1; option >>= 1, ptr++);
469
470 return *ptr;
471}
472
473static char
474cmd2char(int option)
475{
476 const char *ptr;
477 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
478
479 return *ptr;
480}
481
482static void
483add_command(int *cmd, const int newcmd, const int othercmds, int invert)
484{
485 if (invert)
486 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
487 if (*cmd & (~othercmds))
488 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
489 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
490 *cmd |= newcmd;
491}
492
493int
Harald Welteb77f1da2002-03-14 11:35:58 +0000494check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000495{
496 if (option && strcmp(option, "!") == 0) {
497 if (*invert)
498 exit_error(PARAMETER_PROBLEM,
499 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000500 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000501 if (optind) {
502 *optind = *optind+1;
503 if (argc && *optind > argc)
504 exit_error(PARAMETER_PROBLEM,
505 "no argument following `!'");
506 }
507
Marc Bouchere6869a82000-03-20 06:03:29 +0000508 return TRUE;
509 }
510 return FALSE;
511}
512
513static void *
514fw_calloc(size_t count, size_t size)
515{
516 void *p;
517
518 if ((p = calloc(count, size)) == NULL) {
519 perror("iptables: calloc failed");
520 exit(1);
521 }
522 return p;
523}
524
525static void *
526fw_malloc(size_t size)
527{
528 void *p;
529
530 if ((p = malloc(size)) == NULL) {
531 perror("iptables: malloc failed");
532 exit(1);
533 }
534 return p;
535}
536
537static struct in_addr *
538host_to_addr(const char *name, unsigned int *naddr)
539{
540 struct hostent *host;
541 struct in_addr *addr;
542 unsigned int i;
543
544 *naddr = 0;
545 if ((host = gethostbyname(name)) != NULL) {
546 if (host->h_addrtype != AF_INET ||
547 host->h_length != sizeof(struct in_addr))
548 return (struct in_addr *) NULL;
549
550 while (host->h_addr_list[*naddr] != (char *) NULL)
551 (*naddr)++;
552 addr = fw_calloc(*naddr, sizeof(struct in_addr));
553 for (i = 0; i < *naddr; i++)
554 inaddrcpy(&(addr[i]),
555 (struct in_addr *) host->h_addr_list[i]);
556 return addr;
557 }
558
559 return (struct in_addr *) NULL;
560}
561
562static char *
563addr_to_host(const struct in_addr *addr)
564{
565 struct hostent *host;
566
567 if ((host = gethostbyaddr((char *) addr,
568 sizeof(struct in_addr), AF_INET)) != NULL)
569 return (char *) host->h_name;
570
571 return (char *) NULL;
572}
573
574/*
575 * All functions starting with "parse" should succeed, otherwise
576 * the program fails.
577 * Most routines return pointers to static data that may change
578 * between calls to the same or other routines with a few exceptions:
579 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
580 * return global static data.
581*/
582
583static struct in_addr *
584parse_hostnetwork(const char *name, unsigned int *naddrs)
585{
586 struct in_addr *addrp, *addrptmp;
587
588 if ((addrptmp = dotted_to_addr(name)) != NULL ||
589 (addrptmp = network_to_addr(name)) != NULL) {
590 addrp = fw_malloc(sizeof(struct in_addr));
591 inaddrcpy(addrp, addrptmp);
592 *naddrs = 1;
593 return addrp;
594 }
595 if ((addrp = host_to_addr(name, naddrs)) != NULL)
596 return addrp;
597
598 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
599}
600
601static struct in_addr *
602parse_mask(char *mask)
603{
604 static struct in_addr maskaddr;
605 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000606 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000607
608 if (mask == NULL) {
609 /* no mask at all defaults to 32 bits */
610 maskaddr.s_addr = 0xFFFFFFFF;
611 return &maskaddr;
612 }
613 if ((addrp = dotted_to_addr(mask)) != NULL)
614 /* dotted_to_addr already returns a network byte order addr */
615 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000616 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000617 exit_error(PARAMETER_PROBLEM,
618 "invalid mask `%s' specified", mask);
619 if (bits != 0) {
620 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
621 return &maskaddr;
622 }
623
624 maskaddr.s_addr = 0L;
625 return &maskaddr;
626}
627
Marc Boucherb93c7982001-12-06 14:50:19 +0000628void
Marc Bouchere6869a82000-03-20 06:03:29 +0000629parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
630 struct in_addr *maskp, unsigned int *naddrs)
631{
632 struct in_addr *addrp;
633 char buf[256];
634 char *p;
635 int i, j, k, n;
636
637 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000638 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000639 if ((p = strrchr(buf, '/')) != NULL) {
640 *p = '\0';
641 addrp = parse_mask(p + 1);
642 } else
643 addrp = parse_mask(NULL);
644 inaddrcpy(maskp, addrp);
645
646 /* if a null mask is given, the name is ignored, like in "any/0" */
647 if (maskp->s_addr == 0L)
648 strcpy(buf, "0.0.0.0");
649
650 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
651 n = *naddrs;
652 for (i = 0, j = 0; i < n; i++) {
653 addrp[j++].s_addr &= maskp->s_addr;
654 for (k = 0; k < j - 1; k++) {
655 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
656 (*naddrs)--;
657 j--;
658 break;
659 }
660 }
661 }
662}
663
664struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000665find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000666{
667 struct iptables_match *ptr;
668
669 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
670 if (strcmp(name, ptr->name) == 0)
671 break;
672 }
673
Harald Welte3efb6ea2001-08-06 18:50:21 +0000674#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000675 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000676 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
677 + strlen(name)];
678 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000679 if (dlopen(path, RTLD_NOW)) {
680 /* Found library. If it didn't register itself,
681 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000682 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000683
Rusty Russell9e1d2142000-04-23 09:11:12 +0000684 if (!ptr)
685 exit_error(PARAMETER_PROBLEM,
686 "Couldn't load match `%s'\n",
687 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000688 } else if (tryload == LOAD_MUST_SUCCEED)
689 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000690 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000691 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000692 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000693#else
694 if (ptr && !ptr->loaded) {
695 if (tryload != DONT_LOAD)
696 ptr->loaded = 1;
697 else
698 ptr = NULL;
699 }
Marc Boucher067477b2002-03-24 15:09:31 +0000700 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
701 exit_error(PARAMETER_PROBLEM,
702 "Couldn't find match `%s'\n", name);
703 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000704#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000705
Martin Josefsson78cafda2004-02-02 20:01:18 +0000706 if (ptr && matches) {
707 struct iptables_rule_match **i;
708 struct iptables_rule_match *newentry;
709
710 newentry = fw_malloc(sizeof(struct iptables_rule_match));
711
712 for (i = matches; *i; i = &(*i)->next);
713 newentry->match = ptr;
714 newentry->next = NULL;
715 *i = newentry;
716 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000717
Marc Bouchere6869a82000-03-20 06:03:29 +0000718 return ptr;
719}
720
Rusty Russell28381a42000-05-10 00:19:50 +0000721/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
722static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000723find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000724{
Harald Welteed498492001-07-23 01:24:22 +0000725 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000726
Harald Welte0b0013a2002-02-18 16:15:31 +0000727 if (string_to_number(pname, 0, 255, &proto) != -1) {
728 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000729
Harald Welte0b0013a2002-02-18 16:15:31 +0000730 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000731 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000732 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000733 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000734
735 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000736}
737
Marc Boucherb93c7982001-12-06 14:50:19 +0000738u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000739parse_protocol(const char *s)
740{
Harald Welteed498492001-07-23 01:24:22 +0000741 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000742
Harald Welteed498492001-07-23 01:24:22 +0000743 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000744 struct protoent *pent;
745
746 if ((pent = getprotobyname(s)))
747 proto = pent->p_proto;
748 else {
749 unsigned int i;
750 for (i = 0;
751 i < sizeof(chain_protos)/sizeof(struct pprot);
752 i++) {
753 if (strcmp(s, chain_protos[i].name) == 0) {
754 proto = chain_protos[i].num;
755 break;
756 }
757 }
758 if (i == sizeof(chain_protos)/sizeof(struct pprot))
759 exit_error(PARAMETER_PROBLEM,
760 "unknown protocol `%s' specified",
761 s);
762 }
763 }
764
765 return (u_int16_t)proto;
766}
767
768static void
769parse_interface(const char *arg, char *vianame, unsigned char *mask)
770{
771 int vialen = strlen(arg);
772 unsigned int i;
773
774 memset(mask, 0, IFNAMSIZ);
775 memset(vianame, 0, IFNAMSIZ);
776
777 if (vialen + 1 > IFNAMSIZ)
778 exit_error(PARAMETER_PROBLEM,
779 "interface name `%s' must be shorter than IFNAMSIZ"
780 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000781
Marc Bouchere6869a82000-03-20 06:03:29 +0000782 strcpy(vianame, arg);
783 if (vialen == 0)
784 memset(mask, 0, IFNAMSIZ);
785 else if (vianame[vialen - 1] == '+') {
786 memset(mask, 0xFF, vialen - 1);
787 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000788 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000789 } else {
790 /* Include nul-terminator in match */
791 memset(mask, 0xFF, vialen + 1);
792 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000793 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000794 if (!isalnum(vianame[i])
795 && vianame[i] != '_'
796 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000797 printf("Warning: wierd character in interface"
798 " `%s' (No aliases, :, ! or *).\n",
799 vianame);
800 break;
801 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 }
803 }
804}
805
806/* Can't be zero. */
807static int
808parse_rulenumber(const char *rule)
809{
Harald Welteed498492001-07-23 01:24:22 +0000810 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000811
Harald Welteed498492001-07-23 01:24:22 +0000812 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000813 exit_error(PARAMETER_PROBLEM,
814 "Invalid rule number `%s'", rule);
815
816 return rulenum;
817}
818
819static const char *
820parse_target(const char *targetname)
821{
822 const char *ptr;
823
824 if (strlen(targetname) < 1)
825 exit_error(PARAMETER_PROBLEM,
826 "Invalid target name (too short)");
827
828 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
829 exit_error(PARAMETER_PROBLEM,
830 "Invalid target name `%s' (%i chars max)",
831 targetname, sizeof(ipt_chainlabel)-1);
832
833 for (ptr = targetname; *ptr; ptr++)
834 if (isspace(*ptr))
835 exit_error(PARAMETER_PROBLEM,
836 "Invalid target name `%s'", targetname);
837 return targetname;
838}
839
840static char *
841addr_to_network(const struct in_addr *addr)
842{
843 struct netent *net;
844
845 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
846 return (char *) net->n_name;
847
848 return (char *) NULL;
849}
850
851char *
852addr_to_dotted(const struct in_addr *addrp)
853{
854 static char buf[20];
855 const unsigned char *bytep;
856
857 bytep = (const unsigned char *) &(addrp->s_addr);
858 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
859 return buf;
860}
Marc Boucherb93c7982001-12-06 14:50:19 +0000861
862char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000863addr_to_anyname(const struct in_addr *addr)
864{
865 char *name;
866
867 if ((name = addr_to_host(addr)) != NULL ||
868 (name = addr_to_network(addr)) != NULL)
869 return name;
870
871 return addr_to_dotted(addr);
872}
873
Marc Boucherb93c7982001-12-06 14:50:19 +0000874char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000875mask_to_dotted(const struct in_addr *mask)
876{
877 int i;
878 static char buf[20];
879 u_int32_t maskaddr, bits;
880
881 maskaddr = ntohl(mask->s_addr);
882
883 if (maskaddr == 0xFFFFFFFFL)
884 /* we don't want to see "/32" */
885 return "";
886
887 i = 32;
888 bits = 0xFFFFFFFEL;
889 while (--i >= 0 && maskaddr != bits)
890 bits <<= 1;
891 if (i >= 0)
892 sprintf(buf, "/%d", i);
893 else
894 /* mask was not a decent combination of 1's and 0's */
895 sprintf(buf, "/%s", addr_to_dotted(mask));
896
897 return buf;
898}
899
900int
Harald Welteed498492001-07-23 01:24:22 +0000901string_to_number(const char *s, unsigned int min, unsigned int max,
902 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000903{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000904 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000905 char *end;
906
907 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000908 errno = 0;
909 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000910 if (*end == '\0' && end != s) {
911 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000912 if (errno != ERANGE && min <= number && number <= max) {
913 *ret = number;
914 return 0;
915 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000916 }
917 return -1;
918}
919
920static void
921set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
922 int invert)
923{
924 if (*options & option)
925 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
926 opt2char(option));
927 *options |= option;
928
929 if (invert) {
930 unsigned int i;
931 for (i = 0; 1 << i != option; i++);
932
933 if (!inverse_for_options[i])
934 exit_error(PARAMETER_PROBLEM,
935 "cannot have ! before -%c",
936 opt2char(option));
937 *invflg |= inverse_for_options[i];
938 }
939}
940
941struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000942find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000943{
944 struct iptables_target *ptr;
945
946 /* Standard target? */
947 if (strcmp(name, "") == 0
948 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
949 || strcmp(name, IPTC_LABEL_DROP) == 0
950 || strcmp(name, IPTC_LABEL_QUEUE) == 0
951 || strcmp(name, IPTC_LABEL_RETURN) == 0)
952 name = "standard";
953
954 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
955 if (strcmp(name, ptr->name) == 0)
956 break;
957 }
958
Harald Welte3efb6ea2001-08-06 18:50:21 +0000959#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000960 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000961 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
962 + strlen(name)];
963 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000964 if (dlopen(path, RTLD_NOW)) {
965 /* Found library. If it didn't register itself,
966 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000967 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000968 if (!ptr)
969 exit_error(PARAMETER_PROBLEM,
970 "Couldn't load target `%s'\n",
971 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000972 } else if (tryload == LOAD_MUST_SUCCEED)
973 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000974 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000975 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000976 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000977#else
978 if (ptr && !ptr->loaded) {
979 if (tryload != DONT_LOAD)
980 ptr->loaded = 1;
981 else
982 ptr = NULL;
983 }
Marc Boucher067477b2002-03-24 15:09:31 +0000984 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
985 exit_error(PARAMETER_PROBLEM,
986 "Couldn't find target `%s'\n", name);
987 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000988#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000989
Harald Welteae1ff9f2000-12-01 14:26:20 +0000990 if (ptr)
991 ptr->used = 1;
992
Marc Bouchere6869a82000-03-20 06:03:29 +0000993 return ptr;
994}
995
996static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000997merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000998 unsigned int *option_offset)
999{
1000 unsigned int num_old, num_new, i;
1001 struct option *merge;
1002
1003 for (num_old = 0; oldopts[num_old].name; num_old++);
1004 for (num_new = 0; newopts[num_new].name; num_new++);
1005
1006 global_option_offset += OPTION_OFFSET;
1007 *option_offset = global_option_offset;
1008
1009 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1010 memcpy(merge, oldopts, num_old * sizeof(struct option));
1011 for (i = 0; i < num_new; i++) {
1012 merge[num_old + i] = newopts[i];
1013 merge[num_old + i].val += *option_offset;
1014 }
1015 memset(merge + num_old + num_new, 0, sizeof(struct option));
1016
1017 return merge;
1018}
1019
1020void
1021register_match(struct iptables_match *me)
1022{
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001023 struct iptables_match **i;
1024
Marc Bouchere6869a82000-03-20 06:03:29 +00001025 if (strcmp(me->version, program_version) != 0) {
1026 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1027 program_name, me->name, me->version, program_version);
1028 exit(1);
1029 }
1030
Martin Josefsson78cafda2004-02-02 20:01:18 +00001031 if (find_match(me->name, DONT_LOAD, NULL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001032 fprintf(stderr, "%s: match `%s' already registered.\n",
1033 program_name, me->name);
1034 exit(1);
1035 }
1036
Rusty Russell73f72f52000-07-03 10:17:57 +00001037 if (me->size != IPT_ALIGN(me->size)) {
1038 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1039 program_name, me->name, me->size);
1040 exit(1);
1041 }
1042
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001043 /* Append to list. */
1044 for (i = &iptables_matches; *i; i = &(*i)->next);
1045 me->next = NULL;
1046 *i = me;
1047
Marc Bouchere6869a82000-03-20 06:03:29 +00001048 me->m = NULL;
1049 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001050}
1051
1052void
1053register_target(struct iptables_target *me)
1054{
1055 if (strcmp(me->version, program_version) != 0) {
1056 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1057 program_name, me->name, me->version, program_version);
1058 exit(1);
1059 }
1060
Rusty Russell52a51492000-05-02 16:44:29 +00001061 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001062 fprintf(stderr, "%s: target `%s' already registered.\n",
1063 program_name, me->name);
1064 exit(1);
1065 }
1066
Rusty Russell73f72f52000-07-03 10:17:57 +00001067 if (me->size != IPT_ALIGN(me->size)) {
1068 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1069 program_name, me->name, me->size);
1070 exit(1);
1071 }
1072
Marc Bouchere6869a82000-03-20 06:03:29 +00001073 /* Prepend to list. */
1074 me->next = iptables_targets;
1075 iptables_targets = me;
1076 me->t = NULL;
1077 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001078}
1079
1080static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001081print_num(u_int64_t number, unsigned int format)
1082{
1083 if (format & FMT_KILOMEGAGIGA) {
1084 if (number > 99999) {
1085 number = (number + 500) / 1000;
1086 if (number > 9999) {
1087 number = (number + 500) / 1000;
1088 if (number > 9999) {
1089 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001090 if (number > 9999) {
1091 number = (number + 500) / 1000;
1092 printf(FMT("%4lluT ","%lluT "), number);
1093 }
1094 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001095 }
1096 else printf(FMT("%4lluM ","%lluM "), number);
1097 } else
1098 printf(FMT("%4lluK ","%lluK "), number);
1099 } else
1100 printf(FMT("%5llu ","%llu "), number);
1101 } else
1102 printf(FMT("%8llu ","%llu "), number);
1103}
1104
1105
1106static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001107print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1108{
1109 struct ipt_counters counters;
1110 const char *pol = iptc_get_policy(chain, &counters, handle);
1111 printf("Chain %s", chain);
1112 if (pol) {
1113 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001114 if (!(format & FMT_NOCOUNTS)) {
1115 fputc(' ', stdout);
1116 print_num(counters.pcnt, (format|FMT_NOTABLE));
1117 fputs("packets, ", stdout);
1118 print_num(counters.bcnt, (format|FMT_NOTABLE));
1119 fputs("bytes", stdout);
1120 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001121 printf(")\n");
1122 } else {
1123 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001124 if (!iptc_get_references(&refs, chain, handle))
1125 printf(" (ERROR obtaining refs)\n");
1126 else
1127 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001128 }
1129
1130 if (format & FMT_LINENUMBERS)
1131 printf(FMT("%-4s ", "%s "), "num");
1132 if (!(format & FMT_NOCOUNTS)) {
1133 if (format & FMT_KILOMEGAGIGA) {
1134 printf(FMT("%5s ","%s "), "pkts");
1135 printf(FMT("%5s ","%s "), "bytes");
1136 } else {
1137 printf(FMT("%8s ","%s "), "pkts");
1138 printf(FMT("%10s ","%s "), "bytes");
1139 }
1140 }
1141 if (!(format & FMT_NOTARGET))
1142 printf(FMT("%-9s ","%s "), "target");
1143 fputs(" prot ", stdout);
1144 if (format & FMT_OPTIONS)
1145 fputs("opt", stdout);
1146 if (format & FMT_VIA) {
1147 printf(FMT(" %-6s ","%s "), "in");
1148 printf(FMT("%-6s ","%s "), "out");
1149 }
1150 printf(FMT(" %-19s ","%s "), "source");
1151 printf(FMT(" %-19s "," %s "), "destination");
1152 printf("\n");
1153}
1154
Marc Bouchere6869a82000-03-20 06:03:29 +00001155
1156static int
1157print_match(const struct ipt_entry_match *m,
1158 const struct ipt_ip *ip,
1159 int numeric)
1160{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001161 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001162
1163 if (match) {
1164 if (match->print)
1165 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001166 else
Rusty Russellb039b022000-09-01 06:04:05 +00001167 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001168 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001169 if (m->u.user.name[0])
1170 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001171 }
1172 /* Don't stop iterating. */
1173 return 0;
1174}
1175
1176/* e is called `fw' here for hysterical raisins */
1177static void
1178print_firewall(const struct ipt_entry *fw,
1179 const char *targname,
1180 unsigned int num,
1181 unsigned int format,
1182 const iptc_handle_t handle)
1183{
1184 struct iptables_target *target = NULL;
1185 const struct ipt_entry_target *t;
1186 u_int8_t flags;
1187 char buf[BUFSIZ];
1188
Marc Bouchere6869a82000-03-20 06:03:29 +00001189 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001190 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001191 else
Rusty Russell52a51492000-05-02 16:44:29 +00001192 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001193
1194 t = ipt_get_target((struct ipt_entry *)fw);
1195 flags = fw->ip.flags;
1196
1197 if (format & FMT_LINENUMBERS)
1198 printf(FMT("%-4u ", "%u "), num+1);
1199
1200 if (!(format & FMT_NOCOUNTS)) {
1201 print_num(fw->counters.pcnt, format);
1202 print_num(fw->counters.bcnt, format);
1203 }
1204
1205 if (!(format & FMT_NOTARGET))
1206 printf(FMT("%-9s ", "%s "), targname);
1207
1208 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1209 {
Rusty Russell28381a42000-05-10 00:19:50 +00001210 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001211 if (pname)
1212 printf(FMT("%-5s", "%s "), pname);
1213 else
1214 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1215 }
1216
1217 if (format & FMT_OPTIONS) {
1218 if (format & FMT_NOTABLE)
1219 fputs("opt ", stdout);
1220 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1221 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1222 fputc(' ', stdout);
1223 }
1224
1225 if (format & FMT_VIA) {
1226 char iface[IFNAMSIZ+2];
1227
1228 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1229 iface[0] = '!';
1230 iface[1] = '\0';
1231 }
1232 else iface[0] = '\0';
1233
1234 if (fw->ip.iniface[0] != '\0') {
1235 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001236 }
1237 else if (format & FMT_NUMERIC) strcat(iface, "*");
1238 else strcat(iface, "any");
1239 printf(FMT(" %-6s ","in %s "), iface);
1240
1241 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1242 iface[0] = '!';
1243 iface[1] = '\0';
1244 }
1245 else iface[0] = '\0';
1246
1247 if (fw->ip.outiface[0] != '\0') {
1248 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001249 }
1250 else if (format & FMT_NUMERIC) strcat(iface, "*");
1251 else strcat(iface, "any");
1252 printf(FMT("%-6s ","out %s "), iface);
1253 }
1254
1255 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1256 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1257 printf(FMT("%-19s ","%s "), "anywhere");
1258 else {
1259 if (format & FMT_NUMERIC)
1260 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1261 else
1262 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1263 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1264 printf(FMT("%-19s ","%s "), buf);
1265 }
1266
1267 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1268 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001269 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 else {
1271 if (format & FMT_NUMERIC)
1272 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1273 else
1274 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1275 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001276 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001277 }
1278
1279 if (format & FMT_NOTABLE)
1280 fputs(" ", stdout);
1281
1282 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1283
1284 if (target) {
1285 if (target->print)
1286 /* Print the target information. */
1287 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001288 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001289 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001290 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001291
1292 if (!(format & FMT_NONEWLINE))
1293 fputc('\n', stdout);
1294}
1295
1296static void
1297print_firewall_line(const struct ipt_entry *fw,
1298 const iptc_handle_t h)
1299{
1300 struct ipt_entry_target *t;
1301
1302 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001303 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001304}
1305
1306static int
1307append_entry(const ipt_chainlabel chain,
1308 struct ipt_entry *fw,
1309 unsigned int nsaddrs,
1310 const struct in_addr saddrs[],
1311 unsigned int ndaddrs,
1312 const struct in_addr daddrs[],
1313 int verbose,
1314 iptc_handle_t *handle)
1315{
1316 unsigned int i, j;
1317 int ret = 1;
1318
1319 for (i = 0; i < nsaddrs; i++) {
1320 fw->ip.src.s_addr = saddrs[i].s_addr;
1321 for (j = 0; j < ndaddrs; j++) {
1322 fw->ip.dst.s_addr = daddrs[j].s_addr;
1323 if (verbose)
1324 print_firewall_line(fw, *handle);
1325 ret &= iptc_append_entry(chain, fw, handle);
1326 }
1327 }
1328
1329 return ret;
1330}
1331
1332static int
1333replace_entry(const ipt_chainlabel chain,
1334 struct ipt_entry *fw,
1335 unsigned int rulenum,
1336 const struct in_addr *saddr,
1337 const struct in_addr *daddr,
1338 int verbose,
1339 iptc_handle_t *handle)
1340{
1341 fw->ip.src.s_addr = saddr->s_addr;
1342 fw->ip.dst.s_addr = daddr->s_addr;
1343
1344 if (verbose)
1345 print_firewall_line(fw, *handle);
1346 return iptc_replace_entry(chain, fw, rulenum, handle);
1347}
1348
1349static int
1350insert_entry(const ipt_chainlabel chain,
1351 struct ipt_entry *fw,
1352 unsigned int rulenum,
1353 unsigned int nsaddrs,
1354 const struct in_addr saddrs[],
1355 unsigned int ndaddrs,
1356 const struct in_addr daddrs[],
1357 int verbose,
1358 iptc_handle_t *handle)
1359{
1360 unsigned int i, j;
1361 int ret = 1;
1362
1363 for (i = 0; i < nsaddrs; i++) {
1364 fw->ip.src.s_addr = saddrs[i].s_addr;
1365 for (j = 0; j < ndaddrs; j++) {
1366 fw->ip.dst.s_addr = daddrs[j].s_addr;
1367 if (verbose)
1368 print_firewall_line(fw, *handle);
1369 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1370 }
1371 }
1372
1373 return ret;
1374}
1375
Rusty Russell2e0a3212000-04-19 11:23:18 +00001376static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001377make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001378{
1379 /* Establish mask for comparison */
1380 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001381 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001382 unsigned char *mask, *mptr;
1383
1384 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001385 for (matchp = matches; matchp; matchp = matchp->next)
1386 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001387
Rusty Russell9e1d2142000-04-23 09:11:12 +00001388 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001389 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001390 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001391
Rusty Russell9e1d2142000-04-23 09:11:12 +00001392 memset(mask, 0xFF, sizeof(struct ipt_entry));
1393 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001394
Martin Josefsson78cafda2004-02-02 20:01:18 +00001395 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001396 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001397 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001398 + matchp->match->userspacesize);
1399 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001400 }
1401
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001402 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001403 IPT_ALIGN(sizeof(struct ipt_entry_target))
1404 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001405
1406 return mask;
1407}
1408
Marc Bouchere6869a82000-03-20 06:03:29 +00001409static int
1410delete_entry(const ipt_chainlabel chain,
1411 struct ipt_entry *fw,
1412 unsigned int nsaddrs,
1413 const struct in_addr saddrs[],
1414 unsigned int ndaddrs,
1415 const struct in_addr daddrs[],
1416 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001417 iptc_handle_t *handle,
1418 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001419{
1420 unsigned int i, j;
1421 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001422 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001423
Martin Josefsson78cafda2004-02-02 20:01:18 +00001424 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001425 for (i = 0; i < nsaddrs; i++) {
1426 fw->ip.src.s_addr = saddrs[i].s_addr;
1427 for (j = 0; j < ndaddrs; j++) {
1428 fw->ip.dst.s_addr = daddrs[j].s_addr;
1429 if (verbose)
1430 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001431 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001432 }
1433 }
1434 return ret;
1435}
1436
Harald Welteae1ff9f2000-12-01 14:26:20 +00001437int
Marc Bouchere6869a82000-03-20 06:03:29 +00001438for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001439 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001440{
1441 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001442 const char *chain;
1443 char *chains;
1444 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001445
Rusty Russell9e1d2142000-04-23 09:11:12 +00001446 chain = iptc_first_chain(handle);
1447 while (chain) {
1448 chaincount++;
1449 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001450 }
1451
Rusty Russell9e1d2142000-04-23 09:11:12 +00001452 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1453 i = 0;
1454 chain = iptc_first_chain(handle);
1455 while (chain) {
1456 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1457 i++;
1458 chain = iptc_next_chain(handle);
1459 }
1460
1461 for (i = 0; i < chaincount; i++) {
1462 if (!builtinstoo
1463 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1464 *handle))
1465 continue;
1466 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1467 }
1468
1469 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001470 return ret;
1471}
1472
Harald Welteae1ff9f2000-12-01 14:26:20 +00001473int
Marc Bouchere6869a82000-03-20 06:03:29 +00001474flush_entries(const ipt_chainlabel chain, int verbose,
1475 iptc_handle_t *handle)
1476{
1477 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001478 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001479
1480 if (verbose)
1481 fprintf(stdout, "Flushing chain `%s'\n", chain);
1482 return iptc_flush_entries(chain, handle);
1483}
Marc Bouchere6869a82000-03-20 06:03:29 +00001484
1485static int
1486zero_entries(const ipt_chainlabel chain, int verbose,
1487 iptc_handle_t *handle)
1488{
1489 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001490 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001491
Marc Bouchere6869a82000-03-20 06:03:29 +00001492 if (verbose)
1493 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1494 return iptc_zero_entries(chain, handle);
1495}
1496
Harald Welteae1ff9f2000-12-01 14:26:20 +00001497int
Marc Bouchere6869a82000-03-20 06:03:29 +00001498delete_chain(const ipt_chainlabel chain, int verbose,
1499 iptc_handle_t *handle)
1500{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001501 if (!chain)
1502 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001503
1504 if (verbose)
1505 fprintf(stdout, "Deleting chain `%s'\n", chain);
1506 return iptc_delete_chain(chain, handle);
1507}
1508
1509static int
1510list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1511 int expanded, int linenumbers, iptc_handle_t *handle)
1512{
1513 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001514 unsigned int format;
1515 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001516
1517 format = FMT_OPTIONS;
1518 if (!verbose)
1519 format |= FMT_NOCOUNTS;
1520 else
1521 format |= FMT_VIA;
1522
1523 if (numeric)
1524 format |= FMT_NUMERIC;
1525
1526 if (!expanded)
1527 format |= FMT_KILOMEGAGIGA;
1528
1529 if (linenumbers)
1530 format |= FMT_LINENUMBERS;
1531
Rusty Russell9e1d2142000-04-23 09:11:12 +00001532 for (this = iptc_first_chain(handle);
1533 this;
1534 this = iptc_next_chain(handle)) {
1535 const struct ipt_entry *i;
1536 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001537
Marc Bouchere6869a82000-03-20 06:03:29 +00001538 if (chain && strcmp(chain, this) != 0)
1539 continue;
1540
1541 if (found) printf("\n");
1542
1543 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001544 i = iptc_first_rule(this, handle);
1545
1546 num = 0;
1547 while (i) {
1548 print_firewall(i,
1549 iptc_get_target(i, handle),
1550 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001551 format,
1552 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001553 i = iptc_next_rule(i, handle);
1554 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001555 found = 1;
1556 }
1557
1558 errno = ENOENT;
1559 return found;
1560}
1561
Harald Welte82dd2ec2000-12-19 05:18:15 +00001562static char *get_modprobe(void)
1563{
1564 int procfile;
1565 char *ret;
1566
1567 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1568 if (procfile < 0)
1569 return NULL;
1570
1571 ret = malloc(1024);
1572 if (ret) {
1573 switch (read(procfile, ret, 1024)) {
1574 case -1: goto fail;
1575 case 1024: goto fail; /* Partial read. Wierd */
1576 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001577 if (ret[strlen(ret)-1]=='\n')
1578 ret[strlen(ret)-1]=0;
1579 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001580 return ret;
1581 }
1582 fail:
1583 free(ret);
1584 close(procfile);
1585 return NULL;
1586}
1587
Harald Welte58918652001-06-16 18:25:25 +00001588int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001589{
1590 char *buf = NULL;
1591 char *argv[3];
1592
1593 /* If they don't explicitly set it, read out of kernel */
1594 if (!modprobe) {
1595 buf = get_modprobe();
1596 if (!buf)
1597 return -1;
1598 modprobe = buf;
1599 }
1600
1601 switch (fork()) {
1602 case 0:
1603 argv[0] = (char *)modprobe;
1604 argv[1] = (char *)modname;
1605 argv[2] = NULL;
1606 execv(argv[0], argv);
1607
1608 /* not usually reached */
1609 exit(0);
1610 case -1:
1611 return -1;
1612
1613 default: /* parent */
1614 wait(NULL);
1615 }
1616
1617 free(buf);
1618 return 0;
1619}
1620
Marc Bouchere6869a82000-03-20 06:03:29 +00001621static struct ipt_entry *
1622generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001623 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001624 struct ipt_entry_target *target)
1625{
1626 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001627 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001628 struct ipt_entry *e;
1629
1630 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001631 for (matchp = matches; matchp; matchp = matchp->next)
1632 size += matchp->match->m->u.match_size;
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;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001640 for (matchp = matches; matchp; matchp = matchp->next) {
1641 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1642 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001643 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001644 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001645
1646 return e;
1647}
1648
Martin Josefsson78cafda2004-02-02 20:01:18 +00001649void clear_rule_matches(struct iptables_rule_match **matches)
1650{
1651 struct iptables_rule_match *matchp, *tmp;
1652
1653 for (matchp = *matches; matchp;) {
1654 tmp = matchp->next;
1655 free(matchp);
1656 matchp = tmp;
1657 }
1658
1659 *matches = NULL;
1660}
1661
Marc Bouchere6869a82000-03-20 06:03:29 +00001662int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1663{
1664 struct ipt_entry fw, *e = NULL;
1665 int invert = 0;
1666 unsigned int nsaddrs = 0, ndaddrs = 0;
1667 struct in_addr *saddrs = NULL, *daddrs = NULL;
1668
1669 int c, verbose = 0;
1670 const char *chain = NULL;
1671 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1672 const char *policy = NULL, *newname = NULL;
1673 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001674 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001675 int ret = 1;
1676 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001677 struct iptables_rule_match *matches = NULL;
1678 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001679 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001680 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 const char *jumpto = "";
1682 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001683 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001684 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001685
1686 memset(&fw, 0, sizeof(fw));
1687
Sven Kochfb1279a2001-02-27 12:25:12 +00001688 opts = original_opts;
1689 global_option_offset = 0;
1690
Harald Welteae1ff9f2000-12-01 14:26:20 +00001691 /* re-set optind to 0 in case do_command gets called
1692 * a second time */
1693 optind = 0;
1694
1695 /* clear mflags in case do_command gets called a second time
1696 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001697 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001698 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001699
1700 for (t = iptables_targets; t; t = t->next) {
1701 t->tflags = 0;
1702 t->used = 0;
1703 }
1704
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 /* Suppress error messages: we may add new options if we
1706 demand-load a protocol. */
1707 opterr = 0;
1708
1709 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001710 "-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 +00001711 opts, NULL)) != -1) {
1712 switch (c) {
1713 /*
1714 * Command selection
1715 */
1716 case 'A':
1717 add_command(&command, CMD_APPEND, CMD_NONE,
1718 invert);
1719 chain = optarg;
1720 break;
1721
1722 case 'D':
1723 add_command(&command, CMD_DELETE, CMD_NONE,
1724 invert);
1725 chain = optarg;
1726 if (optind < argc && argv[optind][0] != '-'
1727 && argv[optind][0] != '!') {
1728 rulenum = parse_rulenumber(argv[optind++]);
1729 command = CMD_DELETE_NUM;
1730 }
1731 break;
1732
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 case 'R':
1734 add_command(&command, CMD_REPLACE, CMD_NONE,
1735 invert);
1736 chain = optarg;
1737 if (optind < argc && argv[optind][0] != '-'
1738 && argv[optind][0] != '!')
1739 rulenum = parse_rulenumber(argv[optind++]);
1740 else
1741 exit_error(PARAMETER_PROBLEM,
1742 "-%c requires a rule number",
1743 cmd2char(CMD_REPLACE));
1744 break;
1745
1746 case 'I':
1747 add_command(&command, CMD_INSERT, CMD_NONE,
1748 invert);
1749 chain = optarg;
1750 if (optind < argc && argv[optind][0] != '-'
1751 && argv[optind][0] != '!')
1752 rulenum = parse_rulenumber(argv[optind++]);
1753 else rulenum = 1;
1754 break;
1755
1756 case 'L':
1757 add_command(&command, CMD_LIST, CMD_ZERO,
1758 invert);
1759 if (optarg) chain = optarg;
1760 else if (optind < argc && argv[optind][0] != '-'
1761 && argv[optind][0] != '!')
1762 chain = argv[optind++];
1763 break;
1764
1765 case 'F':
1766 add_command(&command, CMD_FLUSH, CMD_NONE,
1767 invert);
1768 if (optarg) chain = optarg;
1769 else if (optind < argc && argv[optind][0] != '-'
1770 && argv[optind][0] != '!')
1771 chain = argv[optind++];
1772 break;
1773
1774 case 'Z':
1775 add_command(&command, CMD_ZERO, CMD_LIST,
1776 invert);
1777 if (optarg) chain = optarg;
1778 else if (optind < argc && argv[optind][0] != '-'
1779 && argv[optind][0] != '!')
1780 chain = argv[optind++];
1781 break;
1782
1783 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001784 if (optarg && *optarg == '-')
1785 exit_error(PARAMETER_PROBLEM,
1786 "chain name not allowed to start "
1787 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001788 if (find_target(optarg, TRY_LOAD))
1789 exit_error(PARAMETER_PROBLEM,
1790 "chain name may not clash "
1791 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001792 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1793 invert);
1794 chain = optarg;
1795 break;
1796
1797 case 'X':
1798 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1799 invert);
1800 if (optarg) chain = optarg;
1801 else if (optind < argc && argv[optind][0] != '-'
1802 && argv[optind][0] != '!')
1803 chain = argv[optind++];
1804 break;
1805
1806 case 'E':
1807 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1808 invert);
1809 chain = optarg;
1810 if (optind < argc && argv[optind][0] != '-'
1811 && argv[optind][0] != '!')
1812 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001813 else
1814 exit_error(PARAMETER_PROBLEM,
1815 "-%c requires old-chain-name and "
1816 "new-chain-name",
1817 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001818 break;
1819
1820 case 'P':
1821 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1822 invert);
1823 chain = optarg;
1824 if (optind < argc && argv[optind][0] != '-'
1825 && argv[optind][0] != '!')
1826 policy = argv[optind++];
1827 else
1828 exit_error(PARAMETER_PROBLEM,
1829 "-%c requires a chain and a policy",
1830 cmd2char(CMD_SET_POLICY));
1831 break;
1832
1833 case 'h':
1834 if (!optarg)
1835 optarg = argv[optind];
1836
Rusty Russell2e0a3212000-04-19 11:23:18 +00001837 /* iptables -p icmp -h */
1838 if (!iptables_matches && protocol)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001839 find_match(protocol, TRY_LOAD, NULL);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001840
Marc Bouchere6869a82000-03-20 06:03:29 +00001841 exit_printhelp();
1842
1843 /*
1844 * Option selection
1845 */
1846 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001847 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001848 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1849 invert);
1850
1851 /* Canonicalize into lower case */
1852 for (protocol = argv[optind-1]; *protocol; protocol++)
1853 *protocol = tolower(*protocol);
1854
1855 protocol = argv[optind-1];
1856 fw.ip.proto = parse_protocol(protocol);
1857
1858 if (fw.ip.proto == 0
1859 && (fw.ip.invflags & IPT_INV_PROTO))
1860 exit_error(PARAMETER_PROBLEM,
1861 "rule would never match protocol");
1862 fw.nfcache |= NFC_IP_PROTO;
1863 break;
1864
1865 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001866 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001867 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1868 invert);
1869 shostnetworkmask = argv[optind-1];
1870 fw.nfcache |= NFC_IP_SRC;
1871 break;
1872
1873 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001874 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001875 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1876 invert);
1877 dhostnetworkmask = argv[optind-1];
1878 fw.nfcache |= NFC_IP_DST;
1879 break;
1880
1881 case 'j':
1882 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1883 invert);
1884 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001885 /* TRY_LOAD (may be chain name) */
1886 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001887
1888 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001889 size_t size;
1890
Rusty Russell73f72f52000-07-03 10:17:57 +00001891 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1892 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001893
Rusty Russell2e0a3212000-04-19 11:23:18 +00001894 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001895 target->t->u.target_size = size;
1896 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001897 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001898 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 }
1900 break;
1901
1902
1903 case 'i':
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_VIANAMEIN, &fw.ip.invflags,
1906 invert);
1907 parse_interface(argv[optind-1],
1908 fw.ip.iniface,
1909 fw.ip.iniface_mask);
1910 fw.nfcache |= NFC_IP_IF_IN;
1911 break;
1912
1913 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001914 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001915 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1916 invert);
1917 parse_interface(argv[optind-1],
1918 fw.ip.outiface,
1919 fw.ip.outiface_mask);
1920 fw.nfcache |= NFC_IP_IF_OUT;
1921 break;
1922
1923 case 'f':
1924 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1925 invert);
1926 fw.ip.flags |= IPT_F_FRAG;
1927 fw.nfcache |= NFC_IP_FRAG;
1928 break;
1929
1930 case 'v':
1931 if (!verbose)
1932 set_option(&options, OPT_VERBOSE,
1933 &fw.ip.invflags, invert);
1934 verbose++;
1935 break;
1936
Rusty Russell52a51492000-05-02 16:44:29 +00001937 case 'm': {
1938 size_t size;
1939
Marc Bouchere6869a82000-03-20 06:03:29 +00001940 if (invert)
1941 exit_error(PARAMETER_PROBLEM,
1942 "unexpected ! flag before --match");
1943
Martin Josefsson78cafda2004-02-02 20:01:18 +00001944 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001945 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1946 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001947 m->m = fw_calloc(1, size);
1948 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001949 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001950 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001951 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001952 }
1953 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001954
1955 case 'n':
1956 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1957 invert);
1958 break;
1959
1960 case 't':
1961 if (invert)
1962 exit_error(PARAMETER_PROBLEM,
1963 "unexpected ! flag before --table");
1964 *table = argv[optind-1];
1965 break;
1966
1967 case 'x':
1968 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1969 invert);
1970 break;
1971
1972 case 'V':
1973 if (invert)
1974 printf("Not %s ;-)\n", program_version);
1975 else
1976 printf("%s v%s\n",
1977 program_name, program_version);
1978 exit(0);
1979
1980 case '0':
1981 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1982 invert);
1983 break;
1984
Harald Welte82dd2ec2000-12-19 05:18:15 +00001985 case 'M':
1986 modprobe = optarg;
1987 break;
1988
Harald Welteccd49e52001-01-23 22:54:34 +00001989 case 'c':
1990
1991 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1992 invert);
1993 pcnt = optarg;
1994 if (optind < argc && argv[optind][0] != '-'
1995 && argv[optind][0] != '!')
1996 bcnt = argv[optind++];
1997 else
1998 exit_error(PARAMETER_PROBLEM,
1999 "-%c requires packet and byte counter",
2000 opt2char(OPT_COUNTERS));
2001
2002 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2003 exit_error(PARAMETER_PROBLEM,
2004 "-%c packet counter not numeric",
2005 opt2char(OPT_COUNTERS));
2006
2007 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2008 exit_error(PARAMETER_PROBLEM,
2009 "-%c byte counter not numeric",
2010 opt2char(OPT_COUNTERS));
2011
2012 break;
2013
2014
Marc Bouchere6869a82000-03-20 06:03:29 +00002015 case 1: /* non option */
2016 if (optarg[0] == '!' && optarg[1] == '\0') {
2017 if (invert)
2018 exit_error(PARAMETER_PROBLEM,
2019 "multiple consecutive ! not"
2020 " allowed");
2021 invert = TRUE;
2022 optarg[0] = '\0';
2023 continue;
2024 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002025 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 exit_tryhelp(2);
2027
2028 default:
2029 /* FIXME: This scheme doesn't allow two of the same
2030 matches --RR */
2031 if (!target
2032 || !(target->parse(c - target->option_offset,
2033 argv, invert,
2034 &target->tflags,
2035 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002036 for (matchp = matches; matchp; matchp = matchp->next) {
2037 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002039 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 &fw,
2041 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002042 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 break;
2044 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002045 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002046
2047 /* If you listen carefully, you can
2048 actually hear this code suck. */
2049
2050 /* some explanations (after four different bugs
2051 * in 3 different releases): If we encountere a
2052 * parameter, that has not been parsed yet,
2053 * it's not an option of an explicitly loaded
2054 * match or a target. However, we support
2055 * implicit loading of the protocol match
2056 * extension. '-p tcp' means 'l4 proto 6' and
2057 * at the same time 'load tcp protocol match on
2058 * demand if we specify --dport'.
2059 *
2060 * To make this work, we need to make sure:
2061 * - the parameter has not been parsed by
2062 * a match (m above)
2063 * - a protocol has been specified
2064 * - the protocol extension has not been
2065 * loaded yet, or is loaded and unused
2066 * [think of iptables-restore!]
2067 * - the protocol extension can be successively
2068 * loaded
2069 */
2070 if (m == NULL
2071 && protocol
2072 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002073 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002074 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002075 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002076 && (proto_used == 0))
2077 )
2078 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002079 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002080 /* Try loading protocol */
2081 size_t size;
2082
2083 proto_used = 1;
2084
2085 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2086 + m->size;
2087
2088 m->m = fw_calloc(1, size);
2089 m->m->u.match_size = size;
2090 strcpy(m->m->u.user.name, m->name);
2091 m->init(m->m, &fw.nfcache);
2092
2093 opts = merge_options(opts,
2094 m->extra_opts, &m->option_offset);
2095
2096 optind--;
2097 continue;
2098 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002099 if (!m)
2100 exit_error(PARAMETER_PROBLEM,
2101 "Unknown arg `%s'",
2102 argv[optind-1]);
2103 }
2104 }
2105 invert = FALSE;
2106 }
2107
Martin Josefsson78cafda2004-02-02 20:01:18 +00002108 for (matchp = matches; matchp; matchp = matchp->next)
2109 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002110
Marc Bouchere6869a82000-03-20 06:03:29 +00002111 if (target)
2112 target->final_check(target->tflags);
2113
2114 /* Fix me: must put inverse options checking here --MN */
2115
2116 if (optind < argc)
2117 exit_error(PARAMETER_PROBLEM,
2118 "unknown arguments found on commandline");
2119 if (!command)
2120 exit_error(PARAMETER_PROBLEM, "no command specified");
2121 if (invert)
2122 exit_error(PARAMETER_PROBLEM,
2123 "nothing appropriate following !");
2124
Harald Welte6336bfd2002-05-07 14:41:43 +00002125 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 if (!(options & OPT_DESTINATION))
2127 dhostnetworkmask = "0.0.0.0/0";
2128 if (!(options & OPT_SOURCE))
2129 shostnetworkmask = "0.0.0.0/0";
2130 }
2131
2132 if (shostnetworkmask)
2133 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2134 &(fw.ip.smsk), &nsaddrs);
2135
2136 if (dhostnetworkmask)
2137 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2138 &(fw.ip.dmsk), &ndaddrs);
2139
2140 if ((nsaddrs > 1 || ndaddrs > 1) &&
2141 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2142 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2143 " source or destination IP addresses");
2144
Marc Bouchere6869a82000-03-20 06:03:29 +00002145 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2146 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2147 "specify a unique address");
2148
2149 generic_opt_check(command, options);
2150
2151 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2152 exit_error(PARAMETER_PROBLEM,
2153 "chain name `%s' too long (must be under %i chars)",
2154 chain, IPT_FUNCTION_MAXNAMELEN);
2155
Harald Welteae1ff9f2000-12-01 14:26:20 +00002156 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002157 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002158 *handle = iptc_init(*table);
2159
Harald Welte82dd2ec2000-12-19 05:18:15 +00002160 if (!*handle) {
2161 /* try to insmod the module if iptc_init failed */
2162 iptables_insmod("ip_tables", modprobe);
2163 *handle = iptc_init(*table);
2164 }
2165
Marc Bouchere6869a82000-03-20 06:03:29 +00002166 if (!*handle)
2167 exit_error(VERSION_PROBLEM,
2168 "can't initialize iptables table `%s': %s",
2169 *table, iptc_strerror(errno));
2170
Harald Welte6336bfd2002-05-07 14:41:43 +00002171 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002172 || command == CMD_DELETE
2173 || command == CMD_INSERT
2174 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002175 if (strcmp(chain, "PREROUTING") == 0
2176 || strcmp(chain, "INPUT") == 0) {
2177 /* -o not valid with incoming packets. */
2178 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002179 exit_error(PARAMETER_PROBLEM,
2180 "Can't use -%c with %s\n",
2181 opt2char(OPT_VIANAMEOUT),
2182 chain);
2183 }
2184
Rusty Russella4860fd2000-06-17 16:13:02 +00002185 if (strcmp(chain, "POSTROUTING") == 0
2186 || strcmp(chain, "OUTPUT") == 0) {
2187 /* -i not valid with outgoing packets */
2188 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002189 exit_error(PARAMETER_PROBLEM,
2190 "Can't use -%c with %s\n",
2191 opt2char(OPT_VIANAMEIN),
2192 chain);
2193 }
2194
2195 if (target && iptc_is_chain(jumpto, *handle)) {
2196 printf("Warning: using chain %s, not extension\n",
2197 jumpto);
2198
2199 target = NULL;
2200 }
2201
2202 /* If they didn't specify a target, or it's a chain
2203 name, use standard. */
2204 if (!target
2205 && (strlen(jumpto) == 0
2206 || iptc_is_chain(jumpto, *handle))) {
2207 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002208
Rusty Russell52a51492000-05-02 16:44:29 +00002209 target = find_target(IPT_STANDARD_TARGET,
2210 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002211
2212 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002213 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002214 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002215 target->t->u.target_size = size;
2216 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002217 target->init(target->t, &fw.nfcache);
2218 }
2219
Rusty Russell7e53bf92000-03-20 07:03:28 +00002220 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002221 /* it is no chain, and we can't load a plugin.
2222 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002223 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002224 * chain. */
2225 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002226 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002227 e = generate_entry(&fw, matches, target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002228 }
2229 }
2230
2231 switch (command) {
2232 case CMD_APPEND:
2233 ret = append_entry(chain, e,
2234 nsaddrs, saddrs, ndaddrs, daddrs,
2235 options&OPT_VERBOSE,
2236 handle);
2237 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002238 case CMD_DELETE:
2239 ret = delete_entry(chain, e,
2240 nsaddrs, saddrs, ndaddrs, daddrs,
2241 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002242 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002243 break;
2244 case CMD_DELETE_NUM:
2245 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2246 break;
2247 case CMD_REPLACE:
2248 ret = replace_entry(chain, e, rulenum - 1,
2249 saddrs, daddrs, options&OPT_VERBOSE,
2250 handle);
2251 break;
2252 case CMD_INSERT:
2253 ret = insert_entry(chain, e, rulenum - 1,
2254 nsaddrs, saddrs, ndaddrs, daddrs,
2255 options&OPT_VERBOSE,
2256 handle);
2257 break;
2258 case CMD_LIST:
2259 ret = list_entries(chain,
2260 options&OPT_VERBOSE,
2261 options&OPT_NUMERIC,
2262 options&OPT_EXPANDED,
2263 options&OPT_LINENUMBERS,
2264 handle);
2265 break;
2266 case CMD_FLUSH:
2267 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2268 break;
2269 case CMD_ZERO:
2270 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2271 break;
2272 case CMD_LIST|CMD_ZERO:
2273 ret = list_entries(chain,
2274 options&OPT_VERBOSE,
2275 options&OPT_NUMERIC,
2276 options&OPT_EXPANDED,
2277 options&OPT_LINENUMBERS,
2278 handle);
2279 if (ret)
2280 ret = zero_entries(chain,
2281 options&OPT_VERBOSE, handle);
2282 break;
2283 case CMD_NEW_CHAIN:
2284 ret = iptc_create_chain(chain, handle);
2285 break;
2286 case CMD_DELETE_CHAIN:
2287 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2288 break;
2289 case CMD_RENAME_CHAIN:
2290 ret = iptc_rename_chain(chain, newname, handle);
2291 break;
2292 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002293 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002294 break;
2295 default:
2296 /* We should never reach this... */
2297 exit_tryhelp(2);
2298 }
2299
2300 if (verbose > 1)
2301 dump_entries(*handle);
2302
Martin Josefsson78cafda2004-02-02 20:01:18 +00002303 clear_rule_matches(&matches);
2304
Marc Bouchere6869a82000-03-20 06:03:29 +00002305 return ret;
2306}