blob: 2c6282a59c0fa3505bdaac617b9b5bb6e779139d [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 *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000664find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
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. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000681 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000682
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
Martin Josefsson78cafda2004-02-02 20:01:18 +0000705 if (ptr && matches) {
706 struct iptables_rule_match **i;
707 struct iptables_rule_match *newentry;
708
709 newentry = fw_malloc(sizeof(struct iptables_rule_match));
710
711 for (i = matches; *i; i = &(*i)->next);
712 newentry->match = ptr;
713 newentry->next = NULL;
714 *i = newentry;
715 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000716
Marc Bouchere6869a82000-03-20 06:03:29 +0000717 return ptr;
718}
719
Rusty Russell28381a42000-05-10 00:19:50 +0000720/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
721static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000722find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000723{
Harald Welteed498492001-07-23 01:24:22 +0000724 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000725
Harald Welte0b0013a2002-02-18 16:15:31 +0000726 if (string_to_number(pname, 0, 255, &proto) != -1) {
727 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000728
Harald Welte0b0013a2002-02-18 16:15:31 +0000729 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000730 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000731 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000732 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000733
734 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000735}
736
Marc Boucherb93c7982001-12-06 14:50:19 +0000737u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000738parse_protocol(const char *s)
739{
Harald Welteed498492001-07-23 01:24:22 +0000740 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000741
Harald Welteed498492001-07-23 01:24:22 +0000742 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000743 struct protoent *pent;
744
745 if ((pent = getprotobyname(s)))
746 proto = pent->p_proto;
747 else {
748 unsigned int i;
749 for (i = 0;
750 i < sizeof(chain_protos)/sizeof(struct pprot);
751 i++) {
752 if (strcmp(s, chain_protos[i].name) == 0) {
753 proto = chain_protos[i].num;
754 break;
755 }
756 }
757 if (i == sizeof(chain_protos)/sizeof(struct pprot))
758 exit_error(PARAMETER_PROBLEM,
759 "unknown protocol `%s' specified",
760 s);
761 }
762 }
763
764 return (u_int16_t)proto;
765}
766
767static void
768parse_interface(const char *arg, char *vianame, unsigned char *mask)
769{
770 int vialen = strlen(arg);
771 unsigned int i;
772
773 memset(mask, 0, IFNAMSIZ);
774 memset(vianame, 0, IFNAMSIZ);
775
776 if (vialen + 1 > IFNAMSIZ)
777 exit_error(PARAMETER_PROBLEM,
778 "interface name `%s' must be shorter than IFNAMSIZ"
779 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000780
Marc Bouchere6869a82000-03-20 06:03:29 +0000781 strcpy(vianame, arg);
782 if (vialen == 0)
783 memset(mask, 0, IFNAMSIZ);
784 else if (vianame[vialen - 1] == '+') {
785 memset(mask, 0xFF, vialen - 1);
786 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000787 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000788 } else {
789 /* Include nul-terminator in match */
790 memset(mask, 0xFF, vialen + 1);
791 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000792 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000793 if (!isalnum(vianame[i])
794 && vianame[i] != '_'
795 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000796 printf("Warning: wierd character in interface"
797 " `%s' (No aliases, :, ! or *).\n",
798 vianame);
799 break;
800 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000801 }
802 }
803}
804
805/* Can't be zero. */
806static int
807parse_rulenumber(const char *rule)
808{
Harald Welteed498492001-07-23 01:24:22 +0000809 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000810
Harald Welteed498492001-07-23 01:24:22 +0000811 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000812 exit_error(PARAMETER_PROBLEM,
813 "Invalid rule number `%s'", rule);
814
815 return rulenum;
816}
817
818static const char *
819parse_target(const char *targetname)
820{
821 const char *ptr;
822
823 if (strlen(targetname) < 1)
824 exit_error(PARAMETER_PROBLEM,
825 "Invalid target name (too short)");
826
827 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
828 exit_error(PARAMETER_PROBLEM,
829 "Invalid target name `%s' (%i chars max)",
830 targetname, sizeof(ipt_chainlabel)-1);
831
832 for (ptr = targetname; *ptr; ptr++)
833 if (isspace(*ptr))
834 exit_error(PARAMETER_PROBLEM,
835 "Invalid target name `%s'", targetname);
836 return targetname;
837}
838
839static char *
840addr_to_network(const struct in_addr *addr)
841{
842 struct netent *net;
843
844 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
845 return (char *) net->n_name;
846
847 return (char *) NULL;
848}
849
850char *
851addr_to_dotted(const struct in_addr *addrp)
852{
853 static char buf[20];
854 const unsigned char *bytep;
855
856 bytep = (const unsigned char *) &(addrp->s_addr);
857 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
858 return buf;
859}
Marc Boucherb93c7982001-12-06 14:50:19 +0000860
861char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000862addr_to_anyname(const struct in_addr *addr)
863{
864 char *name;
865
866 if ((name = addr_to_host(addr)) != NULL ||
867 (name = addr_to_network(addr)) != NULL)
868 return name;
869
870 return addr_to_dotted(addr);
871}
872
Marc Boucherb93c7982001-12-06 14:50:19 +0000873char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000874mask_to_dotted(const struct in_addr *mask)
875{
876 int i;
877 static char buf[20];
878 u_int32_t maskaddr, bits;
879
880 maskaddr = ntohl(mask->s_addr);
881
882 if (maskaddr == 0xFFFFFFFFL)
883 /* we don't want to see "/32" */
884 return "";
885
886 i = 32;
887 bits = 0xFFFFFFFEL;
888 while (--i >= 0 && maskaddr != bits)
889 bits <<= 1;
890 if (i >= 0)
891 sprintf(buf, "/%d", i);
892 else
893 /* mask was not a decent combination of 1's and 0's */
894 sprintf(buf, "/%s", addr_to_dotted(mask));
895
896 return buf;
897}
898
899int
Harald Welteed498492001-07-23 01:24:22 +0000900string_to_number(const char *s, unsigned int min, unsigned int max,
901 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000902{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000903 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000904 char *end;
905
906 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000907 errno = 0;
908 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000909 if (*end == '\0' && end != s) {
910 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000911 if (errno != ERANGE && min <= number && number <= max) {
912 *ret = number;
913 return 0;
914 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000915 }
916 return -1;
917}
918
919static void
920set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
921 int invert)
922{
923 if (*options & option)
924 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
925 opt2char(option));
926 *options |= option;
927
928 if (invert) {
929 unsigned int i;
930 for (i = 0; 1 << i != option; i++);
931
932 if (!inverse_for_options[i])
933 exit_error(PARAMETER_PROBLEM,
934 "cannot have ! before -%c",
935 opt2char(option));
936 *invflg |= inverse_for_options[i];
937 }
938}
939
940struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000941find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000942{
943 struct iptables_target *ptr;
944
945 /* Standard target? */
946 if (strcmp(name, "") == 0
947 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
948 || strcmp(name, IPTC_LABEL_DROP) == 0
949 || strcmp(name, IPTC_LABEL_QUEUE) == 0
950 || strcmp(name, IPTC_LABEL_RETURN) == 0)
951 name = "standard";
952
953 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
954 if (strcmp(name, ptr->name) == 0)
955 break;
956 }
957
Harald Welte3efb6ea2001-08-06 18:50:21 +0000958#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000959 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000960 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
961 + strlen(name)];
962 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000963 if (dlopen(path, RTLD_NOW)) {
964 /* Found library. If it didn't register itself,
965 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000966 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000967 if (!ptr)
968 exit_error(PARAMETER_PROBLEM,
969 "Couldn't load target `%s'\n",
970 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000971 } else if (tryload == LOAD_MUST_SUCCEED)
972 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000973 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000974 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000975 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000976#else
977 if (ptr && !ptr->loaded) {
978 if (tryload != DONT_LOAD)
979 ptr->loaded = 1;
980 else
981 ptr = NULL;
982 }
Marc Boucher067477b2002-03-24 15:09:31 +0000983 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
984 exit_error(PARAMETER_PROBLEM,
985 "Couldn't find target `%s'\n", name);
986 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000987#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000988
Harald Welteae1ff9f2000-12-01 14:26:20 +0000989 if (ptr)
990 ptr->used = 1;
991
Marc Bouchere6869a82000-03-20 06:03:29 +0000992 return ptr;
993}
994
995static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000996merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000997 unsigned int *option_offset)
998{
999 unsigned int num_old, num_new, i;
1000 struct option *merge;
1001
1002 for (num_old = 0; oldopts[num_old].name; num_old++);
1003 for (num_new = 0; newopts[num_new].name; num_new++);
1004
1005 global_option_offset += OPTION_OFFSET;
1006 *option_offset = global_option_offset;
1007
1008 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1009 memcpy(merge, oldopts, num_old * sizeof(struct option));
1010 for (i = 0; i < num_new; i++) {
1011 merge[num_old + i] = newopts[i];
1012 merge[num_old + i].val += *option_offset;
1013 }
1014 memset(merge + num_old + num_new, 0, sizeof(struct option));
1015
1016 return merge;
1017}
1018
1019void
1020register_match(struct iptables_match *me)
1021{
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001022 struct iptables_match **i;
1023
Marc Bouchere6869a82000-03-20 06:03:29 +00001024 if (strcmp(me->version, program_version) != 0) {
1025 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1026 program_name, me->name, me->version, program_version);
1027 exit(1);
1028 }
1029
Martin Josefsson78cafda2004-02-02 20:01:18 +00001030 if (find_match(me->name, DONT_LOAD, NULL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001031 fprintf(stderr, "%s: match `%s' already registered.\n",
1032 program_name, me->name);
1033 exit(1);
1034 }
1035
Rusty Russell73f72f52000-07-03 10:17:57 +00001036 if (me->size != IPT_ALIGN(me->size)) {
1037 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1038 program_name, me->name, me->size);
1039 exit(1);
1040 }
1041
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001042 /* Append to list. */
1043 for (i = &iptables_matches; *i; i = &(*i)->next);
1044 me->next = NULL;
1045 *i = me;
1046
Marc Bouchere6869a82000-03-20 06:03:29 +00001047 me->m = NULL;
1048 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001049}
1050
1051void
1052register_target(struct iptables_target *me)
1053{
1054 if (strcmp(me->version, program_version) != 0) {
1055 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1056 program_name, me->name, me->version, program_version);
1057 exit(1);
1058 }
1059
Rusty Russell52a51492000-05-02 16:44:29 +00001060 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001061 fprintf(stderr, "%s: target `%s' already registered.\n",
1062 program_name, me->name);
1063 exit(1);
1064 }
1065
Rusty Russell73f72f52000-07-03 10:17:57 +00001066 if (me->size != IPT_ALIGN(me->size)) {
1067 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1068 program_name, me->name, me->size);
1069 exit(1);
1070 }
1071
Marc Bouchere6869a82000-03-20 06:03:29 +00001072 /* Prepend to list. */
1073 me->next = iptables_targets;
1074 iptables_targets = me;
1075 me->t = NULL;
1076 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001077}
1078
1079static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001080print_num(u_int64_t number, unsigned int format)
1081{
1082 if (format & FMT_KILOMEGAGIGA) {
1083 if (number > 99999) {
1084 number = (number + 500) / 1000;
1085 if (number > 9999) {
1086 number = (number + 500) / 1000;
1087 if (number > 9999) {
1088 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001089 if (number > 9999) {
1090 number = (number + 500) / 1000;
1091 printf(FMT("%4lluT ","%lluT "), number);
1092 }
1093 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001094 }
1095 else printf(FMT("%4lluM ","%lluM "), number);
1096 } else
1097 printf(FMT("%4lluK ","%lluK "), number);
1098 } else
1099 printf(FMT("%5llu ","%llu "), number);
1100 } else
1101 printf(FMT("%8llu ","%llu "), number);
1102}
1103
1104
1105static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001106print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1107{
1108 struct ipt_counters counters;
1109 const char *pol = iptc_get_policy(chain, &counters, handle);
1110 printf("Chain %s", chain);
1111 if (pol) {
1112 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001113 if (!(format & FMT_NOCOUNTS)) {
1114 fputc(' ', stdout);
1115 print_num(counters.pcnt, (format|FMT_NOTABLE));
1116 fputs("packets, ", stdout);
1117 print_num(counters.bcnt, (format|FMT_NOTABLE));
1118 fputs("bytes", stdout);
1119 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001120 printf(")\n");
1121 } else {
1122 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001123 if (!iptc_get_references(&refs, chain, handle))
1124 printf(" (ERROR obtaining refs)\n");
1125 else
1126 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001127 }
1128
1129 if (format & FMT_LINENUMBERS)
1130 printf(FMT("%-4s ", "%s "), "num");
1131 if (!(format & FMT_NOCOUNTS)) {
1132 if (format & FMT_KILOMEGAGIGA) {
1133 printf(FMT("%5s ","%s "), "pkts");
1134 printf(FMT("%5s ","%s "), "bytes");
1135 } else {
1136 printf(FMT("%8s ","%s "), "pkts");
1137 printf(FMT("%10s ","%s "), "bytes");
1138 }
1139 }
1140 if (!(format & FMT_NOTARGET))
1141 printf(FMT("%-9s ","%s "), "target");
1142 fputs(" prot ", stdout);
1143 if (format & FMT_OPTIONS)
1144 fputs("opt", stdout);
1145 if (format & FMT_VIA) {
1146 printf(FMT(" %-6s ","%s "), "in");
1147 printf(FMT("%-6s ","%s "), "out");
1148 }
1149 printf(FMT(" %-19s ","%s "), "source");
1150 printf(FMT(" %-19s "," %s "), "destination");
1151 printf("\n");
1152}
1153
Marc Bouchere6869a82000-03-20 06:03:29 +00001154
1155static int
1156print_match(const struct ipt_entry_match *m,
1157 const struct ipt_ip *ip,
1158 int numeric)
1159{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001160 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001161
1162 if (match) {
1163 if (match->print)
1164 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001165 else
Rusty Russellb039b022000-09-01 06:04:05 +00001166 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001167 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001168 if (m->u.user.name[0])
1169 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001170 }
1171 /* Don't stop iterating. */
1172 return 0;
1173}
1174
1175/* e is called `fw' here for hysterical raisins */
1176static void
1177print_firewall(const struct ipt_entry *fw,
1178 const char *targname,
1179 unsigned int num,
1180 unsigned int format,
1181 const iptc_handle_t handle)
1182{
1183 struct iptables_target *target = NULL;
1184 const struct ipt_entry_target *t;
1185 u_int8_t flags;
1186 char buf[BUFSIZ];
1187
Marc Bouchere6869a82000-03-20 06:03:29 +00001188 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001189 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 else
Rusty Russell52a51492000-05-02 16:44:29 +00001191 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001192
1193 t = ipt_get_target((struct ipt_entry *)fw);
1194 flags = fw->ip.flags;
1195
1196 if (format & FMT_LINENUMBERS)
1197 printf(FMT("%-4u ", "%u "), num+1);
1198
1199 if (!(format & FMT_NOCOUNTS)) {
1200 print_num(fw->counters.pcnt, format);
1201 print_num(fw->counters.bcnt, format);
1202 }
1203
1204 if (!(format & FMT_NOTARGET))
1205 printf(FMT("%-9s ", "%s "), targname);
1206
1207 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1208 {
Rusty Russell28381a42000-05-10 00:19:50 +00001209 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001210 if (pname)
1211 printf(FMT("%-5s", "%s "), pname);
1212 else
1213 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1214 }
1215
1216 if (format & FMT_OPTIONS) {
1217 if (format & FMT_NOTABLE)
1218 fputs("opt ", stdout);
1219 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1220 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1221 fputc(' ', stdout);
1222 }
1223
1224 if (format & FMT_VIA) {
1225 char iface[IFNAMSIZ+2];
1226
1227 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1228 iface[0] = '!';
1229 iface[1] = '\0';
1230 }
1231 else iface[0] = '\0';
1232
1233 if (fw->ip.iniface[0] != '\0') {
1234 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 }
1236 else if (format & FMT_NUMERIC) strcat(iface, "*");
1237 else strcat(iface, "any");
1238 printf(FMT(" %-6s ","in %s "), iface);
1239
1240 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1241 iface[0] = '!';
1242 iface[1] = '\0';
1243 }
1244 else iface[0] = '\0';
1245
1246 if (fw->ip.outiface[0] != '\0') {
1247 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 }
1249 else if (format & FMT_NUMERIC) strcat(iface, "*");
1250 else strcat(iface, "any");
1251 printf(FMT("%-6s ","out %s "), iface);
1252 }
1253
1254 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1255 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1256 printf(FMT("%-19s ","%s "), "anywhere");
1257 else {
1258 if (format & FMT_NUMERIC)
1259 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1260 else
1261 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1262 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1263 printf(FMT("%-19s ","%s "), buf);
1264 }
1265
1266 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1267 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001268 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001269 else {
1270 if (format & FMT_NUMERIC)
1271 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1272 else
1273 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1274 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001275 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001276 }
1277
1278 if (format & FMT_NOTABLE)
1279 fputs(" ", stdout);
1280
1281 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1282
1283 if (target) {
1284 if (target->print)
1285 /* Print the target information. */
1286 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001287 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001289 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001290
1291 if (!(format & FMT_NONEWLINE))
1292 fputc('\n', stdout);
1293}
1294
1295static void
1296print_firewall_line(const struct ipt_entry *fw,
1297 const iptc_handle_t h)
1298{
1299 struct ipt_entry_target *t;
1300
1301 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001302 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001303}
1304
1305static int
1306append_entry(const ipt_chainlabel chain,
1307 struct ipt_entry *fw,
1308 unsigned int nsaddrs,
1309 const struct in_addr saddrs[],
1310 unsigned int ndaddrs,
1311 const struct in_addr daddrs[],
1312 int verbose,
1313 iptc_handle_t *handle)
1314{
1315 unsigned int i, j;
1316 int ret = 1;
1317
1318 for (i = 0; i < nsaddrs; i++) {
1319 fw->ip.src.s_addr = saddrs[i].s_addr;
1320 for (j = 0; j < ndaddrs; j++) {
1321 fw->ip.dst.s_addr = daddrs[j].s_addr;
1322 if (verbose)
1323 print_firewall_line(fw, *handle);
1324 ret &= iptc_append_entry(chain, fw, handle);
1325 }
1326 }
1327
1328 return ret;
1329}
1330
1331static int
1332replace_entry(const ipt_chainlabel chain,
1333 struct ipt_entry *fw,
1334 unsigned int rulenum,
1335 const struct in_addr *saddr,
1336 const struct in_addr *daddr,
1337 int verbose,
1338 iptc_handle_t *handle)
1339{
1340 fw->ip.src.s_addr = saddr->s_addr;
1341 fw->ip.dst.s_addr = daddr->s_addr;
1342
1343 if (verbose)
1344 print_firewall_line(fw, *handle);
1345 return iptc_replace_entry(chain, fw, rulenum, handle);
1346}
1347
1348static int
1349insert_entry(const ipt_chainlabel chain,
1350 struct ipt_entry *fw,
1351 unsigned int rulenum,
1352 unsigned int nsaddrs,
1353 const struct in_addr saddrs[],
1354 unsigned int ndaddrs,
1355 const struct in_addr daddrs[],
1356 int verbose,
1357 iptc_handle_t *handle)
1358{
1359 unsigned int i, j;
1360 int ret = 1;
1361
1362 for (i = 0; i < nsaddrs; i++) {
1363 fw->ip.src.s_addr = saddrs[i].s_addr;
1364 for (j = 0; j < ndaddrs; j++) {
1365 fw->ip.dst.s_addr = daddrs[j].s_addr;
1366 if (verbose)
1367 print_firewall_line(fw, *handle);
1368 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1369 }
1370 }
1371
1372 return ret;
1373}
1374
Rusty Russell2e0a3212000-04-19 11:23:18 +00001375static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001376make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001377{
1378 /* Establish mask for comparison */
1379 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001380 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001381 unsigned char *mask, *mptr;
1382
1383 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001384 for (matchp = matches; matchp; matchp = matchp->next)
1385 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001386
Rusty Russell9e1d2142000-04-23 09:11:12 +00001387 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001388 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001389 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001390
Rusty Russell9e1d2142000-04-23 09:11:12 +00001391 memset(mask, 0xFF, sizeof(struct ipt_entry));
1392 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001393
Martin Josefsson78cafda2004-02-02 20:01:18 +00001394 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001395 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001396 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001397 + matchp->match->userspacesize);
1398 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001399 }
1400
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001401 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001402 IPT_ALIGN(sizeof(struct ipt_entry_target))
1403 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001404
1405 return mask;
1406}
1407
Marc Bouchere6869a82000-03-20 06:03:29 +00001408static int
1409delete_entry(const ipt_chainlabel chain,
1410 struct ipt_entry *fw,
1411 unsigned int nsaddrs,
1412 const struct in_addr saddrs[],
1413 unsigned int ndaddrs,
1414 const struct in_addr daddrs[],
1415 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001416 iptc_handle_t *handle,
1417 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001418{
1419 unsigned int i, j;
1420 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001421 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
Martin Josefsson78cafda2004-02-02 20:01:18 +00001423 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 for (i = 0; i < nsaddrs; i++) {
1425 fw->ip.src.s_addr = saddrs[i].s_addr;
1426 for (j = 0; j < ndaddrs; j++) {
1427 fw->ip.dst.s_addr = daddrs[j].s_addr;
1428 if (verbose)
1429 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001430 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001431 }
1432 }
1433 return ret;
1434}
1435
Harald Welteae1ff9f2000-12-01 14:26:20 +00001436int
Marc Bouchere6869a82000-03-20 06:03:29 +00001437for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001438 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001439{
1440 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001441 const char *chain;
1442 char *chains;
1443 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001444
Rusty Russell9e1d2142000-04-23 09:11:12 +00001445 chain = iptc_first_chain(handle);
1446 while (chain) {
1447 chaincount++;
1448 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001449 }
1450
Rusty Russell9e1d2142000-04-23 09:11:12 +00001451 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1452 i = 0;
1453 chain = iptc_first_chain(handle);
1454 while (chain) {
1455 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1456 i++;
1457 chain = iptc_next_chain(handle);
1458 }
1459
1460 for (i = 0; i < chaincount; i++) {
1461 if (!builtinstoo
1462 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1463 *handle))
1464 continue;
1465 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1466 }
1467
1468 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001469 return ret;
1470}
1471
Harald Welteae1ff9f2000-12-01 14:26:20 +00001472int
Marc Bouchere6869a82000-03-20 06:03:29 +00001473flush_entries(const ipt_chainlabel chain, int verbose,
1474 iptc_handle_t *handle)
1475{
1476 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001477 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001478
1479 if (verbose)
1480 fprintf(stdout, "Flushing chain `%s'\n", chain);
1481 return iptc_flush_entries(chain, handle);
1482}
Marc Bouchere6869a82000-03-20 06:03:29 +00001483
1484static int
1485zero_entries(const ipt_chainlabel chain, int verbose,
1486 iptc_handle_t *handle)
1487{
1488 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001489 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001490
Marc Bouchere6869a82000-03-20 06:03:29 +00001491 if (verbose)
1492 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1493 return iptc_zero_entries(chain, handle);
1494}
1495
Harald Welteae1ff9f2000-12-01 14:26:20 +00001496int
Marc Bouchere6869a82000-03-20 06:03:29 +00001497delete_chain(const ipt_chainlabel chain, int verbose,
1498 iptc_handle_t *handle)
1499{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001500 if (!chain)
1501 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001502
1503 if (verbose)
1504 fprintf(stdout, "Deleting chain `%s'\n", chain);
1505 return iptc_delete_chain(chain, handle);
1506}
1507
1508static int
1509list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1510 int expanded, int linenumbers, iptc_handle_t *handle)
1511{
1512 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001513 unsigned int format;
1514 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001515
1516 format = FMT_OPTIONS;
1517 if (!verbose)
1518 format |= FMT_NOCOUNTS;
1519 else
1520 format |= FMT_VIA;
1521
1522 if (numeric)
1523 format |= FMT_NUMERIC;
1524
1525 if (!expanded)
1526 format |= FMT_KILOMEGAGIGA;
1527
1528 if (linenumbers)
1529 format |= FMT_LINENUMBERS;
1530
Rusty Russell9e1d2142000-04-23 09:11:12 +00001531 for (this = iptc_first_chain(handle);
1532 this;
1533 this = iptc_next_chain(handle)) {
1534 const struct ipt_entry *i;
1535 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001536
Marc Bouchere6869a82000-03-20 06:03:29 +00001537 if (chain && strcmp(chain, this) != 0)
1538 continue;
1539
1540 if (found) printf("\n");
1541
1542 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001543 i = iptc_first_rule(this, handle);
1544
1545 num = 0;
1546 while (i) {
1547 print_firewall(i,
1548 iptc_get_target(i, handle),
1549 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001550 format,
1551 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001552 i = iptc_next_rule(i, handle);
1553 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001554 found = 1;
1555 }
1556
1557 errno = ENOENT;
1558 return found;
1559}
1560
Harald Welte82dd2ec2000-12-19 05:18:15 +00001561static char *get_modprobe(void)
1562{
1563 int procfile;
1564 char *ret;
1565
1566 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1567 if (procfile < 0)
1568 return NULL;
1569
1570 ret = malloc(1024);
1571 if (ret) {
1572 switch (read(procfile, ret, 1024)) {
1573 case -1: goto fail;
1574 case 1024: goto fail; /* Partial read. Wierd */
1575 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001576 if (ret[strlen(ret)-1]=='\n')
1577 ret[strlen(ret)-1]=0;
1578 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001579 return ret;
1580 }
1581 fail:
1582 free(ret);
1583 close(procfile);
1584 return NULL;
1585}
1586
Harald Welte58918652001-06-16 18:25:25 +00001587int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001588{
1589 char *buf = NULL;
1590 char *argv[3];
1591
1592 /* If they don't explicitly set it, read out of kernel */
1593 if (!modprobe) {
1594 buf = get_modprobe();
1595 if (!buf)
1596 return -1;
1597 modprobe = buf;
1598 }
1599
1600 switch (fork()) {
1601 case 0:
1602 argv[0] = (char *)modprobe;
1603 argv[1] = (char *)modname;
1604 argv[2] = NULL;
1605 execv(argv[0], argv);
1606
1607 /* not usually reached */
1608 exit(0);
1609 case -1:
1610 return -1;
1611
1612 default: /* parent */
1613 wait(NULL);
1614 }
1615
1616 free(buf);
1617 return 0;
1618}
1619
Marc Bouchere6869a82000-03-20 06:03:29 +00001620static struct ipt_entry *
1621generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001622 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001623 struct ipt_entry_target *target)
1624{
1625 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001626 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 struct ipt_entry *e;
1628
1629 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001630 for (matchp = matches; matchp; matchp = matchp->next)
1631 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001632
Rusty Russell228e98d2000-04-27 10:28:06 +00001633 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001634 *e = *fw;
1635 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001636 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001637
1638 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001639 for (matchp = matches; matchp; matchp = matchp->next) {
1640 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1641 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001642 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001643 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001644
1645 return e;
1646}
1647
Martin Josefsson78cafda2004-02-02 20:01:18 +00001648void clear_rule_matches(struct iptables_rule_match **matches)
1649{
1650 struct iptables_rule_match *matchp, *tmp;
1651
1652 for (matchp = *matches; matchp;) {
1653 tmp = matchp->next;
1654 free(matchp);
1655 matchp = tmp;
1656 }
1657
1658 *matches = NULL;
1659}
1660
Marc Bouchere6869a82000-03-20 06:03:29 +00001661int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1662{
1663 struct ipt_entry fw, *e = NULL;
1664 int invert = 0;
1665 unsigned int nsaddrs = 0, ndaddrs = 0;
1666 struct in_addr *saddrs = NULL, *daddrs = NULL;
1667
1668 int c, verbose = 0;
1669 const char *chain = NULL;
1670 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1671 const char *policy = NULL, *newname = NULL;
1672 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001673 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 int ret = 1;
1675 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001676 struct iptables_rule_match *matches = NULL;
1677 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001679 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 const char *jumpto = "";
1681 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001682 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001683 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
1685 memset(&fw, 0, sizeof(fw));
1686
Sven Kochfb1279a2001-02-27 12:25:12 +00001687 opts = original_opts;
1688 global_option_offset = 0;
1689
Harald Welteae1ff9f2000-12-01 14:26:20 +00001690 /* re-set optind to 0 in case do_command gets called
1691 * a second time */
1692 optind = 0;
1693
1694 /* clear mflags in case do_command gets called a second time
1695 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001696 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001697 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001698
1699 for (t = iptables_targets; t; t = t->next) {
1700 t->tflags = 0;
1701 t->used = 0;
1702 }
1703
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 /* Suppress error messages: we may add new options if we
1705 demand-load a protocol. */
1706 opterr = 0;
1707
1708 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001709 "-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 +00001710 opts, NULL)) != -1) {
1711 switch (c) {
1712 /*
1713 * Command selection
1714 */
1715 case 'A':
1716 add_command(&command, CMD_APPEND, CMD_NONE,
1717 invert);
1718 chain = optarg;
1719 break;
1720
1721 case 'D':
1722 add_command(&command, CMD_DELETE, CMD_NONE,
1723 invert);
1724 chain = optarg;
1725 if (optind < argc && argv[optind][0] != '-'
1726 && argv[optind][0] != '!') {
1727 rulenum = parse_rulenumber(argv[optind++]);
1728 command = CMD_DELETE_NUM;
1729 }
1730 break;
1731
Marc Bouchere6869a82000-03-20 06:03:29 +00001732 case 'R':
1733 add_command(&command, CMD_REPLACE, CMD_NONE,
1734 invert);
1735 chain = optarg;
1736 if (optind < argc && argv[optind][0] != '-'
1737 && argv[optind][0] != '!')
1738 rulenum = parse_rulenumber(argv[optind++]);
1739 else
1740 exit_error(PARAMETER_PROBLEM,
1741 "-%c requires a rule number",
1742 cmd2char(CMD_REPLACE));
1743 break;
1744
1745 case 'I':
1746 add_command(&command, CMD_INSERT, CMD_NONE,
1747 invert);
1748 chain = optarg;
1749 if (optind < argc && argv[optind][0] != '-'
1750 && argv[optind][0] != '!')
1751 rulenum = parse_rulenumber(argv[optind++]);
1752 else rulenum = 1;
1753 break;
1754
1755 case 'L':
1756 add_command(&command, CMD_LIST, CMD_ZERO,
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 'F':
1765 add_command(&command, CMD_FLUSH, CMD_NONE,
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 'Z':
1774 add_command(&command, CMD_ZERO, CMD_LIST,
1775 invert);
1776 if (optarg) chain = optarg;
1777 else if (optind < argc && argv[optind][0] != '-'
1778 && argv[optind][0] != '!')
1779 chain = argv[optind++];
1780 break;
1781
1782 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001783 if (optarg && *optarg == '-')
1784 exit_error(PARAMETER_PROBLEM,
1785 "chain name not allowed to start "
1786 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001787 if (find_target(optarg, TRY_LOAD))
1788 exit_error(PARAMETER_PROBLEM,
1789 "chain name may not clash "
1790 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001791 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1792 invert);
1793 chain = optarg;
1794 break;
1795
1796 case 'X':
1797 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1798 invert);
1799 if (optarg) chain = optarg;
1800 else if (optind < argc && argv[optind][0] != '-'
1801 && argv[optind][0] != '!')
1802 chain = argv[optind++];
1803 break;
1804
1805 case 'E':
1806 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1807 invert);
1808 chain = optarg;
1809 if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001812 else
1813 exit_error(PARAMETER_PROBLEM,
1814 "-%c requires old-chain-name and "
1815 "new-chain-name",
1816 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001817 break;
1818
1819 case 'P':
1820 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1821 invert);
1822 chain = optarg;
1823 if (optind < argc && argv[optind][0] != '-'
1824 && argv[optind][0] != '!')
1825 policy = argv[optind++];
1826 else
1827 exit_error(PARAMETER_PROBLEM,
1828 "-%c requires a chain and a policy",
1829 cmd2char(CMD_SET_POLICY));
1830 break;
1831
1832 case 'h':
1833 if (!optarg)
1834 optarg = argv[optind];
1835
Rusty Russell2e0a3212000-04-19 11:23:18 +00001836 /* iptables -p icmp -h */
1837 if (!iptables_matches && protocol)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001838 find_match(protocol, TRY_LOAD, NULL);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001839
Marc Bouchere6869a82000-03-20 06:03:29 +00001840 exit_printhelp();
1841
1842 /*
1843 * Option selection
1844 */
1845 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001846 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1848 invert);
1849
1850 /* Canonicalize into lower case */
1851 for (protocol = argv[optind-1]; *protocol; protocol++)
1852 *protocol = tolower(*protocol);
1853
1854 protocol = argv[optind-1];
1855 fw.ip.proto = parse_protocol(protocol);
1856
1857 if (fw.ip.proto == 0
1858 && (fw.ip.invflags & IPT_INV_PROTO))
1859 exit_error(PARAMETER_PROBLEM,
1860 "rule would never match protocol");
1861 fw.nfcache |= NFC_IP_PROTO;
1862 break;
1863
1864 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001865 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001866 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1867 invert);
1868 shostnetworkmask = argv[optind-1];
1869 fw.nfcache |= NFC_IP_SRC;
1870 break;
1871
1872 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001873 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001874 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1875 invert);
1876 dhostnetworkmask = argv[optind-1];
1877 fw.nfcache |= NFC_IP_DST;
1878 break;
1879
1880 case 'j':
1881 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1882 invert);
1883 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001884 /* TRY_LOAD (may be chain name) */
1885 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001886
1887 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001888 size_t size;
1889
Rusty Russell73f72f52000-07-03 10:17:57 +00001890 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1891 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001892
Rusty Russell2e0a3212000-04-19 11:23:18 +00001893 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001894 target->t->u.target_size = size;
1895 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001896 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001897 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001898 }
1899 break;
1900
1901
1902 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001903 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001904 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1905 invert);
1906 parse_interface(argv[optind-1],
1907 fw.ip.iniface,
1908 fw.ip.iniface_mask);
1909 fw.nfcache |= NFC_IP_IF_IN;
1910 break;
1911
1912 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001913 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001914 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1915 invert);
1916 parse_interface(argv[optind-1],
1917 fw.ip.outiface,
1918 fw.ip.outiface_mask);
1919 fw.nfcache |= NFC_IP_IF_OUT;
1920 break;
1921
1922 case 'f':
1923 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1924 invert);
1925 fw.ip.flags |= IPT_F_FRAG;
1926 fw.nfcache |= NFC_IP_FRAG;
1927 break;
1928
1929 case 'v':
1930 if (!verbose)
1931 set_option(&options, OPT_VERBOSE,
1932 &fw.ip.invflags, invert);
1933 verbose++;
1934 break;
1935
Rusty Russell52a51492000-05-02 16:44:29 +00001936 case 'm': {
1937 size_t size;
1938
Marc Bouchere6869a82000-03-20 06:03:29 +00001939 if (invert)
1940 exit_error(PARAMETER_PROBLEM,
1941 "unexpected ! flag before --match");
1942
Martin Josefsson78cafda2004-02-02 20:01:18 +00001943 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001944 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1945 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001946 m->m = fw_calloc(1, size);
1947 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001948 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001949 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001950 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001951 }
1952 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001953
1954 case 'n':
1955 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1956 invert);
1957 break;
1958
1959 case 't':
1960 if (invert)
1961 exit_error(PARAMETER_PROBLEM,
1962 "unexpected ! flag before --table");
1963 *table = argv[optind-1];
1964 break;
1965
1966 case 'x':
1967 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1968 invert);
1969 break;
1970
1971 case 'V':
1972 if (invert)
1973 printf("Not %s ;-)\n", program_version);
1974 else
1975 printf("%s v%s\n",
1976 program_name, program_version);
1977 exit(0);
1978
1979 case '0':
1980 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1981 invert);
1982 break;
1983
Harald Welte82dd2ec2000-12-19 05:18:15 +00001984 case 'M':
1985 modprobe = optarg;
1986 break;
1987
Harald Welteccd49e52001-01-23 22:54:34 +00001988 case 'c':
1989
1990 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1991 invert);
1992 pcnt = optarg;
1993 if (optind < argc && argv[optind][0] != '-'
1994 && argv[optind][0] != '!')
1995 bcnt = argv[optind++];
1996 else
1997 exit_error(PARAMETER_PROBLEM,
1998 "-%c requires packet and byte counter",
1999 opt2char(OPT_COUNTERS));
2000
2001 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2002 exit_error(PARAMETER_PROBLEM,
2003 "-%c packet counter not numeric",
2004 opt2char(OPT_COUNTERS));
2005
2006 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2007 exit_error(PARAMETER_PROBLEM,
2008 "-%c byte counter not numeric",
2009 opt2char(OPT_COUNTERS));
2010
2011 break;
2012
2013
Marc Bouchere6869a82000-03-20 06:03:29 +00002014 case 1: /* non option */
2015 if (optarg[0] == '!' && optarg[1] == '\0') {
2016 if (invert)
2017 exit_error(PARAMETER_PROBLEM,
2018 "multiple consecutive ! not"
2019 " allowed");
2020 invert = TRUE;
2021 optarg[0] = '\0';
2022 continue;
2023 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002024 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002025 exit_tryhelp(2);
2026
2027 default:
2028 /* FIXME: This scheme doesn't allow two of the same
2029 matches --RR */
2030 if (!target
2031 || !(target->parse(c - target->option_offset,
2032 argv, invert,
2033 &target->tflags,
2034 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002035 for (matchp = matches; matchp; matchp = matchp->next) {
2036 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002037 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002038 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 &fw,
2040 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002041 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002042 break;
2043 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002044 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002045
2046 /* If you listen carefully, you can
2047 actually hear this code suck. */
2048
2049 /* some explanations (after four different bugs
2050 * in 3 different releases): If we encountere a
2051 * parameter, that has not been parsed yet,
2052 * it's not an option of an explicitly loaded
2053 * match or a target. However, we support
2054 * implicit loading of the protocol match
2055 * extension. '-p tcp' means 'l4 proto 6' and
2056 * at the same time 'load tcp protocol match on
2057 * demand if we specify --dport'.
2058 *
2059 * To make this work, we need to make sure:
2060 * - the parameter has not been parsed by
2061 * a match (m above)
2062 * - a protocol has been specified
2063 * - the protocol extension has not been
2064 * loaded yet, or is loaded and unused
2065 * [think of iptables-restore!]
2066 * - the protocol extension can be successively
2067 * loaded
2068 */
2069 if (m == NULL
2070 && protocol
2071 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002072 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002073 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002074 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002075 && (proto_used == 0))
2076 )
2077 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002078 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002079 /* Try loading protocol */
2080 size_t size;
2081
2082 proto_used = 1;
2083
2084 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2085 + m->size;
2086
2087 m->m = fw_calloc(1, size);
2088 m->m->u.match_size = size;
2089 strcpy(m->m->u.user.name, m->name);
2090 m->init(m->m, &fw.nfcache);
2091
2092 opts = merge_options(opts,
2093 m->extra_opts, &m->option_offset);
2094
2095 optind--;
2096 continue;
2097 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002098 if (!m)
2099 exit_error(PARAMETER_PROBLEM,
2100 "Unknown arg `%s'",
2101 argv[optind-1]);
2102 }
2103 }
2104 invert = FALSE;
2105 }
2106
Martin Josefsson78cafda2004-02-02 20:01:18 +00002107 for (matchp = matches; matchp; matchp = matchp->next)
2108 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002109
Marc Bouchere6869a82000-03-20 06:03:29 +00002110 if (target)
2111 target->final_check(target->tflags);
2112
2113 /* Fix me: must put inverse options checking here --MN */
2114
2115 if (optind < argc)
2116 exit_error(PARAMETER_PROBLEM,
2117 "unknown arguments found on commandline");
2118 if (!command)
2119 exit_error(PARAMETER_PROBLEM, "no command specified");
2120 if (invert)
2121 exit_error(PARAMETER_PROBLEM,
2122 "nothing appropriate following !");
2123
Harald Welte6336bfd2002-05-07 14:41:43 +00002124 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002125 if (!(options & OPT_DESTINATION))
2126 dhostnetworkmask = "0.0.0.0/0";
2127 if (!(options & OPT_SOURCE))
2128 shostnetworkmask = "0.0.0.0/0";
2129 }
2130
2131 if (shostnetworkmask)
2132 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2133 &(fw.ip.smsk), &nsaddrs);
2134
2135 if (dhostnetworkmask)
2136 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2137 &(fw.ip.dmsk), &ndaddrs);
2138
2139 if ((nsaddrs > 1 || ndaddrs > 1) &&
2140 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2141 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2142 " source or destination IP addresses");
2143
Marc Bouchere6869a82000-03-20 06:03:29 +00002144 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2145 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2146 "specify a unique address");
2147
2148 generic_opt_check(command, options);
2149
2150 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2151 exit_error(PARAMETER_PROBLEM,
2152 "chain name `%s' too long (must be under %i chars)",
2153 chain, IPT_FUNCTION_MAXNAMELEN);
2154
Harald Welteae1ff9f2000-12-01 14:26:20 +00002155 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002156 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002157 *handle = iptc_init(*table);
2158
Harald Welte82dd2ec2000-12-19 05:18:15 +00002159 if (!*handle) {
2160 /* try to insmod the module if iptc_init failed */
2161 iptables_insmod("ip_tables", modprobe);
2162 *handle = iptc_init(*table);
2163 }
2164
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 if (!*handle)
2166 exit_error(VERSION_PROBLEM,
2167 "can't initialize iptables table `%s': %s",
2168 *table, iptc_strerror(errno));
2169
Harald Welte6336bfd2002-05-07 14:41:43 +00002170 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002171 || command == CMD_DELETE
2172 || command == CMD_INSERT
2173 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002174 if (strcmp(chain, "PREROUTING") == 0
2175 || strcmp(chain, "INPUT") == 0) {
2176 /* -o not valid with incoming packets. */
2177 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002178 exit_error(PARAMETER_PROBLEM,
2179 "Can't use -%c with %s\n",
2180 opt2char(OPT_VIANAMEOUT),
2181 chain);
2182 }
2183
Rusty Russella4860fd2000-06-17 16:13:02 +00002184 if (strcmp(chain, "POSTROUTING") == 0
2185 || strcmp(chain, "OUTPUT") == 0) {
2186 /* -i not valid with outgoing packets */
2187 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002188 exit_error(PARAMETER_PROBLEM,
2189 "Can't use -%c with %s\n",
2190 opt2char(OPT_VIANAMEIN),
2191 chain);
2192 }
2193
2194 if (target && iptc_is_chain(jumpto, *handle)) {
2195 printf("Warning: using chain %s, not extension\n",
2196 jumpto);
2197
2198 target = NULL;
2199 }
2200
2201 /* If they didn't specify a target, or it's a chain
2202 name, use standard. */
2203 if (!target
2204 && (strlen(jumpto) == 0
2205 || iptc_is_chain(jumpto, *handle))) {
2206 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002207
Rusty Russell52a51492000-05-02 16:44:29 +00002208 target = find_target(IPT_STANDARD_TARGET,
2209 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002210
2211 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002212 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002213 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002214 target->t->u.target_size = size;
2215 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002216 target->init(target->t, &fw.nfcache);
2217 }
2218
Rusty Russell7e53bf92000-03-20 07:03:28 +00002219 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002220 /* it is no chain, and we can't load a plugin.
2221 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002222 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002223 * chain. */
2224 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002225 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002226 e = generate_entry(&fw, matches, target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002227 }
2228 }
2229
2230 switch (command) {
2231 case CMD_APPEND:
2232 ret = append_entry(chain, e,
2233 nsaddrs, saddrs, ndaddrs, daddrs,
2234 options&OPT_VERBOSE,
2235 handle);
2236 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002237 case CMD_DELETE:
2238 ret = delete_entry(chain, e,
2239 nsaddrs, saddrs, ndaddrs, daddrs,
2240 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002241 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002242 break;
2243 case CMD_DELETE_NUM:
2244 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2245 break;
2246 case CMD_REPLACE:
2247 ret = replace_entry(chain, e, rulenum - 1,
2248 saddrs, daddrs, options&OPT_VERBOSE,
2249 handle);
2250 break;
2251 case CMD_INSERT:
2252 ret = insert_entry(chain, e, rulenum - 1,
2253 nsaddrs, saddrs, ndaddrs, daddrs,
2254 options&OPT_VERBOSE,
2255 handle);
2256 break;
2257 case CMD_LIST:
2258 ret = list_entries(chain,
2259 options&OPT_VERBOSE,
2260 options&OPT_NUMERIC,
2261 options&OPT_EXPANDED,
2262 options&OPT_LINENUMBERS,
2263 handle);
2264 break;
2265 case CMD_FLUSH:
2266 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2267 break;
2268 case CMD_ZERO:
2269 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2270 break;
2271 case CMD_LIST|CMD_ZERO:
2272 ret = list_entries(chain,
2273 options&OPT_VERBOSE,
2274 options&OPT_NUMERIC,
2275 options&OPT_EXPANDED,
2276 options&OPT_LINENUMBERS,
2277 handle);
2278 if (ret)
2279 ret = zero_entries(chain,
2280 options&OPT_VERBOSE, handle);
2281 break;
2282 case CMD_NEW_CHAIN:
2283 ret = iptc_create_chain(chain, handle);
2284 break;
2285 case CMD_DELETE_CHAIN:
2286 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2287 break;
2288 case CMD_RENAME_CHAIN:
2289 ret = iptc_rename_chain(chain, newname, handle);
2290 break;
2291 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002292 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002293 break;
2294 default:
2295 /* We should never reach this... */
2296 exit_tryhelp(2);
2297 }
2298
2299 if (verbose > 1)
2300 dump_entries(*handle);
2301
Martin Josefsson78cafda2004-02-02 20:01:18 +00002302 clear_rule_matches(&matches);
2303
Marc Bouchere6869a82000-03-20 06:03:29 +00002304 return ret;
2305}