blob: 7c003ad19bbab424682cde2c6776737c1aca2fe9 [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
Marc Bouchere6869a82000-03-20 06:03:29 +0000150static struct option *opts = original_opts;
151static unsigned int global_option_offset = 0;
152
153/* Table of legal combinations of commands and options. If any of the
154 * given commands make an option legal, that option is legal (applies to
155 * CMD_LIST and CMD_ZERO only).
156 * Key:
157 * + compulsory
158 * x illegal
159 * optional
160 */
161
162static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
163/* Well, it's better than "Re: Linux vs FreeBSD" */
164{
165 /* -n -s -d -p -j -v -x -i -o -f --line */
166/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
168/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
169/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
170/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
171/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
172/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
175/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
176/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000177/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000178/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
179};
180
181static int inverse_for_options[NUMBER_OF_OPT] =
182{
183/* -n */ 0,
184/* -s */ IPT_INV_SRCIP,
185/* -d */ IPT_INV_DSTIP,
186/* -p */ IPT_INV_PROTO,
187/* -j */ 0,
188/* -v */ 0,
189/* -x */ 0,
190/* -i */ IPT_INV_VIA_IN,
191/* -o */ IPT_INV_VIA_OUT,
192/* -f */ IPT_INV_FRAG,
193/*--line*/ 0
194};
195
196const char *program_version;
197const char *program_name;
198
Rusty Russell2e0a3212000-04-19 11:23:18 +0000199/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000200struct iptables_match *iptables_matches = NULL;
201struct iptables_target *iptables_targets = NULL;
202
203/* Extra debugging from libiptc */
204extern void dump_entries(const iptc_handle_t handle);
205
206/* A few hardcoded protocols for 'all' and in case the user has no
207 /etc/protocols */
208struct pprot {
209 char *name;
210 u_int8_t num;
211};
212
Rusty Russell208d42e2004-12-20 05:29:52 +0000213static char *lib_dir;
214
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000215/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000216/* defined in netinet/in.h */
217#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000218#ifndef IPPROTO_ESP
219#define IPPROTO_ESP 50
220#endif
221#ifndef IPPROTO_AH
222#define IPPROTO_AH 51
223#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000224#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000225
Marc Bouchere6869a82000-03-20 06:03:29 +0000226static const struct pprot chain_protos[] = {
227 { "tcp", IPPROTO_TCP },
228 { "udp", IPPROTO_UDP },
229 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000230 { "esp", IPPROTO_ESP },
231 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000232 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000233 { "all", 0 },
234};
235
236static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000237proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000238{
239 unsigned int i;
240
Rusty Russell28381a42000-05-10 00:19:50 +0000241 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000242 struct protoent *pent = getprotobynumber(proto);
243 if (pent)
244 return pent->p_name;
245 }
246
247 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
248 if (chain_protos[i].num == proto)
249 return chain_protos[i].name;
250
251 return NULL;
252}
253
254struct in_addr *
255dotted_to_addr(const char *dotted)
256{
257 static struct in_addr addr;
258 unsigned char *addrp;
259 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000260 unsigned int onebyte;
261 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000262 char buf[20];
263
264 /* copy dotted string, because we need to modify it */
265 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000266 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000267 addrp = (unsigned char *) &(addr.s_addr);
268
269 p = buf;
270 for (i = 0; i < 3; i++) {
271 if ((q = strchr(p, '.')) == NULL)
272 return (struct in_addr *) NULL;
273
274 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000275 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 return (struct in_addr *) NULL;
277
278 addrp[i] = (unsigned char) onebyte;
279 p = q + 1;
280 }
281
282 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000283 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000284 return (struct in_addr *) NULL;
285
286 addrp[3] = (unsigned char) onebyte;
287
288 return &addr;
289}
290
291static struct in_addr *
292network_to_addr(const char *name)
293{
294 struct netent *net;
295 static struct in_addr addr;
296
297 if ((net = getnetbyname(name)) != NULL) {
298 if (net->n_addrtype != AF_INET)
299 return (struct in_addr *) NULL;
300 addr.s_addr = htonl((unsigned long) net->n_net);
301 return &addr;
302 }
303
304 return (struct in_addr *) NULL;
305}
306
307static void
308inaddrcpy(struct in_addr *dst, struct in_addr *src)
309{
310 /* memcpy(dst, src, sizeof(struct in_addr)); */
311 dst->s_addr = src->s_addr;
312}
313
314void
315exit_error(enum exittype status, char *msg, ...)
316{
317 va_list args;
318
319 va_start(args, msg);
320 fprintf(stderr, "%s v%s: ", program_name, program_version);
321 vfprintf(stderr, msg, args);
322 va_end(args);
323 fprintf(stderr, "\n");
324 if (status == PARAMETER_PROBLEM)
325 exit_tryhelp(status);
326 if (status == VERSION_PROBLEM)
327 fprintf(stderr,
328 "Perhaps iptables or your kernel needs to be upgraded.\n");
329 exit(status);
330}
331
332void
333exit_tryhelp(int status)
334{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000335 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000336 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000337 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
338 program_name, program_name );
339 exit(status);
340}
341
342void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000343exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000344{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000345 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000346 struct iptables_target *t = NULL;
347
Marc Bouchere6869a82000-03-20 06:03:29 +0000348 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000349"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000350" %s -[RI] chain rulenum rule-specification [options]\n"
351" %s -D chain rulenum [options]\n"
352" %s -[LFZ] [chain] [options]\n"
353" %s -[NX] chain\n"
354" %s -E old-chain-name new-chain-name\n"
355" %s -P chain target [options]\n"
356" %s -h (print this help information)\n\n",
357 program_name, program_version, program_name, program_name,
358 program_name, program_name, program_name, program_name,
359 program_name, program_name);
360
361 printf(
362"Commands:\n"
363"Either long or short options are allowed.\n"
364" --append -A chain Append to chain\n"
365" --delete -D chain Delete matching rule from chain\n"
366" --delete -D chain rulenum\n"
367" Delete rule rulenum (1 = first) from chain\n"
368" --insert -I chain [rulenum]\n"
369" Insert in chain as rulenum (default 1=first)\n"
370" --replace -R chain rulenum\n"
371" Replace rule rulenum (1 = first) in chain\n"
372" --list -L [chain] List the rules in a chain or all chains\n"
373" --flush -F [chain] Delete all rules in chain or all chains\n"
374" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000375" --new -N chain Create a new user-defined chain\n"
376" --delete-chain\n"
377" -X [chain] Delete a user-defined chain\n"
378" --policy -P chain target\n"
379" Change policy on chain to target\n"
380" --rename-chain\n"
381" -E old-chain new-chain\n"
382" Change chain name, (moving any references)\n"
383
384"Options:\n"
385" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
386" --source -s [!] address[/mask]\n"
387" source specification\n"
388" --destination -d [!] address[/mask]\n"
389" destination specification\n"
390" --in-interface -i [!] input name[+]\n"
391" network interface name ([+] for wildcard)\n"
392" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000393" target for rule (may load target extension)\n"
394" --match -m match\n"
395" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000396" --numeric -n numeric output of addresses and ports\n"
397" --out-interface -o [!] output name[+]\n"
398" network interface name ([+] for wildcard)\n"
399" --table -t table table to manipulate (default: `filter')\n"
400" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000401" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000402" --exact -x expand numbers (display exact values)\n"
403"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000404" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000405" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000406"[!] --version -V print package version.\n");
407
Rusty Russell363112d2000-08-11 13:49:26 +0000408 /* Print out any special helps. A user might like to be able
409 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000410 results. So we call help for all specified matches & targets */
411 for (t = iptables_targets; t ;t = t->next) {
412 if (t->used) {
413 printf("\n");
414 t->help();
415 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000416 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000417 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000418 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000419 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000420 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000421 exit(0);
422}
423
424static void
425generic_opt_check(int command, int options)
426{
427 int i, j, legal = 0;
428
429 /* Check that commands are valid with options. Complicated by the
430 * fact that if an option is legal with *any* command given, it is
431 * legal overall (ie. -z and -l).
432 */
433 for (i = 0; i < NUMBER_OF_OPT; i++) {
434 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
435
436 for (j = 0; j < NUMBER_OF_CMD; j++) {
437 if (!(command & (1<<j)))
438 continue;
439
440 if (!(options & (1<<i))) {
441 if (commands_v_options[j][i] == '+')
442 exit_error(PARAMETER_PROBLEM,
443 "You need to supply the `-%c' "
444 "option for this command\n",
445 optflags[i]);
446 } else {
447 if (commands_v_options[j][i] != 'x')
448 legal = 1;
449 else if (legal == 0)
450 legal = -1;
451 }
452 }
453 if (legal == -1)
454 exit_error(PARAMETER_PROBLEM,
455 "Illegal option `-%c' with this command\n",
456 optflags[i]);
457 }
458}
459
460static char
461opt2char(int option)
462{
463 const char *ptr;
464 for (ptr = optflags; option > 1; option >>= 1, ptr++);
465
466 return *ptr;
467}
468
469static char
470cmd2char(int option)
471{
472 const char *ptr;
473 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
474
475 return *ptr;
476}
477
478static void
479add_command(int *cmd, const int newcmd, const int othercmds, int invert)
480{
481 if (invert)
482 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
483 if (*cmd & (~othercmds))
484 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
485 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
486 *cmd |= newcmd;
487}
488
489int
Harald Welteb77f1da2002-03-14 11:35:58 +0000490check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000491{
492 if (option && strcmp(option, "!") == 0) {
493 if (*invert)
494 exit_error(PARAMETER_PROBLEM,
495 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000496 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000497 if (optind) {
498 *optind = *optind+1;
499 if (argc && *optind > argc)
500 exit_error(PARAMETER_PROBLEM,
501 "no argument following `!'");
502 }
503
Marc Bouchere6869a82000-03-20 06:03:29 +0000504 return TRUE;
505 }
506 return FALSE;
507}
508
509static void *
510fw_calloc(size_t count, size_t size)
511{
512 void *p;
513
514 if ((p = calloc(count, size)) == NULL) {
515 perror("iptables: calloc failed");
516 exit(1);
517 }
518 return p;
519}
520
521static void *
522fw_malloc(size_t size)
523{
524 void *p;
525
526 if ((p = malloc(size)) == NULL) {
527 perror("iptables: malloc failed");
528 exit(1);
529 }
530 return p;
531}
532
533static struct in_addr *
534host_to_addr(const char *name, unsigned int *naddr)
535{
536 struct hostent *host;
537 struct in_addr *addr;
538 unsigned int i;
539
540 *naddr = 0;
541 if ((host = gethostbyname(name)) != NULL) {
542 if (host->h_addrtype != AF_INET ||
543 host->h_length != sizeof(struct in_addr))
544 return (struct in_addr *) NULL;
545
546 while (host->h_addr_list[*naddr] != (char *) NULL)
547 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000548 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000549 for (i = 0; i < *naddr; i++)
550 inaddrcpy(&(addr[i]),
551 (struct in_addr *) host->h_addr_list[i]);
552 return addr;
553 }
554
555 return (struct in_addr *) NULL;
556}
557
558static char *
559addr_to_host(const struct in_addr *addr)
560{
561 struct hostent *host;
562
563 if ((host = gethostbyaddr((char *) addr,
564 sizeof(struct in_addr), AF_INET)) != NULL)
565 return (char *) host->h_name;
566
567 return (char *) NULL;
568}
569
570/*
571 * All functions starting with "parse" should succeed, otherwise
572 * the program fails.
573 * Most routines return pointers to static data that may change
574 * between calls to the same or other routines with a few exceptions:
575 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
576 * return global static data.
577*/
578
579static struct in_addr *
580parse_hostnetwork(const char *name, unsigned int *naddrs)
581{
582 struct in_addr *addrp, *addrptmp;
583
584 if ((addrptmp = dotted_to_addr(name)) != NULL ||
585 (addrptmp = network_to_addr(name)) != NULL) {
586 addrp = fw_malloc(sizeof(struct in_addr));
587 inaddrcpy(addrp, addrptmp);
588 *naddrs = 1;
589 return addrp;
590 }
591 if ((addrp = host_to_addr(name, naddrs)) != NULL)
592 return addrp;
593
594 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
595}
596
597static struct in_addr *
598parse_mask(char *mask)
599{
600 static struct in_addr maskaddr;
601 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000602 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000603
604 if (mask == NULL) {
605 /* no mask at all defaults to 32 bits */
606 maskaddr.s_addr = 0xFFFFFFFF;
607 return &maskaddr;
608 }
609 if ((addrp = dotted_to_addr(mask)) != NULL)
610 /* dotted_to_addr already returns a network byte order addr */
611 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000612 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000613 exit_error(PARAMETER_PROBLEM,
614 "invalid mask `%s' specified", mask);
615 if (bits != 0) {
616 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
617 return &maskaddr;
618 }
619
620 maskaddr.s_addr = 0L;
621 return &maskaddr;
622}
623
Marc Boucherb93c7982001-12-06 14:50:19 +0000624void
Marc Bouchere6869a82000-03-20 06:03:29 +0000625parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
626 struct in_addr *maskp, unsigned int *naddrs)
627{
628 struct in_addr *addrp;
629 char buf[256];
630 char *p;
631 int i, j, k, n;
632
633 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000634 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000635 if ((p = strrchr(buf, '/')) != NULL) {
636 *p = '\0';
637 addrp = parse_mask(p + 1);
638 } else
639 addrp = parse_mask(NULL);
640 inaddrcpy(maskp, addrp);
641
642 /* if a null mask is given, the name is ignored, like in "any/0" */
643 if (maskp->s_addr == 0L)
644 strcpy(buf, "0.0.0.0");
645
646 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
647 n = *naddrs;
648 for (i = 0, j = 0; i < n; i++) {
649 addrp[j++].s_addr &= maskp->s_addr;
650 for (k = 0; k < j - 1; k++) {
651 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
652 (*naddrs)--;
653 j--;
654 break;
655 }
656 }
657 }
658}
659
660struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000661find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000662{
663 struct iptables_match *ptr;
664
665 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
666 if (strcmp(name, ptr->name) == 0)
667 break;
668 }
669
Harald Welte3efb6ea2001-08-06 18:50:21 +0000670#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000671 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000672 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000673 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000674 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000675 if (dlopen(path, RTLD_NOW)) {
676 /* Found library. If it didn't register itself,
677 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000678 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000679
Rusty Russell9e1d2142000-04-23 09:11:12 +0000680 if (!ptr)
681 exit_error(PARAMETER_PROBLEM,
682 "Couldn't load match `%s'\n",
683 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000684 } else if (tryload == LOAD_MUST_SUCCEED)
685 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000686 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000687 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000688 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000689#else
690 if (ptr && !ptr->loaded) {
691 if (tryload != DONT_LOAD)
692 ptr->loaded = 1;
693 else
694 ptr = NULL;
695 }
Marc Boucher067477b2002-03-24 15:09:31 +0000696 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
697 exit_error(PARAMETER_PROBLEM,
698 "Couldn't find match `%s'\n", name);
699 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000700#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000701
Martin Josefsson78cafda2004-02-02 20:01:18 +0000702 if (ptr && matches) {
703 struct iptables_rule_match **i;
704 struct iptables_rule_match *newentry;
705
706 newentry = fw_malloc(sizeof(struct iptables_rule_match));
707
708 for (i = matches; *i; i = &(*i)->next);
709 newentry->match = ptr;
710 newentry->next = NULL;
711 *i = newentry;
712 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000713
Marc Bouchere6869a82000-03-20 06:03:29 +0000714 return ptr;
715}
716
Rusty Russell28381a42000-05-10 00:19:50 +0000717/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
718static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000719find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000720{
Harald Welteed498492001-07-23 01:24:22 +0000721 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000722
Harald Welte0b0013a2002-02-18 16:15:31 +0000723 if (string_to_number(pname, 0, 255, &proto) != -1) {
724 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000725
Harald Welte0b0013a2002-02-18 16:15:31 +0000726 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000727 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000728 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000729 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000730
731 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000732}
733
Marc Boucherb93c7982001-12-06 14:50:19 +0000734u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000735parse_protocol(const char *s)
736{
Harald Welteed498492001-07-23 01:24:22 +0000737 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000738
Harald Welteed498492001-07-23 01:24:22 +0000739 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000740 struct protoent *pent;
741
742 if ((pent = getprotobyname(s)))
743 proto = pent->p_proto;
744 else {
745 unsigned int i;
746 for (i = 0;
747 i < sizeof(chain_protos)/sizeof(struct pprot);
748 i++) {
749 if (strcmp(s, chain_protos[i].name) == 0) {
750 proto = chain_protos[i].num;
751 break;
752 }
753 }
754 if (i == sizeof(chain_protos)/sizeof(struct pprot))
755 exit_error(PARAMETER_PROBLEM,
756 "unknown protocol `%s' specified",
757 s);
758 }
759 }
760
761 return (u_int16_t)proto;
762}
763
764static void
765parse_interface(const char *arg, char *vianame, unsigned char *mask)
766{
767 int vialen = strlen(arg);
768 unsigned int i;
769
770 memset(mask, 0, IFNAMSIZ);
771 memset(vianame, 0, IFNAMSIZ);
772
773 if (vialen + 1 > IFNAMSIZ)
774 exit_error(PARAMETER_PROBLEM,
775 "interface name `%s' must be shorter than IFNAMSIZ"
776 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000777
Marc Bouchere6869a82000-03-20 06:03:29 +0000778 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000779 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000780 memset(mask, 0, IFNAMSIZ);
781 else if (vianame[vialen - 1] == '+') {
782 memset(mask, 0xFF, vialen - 1);
783 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000784 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000785 } else {
786 /* Include nul-terminator in match */
787 memset(mask, 0xFF, vialen + 1);
788 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000789 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000790 if (!isalnum(vianame[i])
791 && vianame[i] != '_'
792 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000793 printf("Warning: wierd character in interface"
794 " `%s' (No aliases, :, ! or *).\n",
795 vianame);
796 break;
797 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000798 }
799 }
800}
801
802/* Can't be zero. */
803static int
804parse_rulenumber(const char *rule)
805{
Harald Welteed498492001-07-23 01:24:22 +0000806 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000807
Harald Welteed498492001-07-23 01:24:22 +0000808 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 exit_error(PARAMETER_PROBLEM,
810 "Invalid rule number `%s'", rule);
811
812 return rulenum;
813}
814
815static const char *
816parse_target(const char *targetname)
817{
818 const char *ptr;
819
820 if (strlen(targetname) < 1)
821 exit_error(PARAMETER_PROBLEM,
822 "Invalid target name (too short)");
823
824 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
825 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000826 "Invalid target name `%s' (%u chars max)",
827 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000828
829 for (ptr = targetname; *ptr; ptr++)
830 if (isspace(*ptr))
831 exit_error(PARAMETER_PROBLEM,
832 "Invalid target name `%s'", targetname);
833 return targetname;
834}
835
836static char *
837addr_to_network(const struct in_addr *addr)
838{
839 struct netent *net;
840
841 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
842 return (char *) net->n_name;
843
844 return (char *) NULL;
845}
846
847char *
848addr_to_dotted(const struct in_addr *addrp)
849{
850 static char buf[20];
851 const unsigned char *bytep;
852
853 bytep = (const unsigned char *) &(addrp->s_addr);
854 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
855 return buf;
856}
Marc Boucherb93c7982001-12-06 14:50:19 +0000857
858char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000859addr_to_anyname(const struct in_addr *addr)
860{
861 char *name;
862
863 if ((name = addr_to_host(addr)) != NULL ||
864 (name = addr_to_network(addr)) != NULL)
865 return name;
866
867 return addr_to_dotted(addr);
868}
869
Marc Boucherb93c7982001-12-06 14:50:19 +0000870char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000871mask_to_dotted(const struct in_addr *mask)
872{
873 int i;
874 static char buf[20];
875 u_int32_t maskaddr, bits;
876
877 maskaddr = ntohl(mask->s_addr);
878
879 if (maskaddr == 0xFFFFFFFFL)
880 /* we don't want to see "/32" */
881 return "";
882
883 i = 32;
884 bits = 0xFFFFFFFEL;
885 while (--i >= 0 && maskaddr != bits)
886 bits <<= 1;
887 if (i >= 0)
888 sprintf(buf, "/%d", i);
889 else
890 /* mask was not a decent combination of 1's and 0's */
891 sprintf(buf, "/%s", addr_to_dotted(mask));
892
893 return buf;
894}
895
896int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000897string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
898 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000899{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000900 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000901 char *end;
902
903 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000904 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000905 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000906 if (*end == '\0' && end != s) {
907 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000909 *ret = number;
910 return 0;
911 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000912 }
913 return -1;
914}
915
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916int
917string_to_number_l(const char *s, unsigned long min, unsigned long max,
918 unsigned long *ret)
919{
920 int result;
921 unsigned long long number;
922
923 result = string_to_number_ll(s, min, max, &number);
924 *ret = (unsigned long)number;
925
926 return result;
927}
928
929int string_to_number(const char *s, unsigned int min, unsigned int max,
930 unsigned int *ret)
931{
932 int result;
933 unsigned long number;
934
935 result = string_to_number_l(s, min, max, &number);
936 *ret = (unsigned int)number;
937
938 return result;
939}
940
Marc Bouchere6869a82000-03-20 06:03:29 +0000941static void
942set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
943 int invert)
944{
945 if (*options & option)
946 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
947 opt2char(option));
948 *options |= option;
949
950 if (invert) {
951 unsigned int i;
952 for (i = 0; 1 << i != option; i++);
953
954 if (!inverse_for_options[i])
955 exit_error(PARAMETER_PROBLEM,
956 "cannot have ! before -%c",
957 opt2char(option));
958 *invflg |= inverse_for_options[i];
959 }
960}
961
962struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000963find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000964{
965 struct iptables_target *ptr;
966
967 /* Standard target? */
968 if (strcmp(name, "") == 0
969 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
970 || strcmp(name, IPTC_LABEL_DROP) == 0
971 || strcmp(name, IPTC_LABEL_QUEUE) == 0
972 || strcmp(name, IPTC_LABEL_RETURN) == 0)
973 name = "standard";
974
975 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
976 if (strcmp(name, ptr->name) == 0)
977 break;
978 }
979
Harald Welte3efb6ea2001-08-06 18:50:21 +0000980#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000981 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000982 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000983 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000984 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000985 if (dlopen(path, RTLD_NOW)) {
986 /* Found library. If it didn't register itself,
987 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000988 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000989 if (!ptr)
990 exit_error(PARAMETER_PROBLEM,
991 "Couldn't load target `%s'\n",
992 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000993 } else if (tryload == LOAD_MUST_SUCCEED)
994 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000995 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000996 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000997 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000998#else
999 if (ptr && !ptr->loaded) {
1000 if (tryload != DONT_LOAD)
1001 ptr->loaded = 1;
1002 else
1003 ptr = NULL;
1004 }
Marc Boucher067477b2002-03-24 15:09:31 +00001005 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1006 exit_error(PARAMETER_PROBLEM,
1007 "Couldn't find target `%s'\n", name);
1008 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001009#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001010
Harald Welteae1ff9f2000-12-01 14:26:20 +00001011 if (ptr)
1012 ptr->used = 1;
1013
Marc Bouchere6869a82000-03-20 06:03:29 +00001014 return ptr;
1015}
1016
1017static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001018merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001019 unsigned int *option_offset)
1020{
1021 unsigned int num_old, num_new, i;
1022 struct option *merge;
1023
1024 for (num_old = 0; oldopts[num_old].name; num_old++);
1025 for (num_new = 0; newopts[num_new].name; num_new++);
1026
1027 global_option_offset += OPTION_OFFSET;
1028 *option_offset = global_option_offset;
1029
1030 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1031 memcpy(merge, oldopts, num_old * sizeof(struct option));
1032 for (i = 0; i < num_new; i++) {
1033 merge[num_old + i] = newopts[i];
1034 merge[num_old + i].val += *option_offset;
1035 }
1036 memset(merge + num_old + num_new, 0, sizeof(struct option));
1037
1038 return merge;
1039}
1040
1041void
1042register_match(struct iptables_match *me)
1043{
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001044 struct iptables_match **i;
1045
Marc Bouchere6869a82000-03-20 06:03:29 +00001046 if (strcmp(me->version, program_version) != 0) {
1047 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1048 program_name, me->name, me->version, program_version);
1049 exit(1);
1050 }
1051
Martin Josefsson78cafda2004-02-02 20:01:18 +00001052 if (find_match(me->name, DONT_LOAD, NULL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001053 fprintf(stderr, "%s: match `%s' already registered.\n",
1054 program_name, me->name);
1055 exit(1);
1056 }
1057
Rusty Russell73f72f52000-07-03 10:17:57 +00001058 if (me->size != IPT_ALIGN(me->size)) {
1059 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001060 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001061 exit(1);
1062 }
1063
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001064 /* Append to list. */
1065 for (i = &iptables_matches; *i; i = &(*i)->next);
1066 me->next = NULL;
1067 *i = me;
1068
Marc Bouchere6869a82000-03-20 06:03:29 +00001069 me->m = NULL;
1070 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001071}
1072
1073void
1074register_target(struct iptables_target *me)
1075{
1076 if (strcmp(me->version, program_version) != 0) {
1077 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1078 program_name, me->name, me->version, program_version);
1079 exit(1);
1080 }
1081
Rusty Russell52a51492000-05-02 16:44:29 +00001082 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001083 fprintf(stderr, "%s: target `%s' already registered.\n",
1084 program_name, me->name);
1085 exit(1);
1086 }
1087
Rusty Russell73f72f52000-07-03 10:17:57 +00001088 if (me->size != IPT_ALIGN(me->size)) {
1089 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001090 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001091 exit(1);
1092 }
1093
Marc Bouchere6869a82000-03-20 06:03:29 +00001094 /* Prepend to list. */
1095 me->next = iptables_targets;
1096 iptables_targets = me;
1097 me->t = NULL;
1098 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001099}
1100
1101static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001102print_num(u_int64_t number, unsigned int format)
1103{
1104 if (format & FMT_KILOMEGAGIGA) {
1105 if (number > 99999) {
1106 number = (number + 500) / 1000;
1107 if (number > 9999) {
1108 number = (number + 500) / 1000;
1109 if (number > 9999) {
1110 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001111 if (number > 9999) {
1112 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001113 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001114 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001115 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001116 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001117 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001118 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001119 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001120 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001121 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001122 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001123 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001124}
1125
1126
1127static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001128print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1129{
1130 struct ipt_counters counters;
1131 const char *pol = iptc_get_policy(chain, &counters, handle);
1132 printf("Chain %s", chain);
1133 if (pol) {
1134 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001135 if (!(format & FMT_NOCOUNTS)) {
1136 fputc(' ', stdout);
1137 print_num(counters.pcnt, (format|FMT_NOTABLE));
1138 fputs("packets, ", stdout);
1139 print_num(counters.bcnt, (format|FMT_NOTABLE));
1140 fputs("bytes", stdout);
1141 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001142 printf(")\n");
1143 } else {
1144 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001145 if (!iptc_get_references(&refs, chain, handle))
1146 printf(" (ERROR obtaining refs)\n");
1147 else
1148 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001149 }
1150
1151 if (format & FMT_LINENUMBERS)
1152 printf(FMT("%-4s ", "%s "), "num");
1153 if (!(format & FMT_NOCOUNTS)) {
1154 if (format & FMT_KILOMEGAGIGA) {
1155 printf(FMT("%5s ","%s "), "pkts");
1156 printf(FMT("%5s ","%s "), "bytes");
1157 } else {
1158 printf(FMT("%8s ","%s "), "pkts");
1159 printf(FMT("%10s ","%s "), "bytes");
1160 }
1161 }
1162 if (!(format & FMT_NOTARGET))
1163 printf(FMT("%-9s ","%s "), "target");
1164 fputs(" prot ", stdout);
1165 if (format & FMT_OPTIONS)
1166 fputs("opt", stdout);
1167 if (format & FMT_VIA) {
1168 printf(FMT(" %-6s ","%s "), "in");
1169 printf(FMT("%-6s ","%s "), "out");
1170 }
1171 printf(FMT(" %-19s ","%s "), "source");
1172 printf(FMT(" %-19s "," %s "), "destination");
1173 printf("\n");
1174}
1175
Marc Bouchere6869a82000-03-20 06:03:29 +00001176
1177static int
1178print_match(const struct ipt_entry_match *m,
1179 const struct ipt_ip *ip,
1180 int numeric)
1181{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001182 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001183
1184 if (match) {
1185 if (match->print)
1186 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001187 else
Rusty Russellb039b022000-09-01 06:04:05 +00001188 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001189 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001190 if (m->u.user.name[0])
1191 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 }
1193 /* Don't stop iterating. */
1194 return 0;
1195}
1196
1197/* e is called `fw' here for hysterical raisins */
1198static void
1199print_firewall(const struct ipt_entry *fw,
1200 const char *targname,
1201 unsigned int num,
1202 unsigned int format,
1203 const iptc_handle_t handle)
1204{
1205 struct iptables_target *target = NULL;
1206 const struct ipt_entry_target *t;
1207 u_int8_t flags;
1208 char buf[BUFSIZ];
1209
Marc Bouchere6869a82000-03-20 06:03:29 +00001210 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001211 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001212 else
Rusty Russell52a51492000-05-02 16:44:29 +00001213 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001214
1215 t = ipt_get_target((struct ipt_entry *)fw);
1216 flags = fw->ip.flags;
1217
1218 if (format & FMT_LINENUMBERS)
1219 printf(FMT("%-4u ", "%u "), num+1);
1220
1221 if (!(format & FMT_NOCOUNTS)) {
1222 print_num(fw->counters.pcnt, format);
1223 print_num(fw->counters.bcnt, format);
1224 }
1225
1226 if (!(format & FMT_NOTARGET))
1227 printf(FMT("%-9s ", "%s "), targname);
1228
1229 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1230 {
Rusty Russell28381a42000-05-10 00:19:50 +00001231 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001232 if (pname)
1233 printf(FMT("%-5s", "%s "), pname);
1234 else
1235 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1236 }
1237
1238 if (format & FMT_OPTIONS) {
1239 if (format & FMT_NOTABLE)
1240 fputs("opt ", stdout);
1241 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1242 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1243 fputc(' ', stdout);
1244 }
1245
1246 if (format & FMT_VIA) {
1247 char iface[IFNAMSIZ+2];
1248
1249 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1250 iface[0] = '!';
1251 iface[1] = '\0';
1252 }
1253 else iface[0] = '\0';
1254
1255 if (fw->ip.iniface[0] != '\0') {
1256 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001257 }
1258 else if (format & FMT_NUMERIC) strcat(iface, "*");
1259 else strcat(iface, "any");
1260 printf(FMT(" %-6s ","in %s "), iface);
1261
1262 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1263 iface[0] = '!';
1264 iface[1] = '\0';
1265 }
1266 else iface[0] = '\0';
1267
1268 if (fw->ip.outiface[0] != '\0') {
1269 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 }
1271 else if (format & FMT_NUMERIC) strcat(iface, "*");
1272 else strcat(iface, "any");
1273 printf(FMT("%-6s ","out %s "), iface);
1274 }
1275
1276 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1277 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1278 printf(FMT("%-19s ","%s "), "anywhere");
1279 else {
1280 if (format & FMT_NUMERIC)
1281 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1282 else
1283 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1284 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1285 printf(FMT("%-19s ","%s "), buf);
1286 }
1287
1288 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1289 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001290 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001291 else {
1292 if (format & FMT_NUMERIC)
1293 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1294 else
1295 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1296 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001297 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001298 }
1299
1300 if (format & FMT_NOTABLE)
1301 fputs(" ", stdout);
1302
1303 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1304
1305 if (target) {
1306 if (target->print)
1307 /* Print the target information. */
1308 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001309 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001310 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001311 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001312
1313 if (!(format & FMT_NONEWLINE))
1314 fputc('\n', stdout);
1315}
1316
1317static void
1318print_firewall_line(const struct ipt_entry *fw,
1319 const iptc_handle_t h)
1320{
1321 struct ipt_entry_target *t;
1322
1323 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001324 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001325}
1326
1327static int
1328append_entry(const ipt_chainlabel chain,
1329 struct ipt_entry *fw,
1330 unsigned int nsaddrs,
1331 const struct in_addr saddrs[],
1332 unsigned int ndaddrs,
1333 const struct in_addr daddrs[],
1334 int verbose,
1335 iptc_handle_t *handle)
1336{
1337 unsigned int i, j;
1338 int ret = 1;
1339
1340 for (i = 0; i < nsaddrs; i++) {
1341 fw->ip.src.s_addr = saddrs[i].s_addr;
1342 for (j = 0; j < ndaddrs; j++) {
1343 fw->ip.dst.s_addr = daddrs[j].s_addr;
1344 if (verbose)
1345 print_firewall_line(fw, *handle);
1346 ret &= iptc_append_entry(chain, fw, handle);
1347 }
1348 }
1349
1350 return ret;
1351}
1352
1353static int
1354replace_entry(const ipt_chainlabel chain,
1355 struct ipt_entry *fw,
1356 unsigned int rulenum,
1357 const struct in_addr *saddr,
1358 const struct in_addr *daddr,
1359 int verbose,
1360 iptc_handle_t *handle)
1361{
1362 fw->ip.src.s_addr = saddr->s_addr;
1363 fw->ip.dst.s_addr = daddr->s_addr;
1364
1365 if (verbose)
1366 print_firewall_line(fw, *handle);
1367 return iptc_replace_entry(chain, fw, rulenum, handle);
1368}
1369
1370static int
1371insert_entry(const ipt_chainlabel chain,
1372 struct ipt_entry *fw,
1373 unsigned int rulenum,
1374 unsigned int nsaddrs,
1375 const struct in_addr saddrs[],
1376 unsigned int ndaddrs,
1377 const struct in_addr daddrs[],
1378 int verbose,
1379 iptc_handle_t *handle)
1380{
1381 unsigned int i, j;
1382 int ret = 1;
1383
1384 for (i = 0; i < nsaddrs; i++) {
1385 fw->ip.src.s_addr = saddrs[i].s_addr;
1386 for (j = 0; j < ndaddrs; j++) {
1387 fw->ip.dst.s_addr = daddrs[j].s_addr;
1388 if (verbose)
1389 print_firewall_line(fw, *handle);
1390 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1391 }
1392 }
1393
1394 return ret;
1395}
1396
Rusty Russell2e0a3212000-04-19 11:23:18 +00001397static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001398make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001399{
1400 /* Establish mask for comparison */
1401 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001402 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001403 unsigned char *mask, *mptr;
1404
1405 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001406 for (matchp = matches; matchp; matchp = matchp->next)
1407 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001408
Rusty Russell9e1d2142000-04-23 09:11:12 +00001409 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001410 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001411 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001412
Rusty Russell9e1d2142000-04-23 09:11:12 +00001413 memset(mask, 0xFF, sizeof(struct ipt_entry));
1414 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001415
Martin Josefsson78cafda2004-02-02 20:01:18 +00001416 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001417 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001418 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001419 + matchp->match->userspacesize);
1420 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001421 }
1422
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001423 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001424 IPT_ALIGN(sizeof(struct ipt_entry_target))
1425 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001426
1427 return mask;
1428}
1429
Marc Bouchere6869a82000-03-20 06:03:29 +00001430static int
1431delete_entry(const ipt_chainlabel chain,
1432 struct ipt_entry *fw,
1433 unsigned int nsaddrs,
1434 const struct in_addr saddrs[],
1435 unsigned int ndaddrs,
1436 const struct in_addr daddrs[],
1437 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001438 iptc_handle_t *handle,
1439 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001440{
1441 unsigned int i, j;
1442 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001443 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001444
Martin Josefsson78cafda2004-02-02 20:01:18 +00001445 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001446 for (i = 0; i < nsaddrs; i++) {
1447 fw->ip.src.s_addr = saddrs[i].s_addr;
1448 for (j = 0; j < ndaddrs; j++) {
1449 fw->ip.dst.s_addr = daddrs[j].s_addr;
1450 if (verbose)
1451 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001452 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001453 }
1454 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001455 free(mask);
1456
Marc Bouchere6869a82000-03-20 06:03:29 +00001457 return ret;
1458}
1459
Harald Welteae1ff9f2000-12-01 14:26:20 +00001460int
Marc Bouchere6869a82000-03-20 06:03:29 +00001461for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001462 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001463{
1464 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001465 const char *chain;
1466 char *chains;
1467 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001468
Rusty Russell9e1d2142000-04-23 09:11:12 +00001469 chain = iptc_first_chain(handle);
1470 while (chain) {
1471 chaincount++;
1472 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001473 }
1474
Rusty Russell9e1d2142000-04-23 09:11:12 +00001475 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1476 i = 0;
1477 chain = iptc_first_chain(handle);
1478 while (chain) {
1479 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1480 i++;
1481 chain = iptc_next_chain(handle);
1482 }
1483
1484 for (i = 0; i < chaincount; i++) {
1485 if (!builtinstoo
1486 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001487 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001488 continue;
1489 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1490 }
1491
1492 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001493 return ret;
1494}
1495
Harald Welteae1ff9f2000-12-01 14:26:20 +00001496int
Marc Bouchere6869a82000-03-20 06:03:29 +00001497flush_entries(const ipt_chainlabel chain, int verbose,
1498 iptc_handle_t *handle)
1499{
1500 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001501 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001502
1503 if (verbose)
1504 fprintf(stdout, "Flushing chain `%s'\n", chain);
1505 return iptc_flush_entries(chain, handle);
1506}
Marc Bouchere6869a82000-03-20 06:03:29 +00001507
1508static int
1509zero_entries(const ipt_chainlabel chain, int verbose,
1510 iptc_handle_t *handle)
1511{
1512 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001513 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001514
Marc Bouchere6869a82000-03-20 06:03:29 +00001515 if (verbose)
1516 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1517 return iptc_zero_entries(chain, handle);
1518}
1519
Harald Welteae1ff9f2000-12-01 14:26:20 +00001520int
Marc Bouchere6869a82000-03-20 06:03:29 +00001521delete_chain(const ipt_chainlabel chain, int verbose,
1522 iptc_handle_t *handle)
1523{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001524 if (!chain)
1525 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001526
1527 if (verbose)
1528 fprintf(stdout, "Deleting chain `%s'\n", chain);
1529 return iptc_delete_chain(chain, handle);
1530}
1531
1532static int
1533list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1534 int expanded, int linenumbers, iptc_handle_t *handle)
1535{
1536 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001537 unsigned int format;
1538 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001539
1540 format = FMT_OPTIONS;
1541 if (!verbose)
1542 format |= FMT_NOCOUNTS;
1543 else
1544 format |= FMT_VIA;
1545
1546 if (numeric)
1547 format |= FMT_NUMERIC;
1548
1549 if (!expanded)
1550 format |= FMT_KILOMEGAGIGA;
1551
1552 if (linenumbers)
1553 format |= FMT_LINENUMBERS;
1554
Rusty Russell9e1d2142000-04-23 09:11:12 +00001555 for (this = iptc_first_chain(handle);
1556 this;
1557 this = iptc_next_chain(handle)) {
1558 const struct ipt_entry *i;
1559 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001560
Marc Bouchere6869a82000-03-20 06:03:29 +00001561 if (chain && strcmp(chain, this) != 0)
1562 continue;
1563
1564 if (found) printf("\n");
1565
1566 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001567 i = iptc_first_rule(this, handle);
1568
1569 num = 0;
1570 while (i) {
1571 print_firewall(i,
1572 iptc_get_target(i, handle),
1573 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001574 format,
1575 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001576 i = iptc_next_rule(i, handle);
1577 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001578 found = 1;
1579 }
1580
1581 errno = ENOENT;
1582 return found;
1583}
1584
Harald Welte82dd2ec2000-12-19 05:18:15 +00001585static char *get_modprobe(void)
1586{
1587 int procfile;
1588 char *ret;
1589
Harald Welte10f7f142004-10-22 08:14:07 +00001590#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001591 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1592 if (procfile < 0)
1593 return NULL;
1594
Harald Welte10f7f142004-10-22 08:14:07 +00001595 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001596 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001597 memset(ret, 0, PROCFILE_BUFSIZ);
1598 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001599 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001600 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001601 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001602 if (ret[strlen(ret)-1]=='\n')
1603 ret[strlen(ret)-1]=0;
1604 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001605 return ret;
1606 }
1607 fail:
1608 free(ret);
1609 close(procfile);
1610 return NULL;
1611}
1612
Harald Welte58918652001-06-16 18:25:25 +00001613int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001614{
1615 char *buf = NULL;
1616 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001617 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001618
1619 /* If they don't explicitly set it, read out of kernel */
1620 if (!modprobe) {
1621 buf = get_modprobe();
1622 if (!buf)
1623 return -1;
1624 modprobe = buf;
1625 }
1626
1627 switch (fork()) {
1628 case 0:
1629 argv[0] = (char *)modprobe;
1630 argv[1] = (char *)modname;
1631 argv[2] = NULL;
1632 execv(argv[0], argv);
1633
1634 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001635 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001636 case -1:
1637 return -1;
1638
1639 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001640 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001641 }
1642
1643 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001644 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1645 return 0;
1646 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001647}
1648
Marc Bouchere6869a82000-03-20 06:03:29 +00001649static struct ipt_entry *
1650generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001651 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 struct ipt_entry_target *target)
1653{
1654 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001655 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 struct ipt_entry *e;
1657
1658 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001659 for (matchp = matches; matchp; matchp = matchp->next)
1660 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001661
Rusty Russell228e98d2000-04-27 10:28:06 +00001662 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001663 *e = *fw;
1664 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001665 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001666
1667 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001668 for (matchp = matches; matchp; matchp = matchp->next) {
1669 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1670 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001671 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001672 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001673
1674 return e;
1675}
1676
Martin Josefsson78cafda2004-02-02 20:01:18 +00001677void clear_rule_matches(struct iptables_rule_match **matches)
1678{
1679 struct iptables_rule_match *matchp, *tmp;
1680
1681 for (matchp = *matches; matchp;) {
1682 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001683 if (matchp->match->m)
1684 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001685 free(matchp);
1686 matchp = tmp;
1687 }
1688
1689 *matches = NULL;
1690}
1691
Marc Bouchere6869a82000-03-20 06:03:29 +00001692int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1693{
1694 struct ipt_entry fw, *e = NULL;
1695 int invert = 0;
1696 unsigned int nsaddrs = 0, ndaddrs = 0;
1697 struct in_addr *saddrs = NULL, *daddrs = NULL;
1698
1699 int c, verbose = 0;
1700 const char *chain = NULL;
1701 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1702 const char *policy = NULL, *newname = NULL;
1703 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001704 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 int ret = 1;
1706 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001707 struct iptables_rule_match *matches = NULL;
1708 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001709 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001710 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 const char *jumpto = "";
1712 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001713 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001714 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001715
1716 memset(&fw, 0, sizeof(fw));
1717
Rusty Russell208d42e2004-12-20 05:29:52 +00001718 lib_dir = getenv("IPTABLES_LIB_DIR");
1719 if (!lib_dir)
1720 lib_dir = IPT_LIB_DIR;
1721
Harald Welteae1ff9f2000-12-01 14:26:20 +00001722 /* re-set optind to 0 in case do_command gets called
1723 * a second time */
1724 optind = 0;
1725
1726 /* clear mflags in case do_command gets called a second time
1727 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001728 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001729 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001730
1731 for (t = iptables_targets; t; t = t->next) {
1732 t->tflags = 0;
1733 t->used = 0;
1734 }
1735
Marc Bouchere6869a82000-03-20 06:03:29 +00001736 /* Suppress error messages: we may add new options if we
1737 demand-load a protocol. */
1738 opterr = 0;
1739
1740 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001741 "-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 +00001742 opts, NULL)) != -1) {
1743 switch (c) {
1744 /*
1745 * Command selection
1746 */
1747 case 'A':
1748 add_command(&command, CMD_APPEND, CMD_NONE,
1749 invert);
1750 chain = optarg;
1751 break;
1752
1753 case 'D':
1754 add_command(&command, CMD_DELETE, CMD_NONE,
1755 invert);
1756 chain = optarg;
1757 if (optind < argc && argv[optind][0] != '-'
1758 && argv[optind][0] != '!') {
1759 rulenum = parse_rulenumber(argv[optind++]);
1760 command = CMD_DELETE_NUM;
1761 }
1762 break;
1763
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 case 'R':
1765 add_command(&command, CMD_REPLACE, CMD_NONE,
1766 invert);
1767 chain = optarg;
1768 if (optind < argc && argv[optind][0] != '-'
1769 && argv[optind][0] != '!')
1770 rulenum = parse_rulenumber(argv[optind++]);
1771 else
1772 exit_error(PARAMETER_PROBLEM,
1773 "-%c requires a rule number",
1774 cmd2char(CMD_REPLACE));
1775 break;
1776
1777 case 'I':
1778 add_command(&command, CMD_INSERT, CMD_NONE,
1779 invert);
1780 chain = optarg;
1781 if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 rulenum = parse_rulenumber(argv[optind++]);
1784 else rulenum = 1;
1785 break;
1786
1787 case 'L':
1788 add_command(&command, CMD_LIST, CMD_ZERO,
1789 invert);
1790 if (optarg) chain = optarg;
1791 else if (optind < argc && argv[optind][0] != '-'
1792 && argv[optind][0] != '!')
1793 chain = argv[optind++];
1794 break;
1795
1796 case 'F':
1797 add_command(&command, CMD_FLUSH, 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 'Z':
1806 add_command(&command, CMD_ZERO, CMD_LIST,
1807 invert);
1808 if (optarg) chain = optarg;
1809 else if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 chain = argv[optind++];
1812 break;
1813
1814 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001815 if (optarg && *optarg == '-')
1816 exit_error(PARAMETER_PROBLEM,
1817 "chain name not allowed to start "
1818 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001819 if (find_target(optarg, TRY_LOAD))
1820 exit_error(PARAMETER_PROBLEM,
1821 "chain name may not clash "
1822 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001823 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1824 invert);
1825 chain = optarg;
1826 break;
1827
1828 case 'X':
1829 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1830 invert);
1831 if (optarg) chain = optarg;
1832 else if (optind < argc && argv[optind][0] != '-'
1833 && argv[optind][0] != '!')
1834 chain = argv[optind++];
1835 break;
1836
1837 case 'E':
1838 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1839 invert);
1840 chain = optarg;
1841 if (optind < argc && argv[optind][0] != '-'
1842 && argv[optind][0] != '!')
1843 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001844 else
1845 exit_error(PARAMETER_PROBLEM,
1846 "-%c requires old-chain-name and "
1847 "new-chain-name",
1848 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 break;
1850
1851 case 'P':
1852 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1853 invert);
1854 chain = optarg;
1855 if (optind < argc && argv[optind][0] != '-'
1856 && argv[optind][0] != '!')
1857 policy = argv[optind++];
1858 else
1859 exit_error(PARAMETER_PROBLEM,
1860 "-%c requires a chain and a policy",
1861 cmd2char(CMD_SET_POLICY));
1862 break;
1863
1864 case 'h':
1865 if (!optarg)
1866 optarg = argv[optind];
1867
Rusty Russell2e0a3212000-04-19 11:23:18 +00001868 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001869 if (!matches && protocol)
1870 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001871
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001872 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001873
1874 /*
1875 * Option selection
1876 */
1877 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001878 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1880 invert);
1881
1882 /* Canonicalize into lower case */
1883 for (protocol = argv[optind-1]; *protocol; protocol++)
1884 *protocol = tolower(*protocol);
1885
1886 protocol = argv[optind-1];
1887 fw.ip.proto = parse_protocol(protocol);
1888
1889 if (fw.ip.proto == 0
1890 && (fw.ip.invflags & IPT_INV_PROTO))
1891 exit_error(PARAMETER_PROBLEM,
1892 "rule would never match protocol");
1893 fw.nfcache |= NFC_IP_PROTO;
1894 break;
1895
1896 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001897 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001898 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1899 invert);
1900 shostnetworkmask = argv[optind-1];
1901 fw.nfcache |= NFC_IP_SRC;
1902 break;
1903
1904 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001905 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001906 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1907 invert);
1908 dhostnetworkmask = argv[optind-1];
1909 fw.nfcache |= NFC_IP_DST;
1910 break;
1911
1912 case 'j':
1913 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1914 invert);
1915 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001916 /* TRY_LOAD (may be chain name) */
1917 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001918
1919 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001920 size_t size;
1921
Rusty Russell73f72f52000-07-03 10:17:57 +00001922 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1923 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001924
Rusty Russell2e0a3212000-04-19 11:23:18 +00001925 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001926 target->t->u.target_size = size;
1927 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001928 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001929 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001930 }
1931 break;
1932
1933
1934 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001935 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001936 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1937 invert);
1938 parse_interface(argv[optind-1],
1939 fw.ip.iniface,
1940 fw.ip.iniface_mask);
1941 fw.nfcache |= NFC_IP_IF_IN;
1942 break;
1943
1944 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001945 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001946 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1947 invert);
1948 parse_interface(argv[optind-1],
1949 fw.ip.outiface,
1950 fw.ip.outiface_mask);
1951 fw.nfcache |= NFC_IP_IF_OUT;
1952 break;
1953
1954 case 'f':
1955 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1956 invert);
1957 fw.ip.flags |= IPT_F_FRAG;
1958 fw.nfcache |= NFC_IP_FRAG;
1959 break;
1960
1961 case 'v':
1962 if (!verbose)
1963 set_option(&options, OPT_VERBOSE,
1964 &fw.ip.invflags, invert);
1965 verbose++;
1966 break;
1967
Rusty Russell52a51492000-05-02 16:44:29 +00001968 case 'm': {
1969 size_t size;
1970
Marc Bouchere6869a82000-03-20 06:03:29 +00001971 if (invert)
1972 exit_error(PARAMETER_PROBLEM,
1973 "unexpected ! flag before --match");
1974
Martin Josefsson78cafda2004-02-02 20:01:18 +00001975 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001976 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1977 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001978 m->m = fw_calloc(1, size);
1979 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001980 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001981 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001982 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001983 }
1984 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001985
1986 case 'n':
1987 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1988 invert);
1989 break;
1990
1991 case 't':
1992 if (invert)
1993 exit_error(PARAMETER_PROBLEM,
1994 "unexpected ! flag before --table");
1995 *table = argv[optind-1];
1996 break;
1997
1998 case 'x':
1999 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2000 invert);
2001 break;
2002
2003 case 'V':
2004 if (invert)
2005 printf("Not %s ;-)\n", program_version);
2006 else
2007 printf("%s v%s\n",
2008 program_name, program_version);
2009 exit(0);
2010
2011 case '0':
2012 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2013 invert);
2014 break;
2015
Harald Welte82dd2ec2000-12-19 05:18:15 +00002016 case 'M':
2017 modprobe = optarg;
2018 break;
2019
Harald Welteccd49e52001-01-23 22:54:34 +00002020 case 'c':
2021
2022 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2023 invert);
2024 pcnt = optarg;
2025 if (optind < argc && argv[optind][0] != '-'
2026 && argv[optind][0] != '!')
2027 bcnt = argv[optind++];
2028 else
2029 exit_error(PARAMETER_PROBLEM,
2030 "-%c requires packet and byte counter",
2031 opt2char(OPT_COUNTERS));
2032
Martin Josefssona28d4952004-05-26 16:04:48 +00002033 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002034 exit_error(PARAMETER_PROBLEM,
2035 "-%c packet counter not numeric",
2036 opt2char(OPT_COUNTERS));
2037
Martin Josefssona28d4952004-05-26 16:04:48 +00002038 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002039 exit_error(PARAMETER_PROBLEM,
2040 "-%c byte counter not numeric",
2041 opt2char(OPT_COUNTERS));
2042
2043 break;
2044
2045
Marc Bouchere6869a82000-03-20 06:03:29 +00002046 case 1: /* non option */
2047 if (optarg[0] == '!' && optarg[1] == '\0') {
2048 if (invert)
2049 exit_error(PARAMETER_PROBLEM,
2050 "multiple consecutive ! not"
2051 " allowed");
2052 invert = TRUE;
2053 optarg[0] = '\0';
2054 continue;
2055 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002056 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002057 exit_tryhelp(2);
2058
2059 default:
2060 /* FIXME: This scheme doesn't allow two of the same
2061 matches --RR */
2062 if (!target
2063 || !(target->parse(c - target->option_offset,
2064 argv, invert,
2065 &target->tflags,
2066 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002067 for (matchp = matches; matchp; matchp = matchp->next) {
2068 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002069 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002070 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002071 &fw,
2072 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002073 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 break;
2075 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002076 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002077
2078 /* If you listen carefully, you can
2079 actually hear this code suck. */
2080
2081 /* some explanations (after four different bugs
2082 * in 3 different releases): If we encountere a
2083 * parameter, that has not been parsed yet,
2084 * it's not an option of an explicitly loaded
2085 * match or a target. However, we support
2086 * implicit loading of the protocol match
2087 * extension. '-p tcp' means 'l4 proto 6' and
2088 * at the same time 'load tcp protocol match on
2089 * demand if we specify --dport'.
2090 *
2091 * To make this work, we need to make sure:
2092 * - the parameter has not been parsed by
2093 * a match (m above)
2094 * - a protocol has been specified
2095 * - the protocol extension has not been
2096 * loaded yet, or is loaded and unused
2097 * [think of iptables-restore!]
2098 * - the protocol extension can be successively
2099 * loaded
2100 */
2101 if (m == NULL
2102 && protocol
2103 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002104 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002105 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002106 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002107 && (proto_used == 0))
2108 )
2109 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002110 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002111 /* Try loading protocol */
2112 size_t size;
2113
2114 proto_used = 1;
2115
2116 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2117 + m->size;
2118
2119 m->m = fw_calloc(1, size);
2120 m->m->u.match_size = size;
2121 strcpy(m->m->u.user.name, m->name);
2122 m->init(m->m, &fw.nfcache);
2123
2124 opts = merge_options(opts,
2125 m->extra_opts, &m->option_offset);
2126
2127 optind--;
2128 continue;
2129 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002130 if (!m)
2131 exit_error(PARAMETER_PROBLEM,
2132 "Unknown arg `%s'",
2133 argv[optind-1]);
2134 }
2135 }
2136 invert = FALSE;
2137 }
2138
Martin Josefsson78cafda2004-02-02 20:01:18 +00002139 for (matchp = matches; matchp; matchp = matchp->next)
2140 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002141
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 if (target)
2143 target->final_check(target->tflags);
2144
2145 /* Fix me: must put inverse options checking here --MN */
2146
2147 if (optind < argc)
2148 exit_error(PARAMETER_PROBLEM,
2149 "unknown arguments found on commandline");
2150 if (!command)
2151 exit_error(PARAMETER_PROBLEM, "no command specified");
2152 if (invert)
2153 exit_error(PARAMETER_PROBLEM,
2154 "nothing appropriate following !");
2155
Harald Welte6336bfd2002-05-07 14:41:43 +00002156 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002157 if (!(options & OPT_DESTINATION))
2158 dhostnetworkmask = "0.0.0.0/0";
2159 if (!(options & OPT_SOURCE))
2160 shostnetworkmask = "0.0.0.0/0";
2161 }
2162
2163 if (shostnetworkmask)
2164 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2165 &(fw.ip.smsk), &nsaddrs);
2166
2167 if (dhostnetworkmask)
2168 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2169 &(fw.ip.dmsk), &ndaddrs);
2170
2171 if ((nsaddrs > 1 || ndaddrs > 1) &&
2172 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2173 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2174 " source or destination IP addresses");
2175
Marc Bouchere6869a82000-03-20 06:03:29 +00002176 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2177 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2178 "specify a unique address");
2179
2180 generic_opt_check(command, options);
2181
2182 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2183 exit_error(PARAMETER_PROBLEM,
2184 "chain name `%s' too long (must be under %i chars)",
2185 chain, IPT_FUNCTION_MAXNAMELEN);
2186
Harald Welteae1ff9f2000-12-01 14:26:20 +00002187 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002188 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002189 *handle = iptc_init(*table);
2190
Rusty Russell8beb0492004-12-22 00:37:10 +00002191 /* try to insmod the module if iptc_init failed */
2192 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002193 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002194
Marc Bouchere6869a82000-03-20 06:03:29 +00002195 if (!*handle)
2196 exit_error(VERSION_PROBLEM,
2197 "can't initialize iptables table `%s': %s",
2198 *table, iptc_strerror(errno));
2199
Harald Welte6336bfd2002-05-07 14:41:43 +00002200 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002201 || command == CMD_DELETE
2202 || command == CMD_INSERT
2203 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002204 if (strcmp(chain, "PREROUTING") == 0
2205 || strcmp(chain, "INPUT") == 0) {
2206 /* -o not valid with incoming packets. */
2207 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002208 exit_error(PARAMETER_PROBLEM,
2209 "Can't use -%c with %s\n",
2210 opt2char(OPT_VIANAMEOUT),
2211 chain);
2212 }
2213
Rusty Russella4860fd2000-06-17 16:13:02 +00002214 if (strcmp(chain, "POSTROUTING") == 0
2215 || strcmp(chain, "OUTPUT") == 0) {
2216 /* -i not valid with outgoing packets */
2217 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002218 exit_error(PARAMETER_PROBLEM,
2219 "Can't use -%c with %s\n",
2220 opt2char(OPT_VIANAMEIN),
2221 chain);
2222 }
2223
2224 if (target && iptc_is_chain(jumpto, *handle)) {
2225 printf("Warning: using chain %s, not extension\n",
2226 jumpto);
2227
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002228 if (target->t)
2229 free(target->t);
2230
Marc Bouchere6869a82000-03-20 06:03:29 +00002231 target = NULL;
2232 }
2233
2234 /* If they didn't specify a target, or it's a chain
2235 name, use standard. */
2236 if (!target
2237 && (strlen(jumpto) == 0
2238 || iptc_is_chain(jumpto, *handle))) {
2239 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002240
Rusty Russell52a51492000-05-02 16:44:29 +00002241 target = find_target(IPT_STANDARD_TARGET,
2242 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002243
2244 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002245 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002246 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002247 target->t->u.target_size = size;
2248 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002249 target->init(target->t, &fw.nfcache);
2250 }
2251
Rusty Russell7e53bf92000-03-20 07:03:28 +00002252 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002253 /* it is no chain, and we can't load a plugin.
2254 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002255 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002256 * chain. */
2257 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002258 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002259 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002260 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002261 }
2262 }
2263
2264 switch (command) {
2265 case CMD_APPEND:
2266 ret = append_entry(chain, e,
2267 nsaddrs, saddrs, ndaddrs, daddrs,
2268 options&OPT_VERBOSE,
2269 handle);
2270 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002271 case CMD_DELETE:
2272 ret = delete_entry(chain, e,
2273 nsaddrs, saddrs, ndaddrs, daddrs,
2274 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002275 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002276 break;
2277 case CMD_DELETE_NUM:
2278 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2279 break;
2280 case CMD_REPLACE:
2281 ret = replace_entry(chain, e, rulenum - 1,
2282 saddrs, daddrs, options&OPT_VERBOSE,
2283 handle);
2284 break;
2285 case CMD_INSERT:
2286 ret = insert_entry(chain, e, rulenum - 1,
2287 nsaddrs, saddrs, ndaddrs, daddrs,
2288 options&OPT_VERBOSE,
2289 handle);
2290 break;
2291 case CMD_LIST:
2292 ret = list_entries(chain,
2293 options&OPT_VERBOSE,
2294 options&OPT_NUMERIC,
2295 options&OPT_EXPANDED,
2296 options&OPT_LINENUMBERS,
2297 handle);
2298 break;
2299 case CMD_FLUSH:
2300 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2301 break;
2302 case CMD_ZERO:
2303 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2304 break;
2305 case CMD_LIST|CMD_ZERO:
2306 ret = list_entries(chain,
2307 options&OPT_VERBOSE,
2308 options&OPT_NUMERIC,
2309 options&OPT_EXPANDED,
2310 options&OPT_LINENUMBERS,
2311 handle);
2312 if (ret)
2313 ret = zero_entries(chain,
2314 options&OPT_VERBOSE, handle);
2315 break;
2316 case CMD_NEW_CHAIN:
2317 ret = iptc_create_chain(chain, handle);
2318 break;
2319 case CMD_DELETE_CHAIN:
2320 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2321 break;
2322 case CMD_RENAME_CHAIN:
2323 ret = iptc_rename_chain(chain, newname, handle);
2324 break;
2325 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002326 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002327 break;
2328 default:
2329 /* We should never reach this... */
2330 exit_tryhelp(2);
2331 }
2332
2333 if (verbose > 1)
2334 dump_entries(*handle);
2335
Martin Josefsson78cafda2004-02-02 20:01:18 +00002336 clear_rule_matches(&matches);
2337
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002338 if (e != NULL) {
2339 free(e);
2340 e = NULL;
2341 }
2342
keso6997cdf2004-07-04 15:20:53 +00002343 free(saddrs);
2344 free(daddrs);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002345
2346 if (opts != original_opts) {
2347 free(opts);
2348 opts = original_opts;
2349 global_option_offset = 0;
2350 }
2351
Marc Bouchere6869a82000-03-20 06:03:29 +00002352 return ret;
2353}