blob: 33ee337e6a8172b498a9c9c7461d83003e2a002d [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
Harald Welte82dd2ec2000-12-19 05:18:15 +000050#ifndef PROC_SYS_MODPROBE
51#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
52#endif
53
Marc Bouchere6869a82000-03-20 06:03:29 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
81#define CMD_CHECK 0x0800U
82#define CMD_RENAME_CHAIN 0x1000U
83#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000085 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000086
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_FRAGMENT 0x00200U
100#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000101#define OPT_COUNTERS 0x00800U
102#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000103static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000104= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000105
106static struct option original_opts[] = {
107 { "append", 1, 0, 'A' },
108 { "delete", 1, 0, 'D' },
109 { "insert", 1, 0, 'I' },
110 { "replace", 1, 0, 'R' },
111 { "list", 2, 0, 'L' },
112 { "flush", 2, 0, 'F' },
113 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000116 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000122 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "fragments", 0, 0, 'f' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000135 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000136 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000137 { 0 }
138};
139
Illes Marci63e90632003-03-03 08:08:37 +0000140/* we need this for iptables-restore. iptables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. iptables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000194char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195
Rusty Russell2e0a3212000-04-19 11:23:18 +0000196/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000197struct iptables_match *iptables_matches = NULL;
198struct iptables_target *iptables_targets = NULL;
199
200/* Extra debugging from libiptc */
201extern void dump_entries(const iptc_handle_t handle);
202
203/* A few hardcoded protocols for 'all' and in case the user has no
204 /etc/protocols */
205struct pprot {
206 char *name;
207 u_int8_t num;
208};
209
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000210/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000211/* defined in netinet/in.h */
212#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000213#ifndef IPPROTO_ESP
214#define IPPROTO_ESP 50
215#endif
216#ifndef IPPROTO_AH
217#define IPPROTO_AH 51
218#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000219#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000220
Marc Bouchere6869a82000-03-20 06:03:29 +0000221static const struct pprot chain_protos[] = {
222 { "tcp", IPPROTO_TCP },
223 { "udp", IPPROTO_UDP },
224 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000225 { "esp", IPPROTO_ESP },
226 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000227 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 { "all", 0 },
229};
230
231static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000232proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000233{
234 unsigned int i;
235
Rusty Russell28381a42000-05-10 00:19:50 +0000236 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000237 struct protoent *pent = getprotobynumber(proto);
238 if (pent)
239 return pent->p_name;
240 }
241
242 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
243 if (chain_protos[i].num == proto)
244 return chain_protos[i].name;
245
246 return NULL;
247}
248
249struct in_addr *
250dotted_to_addr(const char *dotted)
251{
252 static struct in_addr addr;
253 unsigned char *addrp;
254 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000255 unsigned int onebyte;
256 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000257 char buf[20];
258
259 /* copy dotted string, because we need to modify it */
260 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000261 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000262 addrp = (unsigned char *) &(addr.s_addr);
263
264 p = buf;
265 for (i = 0; i < 3; i++) {
266 if ((q = strchr(p, '.')) == NULL)
267 return (struct in_addr *) NULL;
268
269 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000270 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000271 return (struct in_addr *) NULL;
272
273 addrp[i] = (unsigned char) onebyte;
274 p = q + 1;
275 }
276
277 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000278 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000279 return (struct in_addr *) NULL;
280
281 addrp[3] = (unsigned char) onebyte;
282
283 return &addr;
284}
285
286static struct in_addr *
287network_to_addr(const char *name)
288{
289 struct netent *net;
290 static struct in_addr addr;
291
292 if ((net = getnetbyname(name)) != NULL) {
293 if (net->n_addrtype != AF_INET)
294 return (struct in_addr *) NULL;
295 addr.s_addr = htonl((unsigned long) net->n_net);
296 return &addr;
297 }
298
299 return (struct in_addr *) NULL;
300}
301
302static void
303inaddrcpy(struct in_addr *dst, struct in_addr *src)
304{
305 /* memcpy(dst, src, sizeof(struct in_addr)); */
306 dst->s_addr = src->s_addr;
307}
308
Pablo Neiradfdcd642005-05-29 19:05:23 +0000309static void free_opts(int reset_offset)
310{
311 if (opts != original_opts) {
312 free(opts);
313 opts = original_opts;
314 if (reset_offset)
315 global_option_offset = 0;
316 }
317}
318
Marc Bouchere6869a82000-03-20 06:03:29 +0000319void
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");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000334 /* On error paths, make sure that we don't leak memory */
335 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000336 exit(status);
337}
338
339void
340exit_tryhelp(int status)
341{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000342 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000343 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000344 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
345 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000346 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 exit(status);
348}
349
350void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000352{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000353 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000354 struct iptables_target *t = NULL;
355
Marc Bouchere6869a82000-03-20 06:03:29 +0000356 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000357"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000358" %s -[RI] chain rulenum rule-specification [options]\n"
359" %s -D chain rulenum [options]\n"
360" %s -[LFZ] [chain] [options]\n"
361" %s -[NX] chain\n"
362" %s -E old-chain-name new-chain-name\n"
363" %s -P chain target [options]\n"
364" %s -h (print this help information)\n\n",
365 program_name, program_version, program_name, program_name,
366 program_name, program_name, program_name, program_name,
367 program_name, program_name);
368
369 printf(
370"Commands:\n"
371"Either long or short options are allowed.\n"
372" --append -A chain Append to chain\n"
373" --delete -D chain Delete matching rule from chain\n"
374" --delete -D chain rulenum\n"
375" Delete rule rulenum (1 = first) from chain\n"
376" --insert -I chain [rulenum]\n"
377" Insert in chain as rulenum (default 1=first)\n"
378" --replace -R chain rulenum\n"
379" Replace rule rulenum (1 = first) in chain\n"
380" --list -L [chain] List the rules in a chain or all chains\n"
381" --flush -F [chain] Delete all rules in chain or all chains\n"
382" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000383" --new -N chain Create a new user-defined chain\n"
384" --delete-chain\n"
385" -X [chain] Delete a user-defined chain\n"
386" --policy -P chain target\n"
387" Change policy on chain to target\n"
388" --rename-chain\n"
389" -E old-chain new-chain\n"
390" Change chain name, (moving any references)\n"
391
392"Options:\n"
393" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
394" --source -s [!] address[/mask]\n"
395" source specification\n"
396" --destination -d [!] address[/mask]\n"
397" destination specification\n"
398" --in-interface -i [!] input name[+]\n"
399" network interface name ([+] for wildcard)\n"
400" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000401" target for rule (may load target extension)\n"
402" --match -m match\n"
403" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000404" --numeric -n numeric output of addresses and ports\n"
405" --out-interface -o [!] output name[+]\n"
406" network interface name ([+] for wildcard)\n"
407" --table -t table table to manipulate (default: `filter')\n"
408" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000409" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000410" --exact -x expand numbers (display exact values)\n"
411"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000412" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000413" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000414"[!] --version -V print package version.\n");
415
Rusty Russell363112d2000-08-11 13:49:26 +0000416 /* Print out any special helps. A user might like to be able
417 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000418 results. So we call help for all specified matches & targets */
419 for (t = iptables_targets; t ;t = t->next) {
420 if (t->used) {
421 printf("\n");
422 t->help();
423 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000424 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000425 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000426 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000427 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000428 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000429 exit(0);
430}
431
432static void
433generic_opt_check(int command, int options)
434{
435 int i, j, legal = 0;
436
437 /* Check that commands are valid with options. Complicated by the
438 * fact that if an option is legal with *any* command given, it is
439 * legal overall (ie. -z and -l).
440 */
441 for (i = 0; i < NUMBER_OF_OPT; i++) {
442 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
443
444 for (j = 0; j < NUMBER_OF_CMD; j++) {
445 if (!(command & (1<<j)))
446 continue;
447
448 if (!(options & (1<<i))) {
449 if (commands_v_options[j][i] == '+')
450 exit_error(PARAMETER_PROBLEM,
451 "You need to supply the `-%c' "
452 "option for this command\n",
453 optflags[i]);
454 } else {
455 if (commands_v_options[j][i] != 'x')
456 legal = 1;
457 else if (legal == 0)
458 legal = -1;
459 }
460 }
461 if (legal == -1)
462 exit_error(PARAMETER_PROBLEM,
463 "Illegal option `-%c' with this command\n",
464 optflags[i]);
465 }
466}
467
468static char
469opt2char(int option)
470{
471 const char *ptr;
472 for (ptr = optflags; option > 1; option >>= 1, ptr++);
473
474 return *ptr;
475}
476
477static char
478cmd2char(int option)
479{
480 const char *ptr;
481 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
482
483 return *ptr;
484}
485
486static void
487add_command(int *cmd, const int newcmd, const int othercmds, int invert)
488{
489 if (invert)
490 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
491 if (*cmd & (~othercmds))
492 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
493 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
494 *cmd |= newcmd;
495}
496
497int
Harald Welteb77f1da2002-03-14 11:35:58 +0000498check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000499{
500 if (option && strcmp(option, "!") == 0) {
501 if (*invert)
502 exit_error(PARAMETER_PROBLEM,
503 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000504 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000505 if (optind) {
506 *optind = *optind+1;
507 if (argc && *optind > argc)
508 exit_error(PARAMETER_PROBLEM,
509 "no argument following `!'");
510 }
511
Marc Bouchere6869a82000-03-20 06:03:29 +0000512 return TRUE;
513 }
514 return FALSE;
515}
516
517static void *
518fw_calloc(size_t count, size_t size)
519{
520 void *p;
521
522 if ((p = calloc(count, size)) == NULL) {
523 perror("iptables: calloc failed");
524 exit(1);
525 }
526 return p;
527}
528
529static void *
530fw_malloc(size_t size)
531{
532 void *p;
533
534 if ((p = malloc(size)) == NULL) {
535 perror("iptables: malloc failed");
536 exit(1);
537 }
538 return p;
539}
540
541static struct in_addr *
542host_to_addr(const char *name, unsigned int *naddr)
543{
544 struct hostent *host;
545 struct in_addr *addr;
546 unsigned int i;
547
548 *naddr = 0;
549 if ((host = gethostbyname(name)) != NULL) {
550 if (host->h_addrtype != AF_INET ||
551 host->h_length != sizeof(struct in_addr))
552 return (struct in_addr *) NULL;
553
554 while (host->h_addr_list[*naddr] != (char *) NULL)
555 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000556 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000557 for (i = 0; i < *naddr; i++)
558 inaddrcpy(&(addr[i]),
559 (struct in_addr *) host->h_addr_list[i]);
560 return addr;
561 }
562
563 return (struct in_addr *) NULL;
564}
565
566static char *
567addr_to_host(const struct in_addr *addr)
568{
569 struct hostent *host;
570
571 if ((host = gethostbyaddr((char *) addr,
572 sizeof(struct in_addr), AF_INET)) != NULL)
573 return (char *) host->h_name;
574
575 return (char *) NULL;
576}
577
578/*
579 * All functions starting with "parse" should succeed, otherwise
580 * the program fails.
581 * Most routines return pointers to static data that may change
582 * between calls to the same or other routines with a few exceptions:
583 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
584 * return global static data.
585*/
586
587static struct in_addr *
588parse_hostnetwork(const char *name, unsigned int *naddrs)
589{
590 struct in_addr *addrp, *addrptmp;
591
592 if ((addrptmp = dotted_to_addr(name)) != NULL ||
593 (addrptmp = network_to_addr(name)) != NULL) {
594 addrp = fw_malloc(sizeof(struct in_addr));
595 inaddrcpy(addrp, addrptmp);
596 *naddrs = 1;
597 return addrp;
598 }
599 if ((addrp = host_to_addr(name, naddrs)) != NULL)
600 return addrp;
601
602 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
603}
604
605static struct in_addr *
606parse_mask(char *mask)
607{
608 static struct in_addr maskaddr;
609 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000610 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000611
612 if (mask == NULL) {
613 /* no mask at all defaults to 32 bits */
614 maskaddr.s_addr = 0xFFFFFFFF;
615 return &maskaddr;
616 }
617 if ((addrp = dotted_to_addr(mask)) != NULL)
618 /* dotted_to_addr already returns a network byte order addr */
619 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000620 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000621 exit_error(PARAMETER_PROBLEM,
622 "invalid mask `%s' specified", mask);
623 if (bits != 0) {
624 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
625 return &maskaddr;
626 }
627
628 maskaddr.s_addr = 0L;
629 return &maskaddr;
630}
631
Marc Boucherb93c7982001-12-06 14:50:19 +0000632void
Marc Bouchere6869a82000-03-20 06:03:29 +0000633parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
634 struct in_addr *maskp, unsigned int *naddrs)
635{
636 struct in_addr *addrp;
637 char buf[256];
638 char *p;
639 int i, j, k, n;
640
641 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000642 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000643 if ((p = strrchr(buf, '/')) != NULL) {
644 *p = '\0';
645 addrp = parse_mask(p + 1);
646 } else
647 addrp = parse_mask(NULL);
648 inaddrcpy(maskp, addrp);
649
650 /* if a null mask is given, the name is ignored, like in "any/0" */
651 if (maskp->s_addr == 0L)
652 strcpy(buf, "0.0.0.0");
653
654 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
655 n = *naddrs;
656 for (i = 0, j = 0; i < n; i++) {
657 addrp[j++].s_addr &= maskp->s_addr;
658 for (k = 0; k < j - 1; k++) {
659 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
660 (*naddrs)--;
661 j--;
662 break;
663 }
664 }
665 }
666}
667
668struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000669find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000670{
671 struct iptables_match *ptr;
672
673 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
674 if (strcmp(name, ptr->name) == 0)
675 break;
676 }
677
Harald Welte3efb6ea2001-08-06 18:50:21 +0000678#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000679 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000680 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000681 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000682 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000683 if (dlopen(path, RTLD_NOW)) {
684 /* Found library. If it didn't register itself,
685 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000686 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000687
Rusty Russell9e1d2142000-04-23 09:11:12 +0000688 if (!ptr)
689 exit_error(PARAMETER_PROBLEM,
690 "Couldn't load match `%s'\n",
691 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000692 } else if (tryload == LOAD_MUST_SUCCEED)
693 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000694 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000695 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000696 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000697#else
698 if (ptr && !ptr->loaded) {
699 if (tryload != DONT_LOAD)
700 ptr->loaded = 1;
701 else
702 ptr = NULL;
703 }
Marc Boucher067477b2002-03-24 15:09:31 +0000704 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
705 exit_error(PARAMETER_PROBLEM,
706 "Couldn't find match `%s'\n", name);
707 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000708#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000709
Martin Josefsson78cafda2004-02-02 20:01:18 +0000710 if (ptr && matches) {
711 struct iptables_rule_match **i;
712 struct iptables_rule_match *newentry;
713
714 newentry = fw_malloc(sizeof(struct iptables_rule_match));
715
716 for (i = matches; *i; i = &(*i)->next);
717 newentry->match = ptr;
718 newentry->next = NULL;
719 *i = newentry;
720 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000721
Marc Bouchere6869a82000-03-20 06:03:29 +0000722 return ptr;
723}
724
Rusty Russell28381a42000-05-10 00:19:50 +0000725/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
726static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000727find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000728{
Harald Welteed498492001-07-23 01:24:22 +0000729 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000730
Harald Welte0b0013a2002-02-18 16:15:31 +0000731 if (string_to_number(pname, 0, 255, &proto) != -1) {
732 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000733
Harald Welte0b0013a2002-02-18 16:15:31 +0000734 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000735 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000736 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000737 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000738
739 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000740}
741
Marc Boucherb93c7982001-12-06 14:50:19 +0000742u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000743parse_protocol(const char *s)
744{
Harald Welteed498492001-07-23 01:24:22 +0000745 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
Harald Welteed498492001-07-23 01:24:22 +0000747 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000748 struct protoent *pent;
749
750 if ((pent = getprotobyname(s)))
751 proto = pent->p_proto;
752 else {
753 unsigned int i;
754 for (i = 0;
755 i < sizeof(chain_protos)/sizeof(struct pprot);
756 i++) {
757 if (strcmp(s, chain_protos[i].name) == 0) {
758 proto = chain_protos[i].num;
759 break;
760 }
761 }
762 if (i == sizeof(chain_protos)/sizeof(struct pprot))
763 exit_error(PARAMETER_PROBLEM,
764 "unknown protocol `%s' specified",
765 s);
766 }
767 }
768
769 return (u_int16_t)proto;
770}
771
772static void
773parse_interface(const char *arg, char *vianame, unsigned char *mask)
774{
775 int vialen = strlen(arg);
776 unsigned int i;
777
778 memset(mask, 0, IFNAMSIZ);
779 memset(vianame, 0, IFNAMSIZ);
780
781 if (vialen + 1 > IFNAMSIZ)
782 exit_error(PARAMETER_PROBLEM,
783 "interface name `%s' must be shorter than IFNAMSIZ"
784 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000785
Marc Bouchere6869a82000-03-20 06:03:29 +0000786 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000787 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000788 memset(mask, 0, IFNAMSIZ);
789 else if (vianame[vialen - 1] == '+') {
790 memset(mask, 0xFF, vialen - 1);
791 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000792 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000793 } else {
794 /* Include nul-terminator in match */
795 memset(mask, 0xFF, vialen + 1);
796 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000797 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000798 if (!isalnum(vianame[i])
799 && vianame[i] != '_'
800 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000801 printf("Warning: wierd character in interface"
802 " `%s' (No aliases, :, ! or *).\n",
803 vianame);
804 break;
805 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000806 }
807 }
808}
809
810/* Can't be zero. */
811static int
812parse_rulenumber(const char *rule)
813{
Harald Welteed498492001-07-23 01:24:22 +0000814 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000815
Harald Welteed498492001-07-23 01:24:22 +0000816 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000817 exit_error(PARAMETER_PROBLEM,
818 "Invalid rule number `%s'", rule);
819
820 return rulenum;
821}
822
823static const char *
824parse_target(const char *targetname)
825{
826 const char *ptr;
827
828 if (strlen(targetname) < 1)
829 exit_error(PARAMETER_PROBLEM,
830 "Invalid target name (too short)");
831
832 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
833 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000834 "Invalid target name `%s' (%u chars max)",
835 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000836
837 for (ptr = targetname; *ptr; ptr++)
838 if (isspace(*ptr))
839 exit_error(PARAMETER_PROBLEM,
840 "Invalid target name `%s'", targetname);
841 return targetname;
842}
843
844static char *
845addr_to_network(const struct in_addr *addr)
846{
847 struct netent *net;
848
849 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
850 return (char *) net->n_name;
851
852 return (char *) NULL;
853}
854
855char *
856addr_to_dotted(const struct in_addr *addrp)
857{
858 static char buf[20];
859 const unsigned char *bytep;
860
861 bytep = (const unsigned char *) &(addrp->s_addr);
862 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
863 return buf;
864}
Marc Boucherb93c7982001-12-06 14:50:19 +0000865
866char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000867addr_to_anyname(const struct in_addr *addr)
868{
869 char *name;
870
871 if ((name = addr_to_host(addr)) != NULL ||
872 (name = addr_to_network(addr)) != NULL)
873 return name;
874
875 return addr_to_dotted(addr);
876}
877
Marc Boucherb93c7982001-12-06 14:50:19 +0000878char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000879mask_to_dotted(const struct in_addr *mask)
880{
881 int i;
882 static char buf[20];
883 u_int32_t maskaddr, bits;
884
885 maskaddr = ntohl(mask->s_addr);
886
887 if (maskaddr == 0xFFFFFFFFL)
888 /* we don't want to see "/32" */
889 return "";
890
891 i = 32;
892 bits = 0xFFFFFFFEL;
893 while (--i >= 0 && maskaddr != bits)
894 bits <<= 1;
895 if (i >= 0)
896 sprintf(buf, "/%d", i);
897 else
898 /* mask was not a decent combination of 1's and 0's */
899 sprintf(buf, "/%s", addr_to_dotted(mask));
900
901 return buf;
902}
903
904int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000905string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
906 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000907{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000909 char *end;
910
911 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000912 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000913 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000914 if (*end == '\0' && end != s) {
915 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000917 *ret = number;
918 return 0;
919 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000920 }
921 return -1;
922}
923
Martin Josefssonb105bc92004-05-26 15:54:49 +0000924int
925string_to_number_l(const char *s, unsigned long min, unsigned long max,
926 unsigned long *ret)
927{
928 int result;
929 unsigned long long number;
930
931 result = string_to_number_ll(s, min, max, &number);
932 *ret = (unsigned long)number;
933
934 return result;
935}
936
937int string_to_number(const char *s, unsigned int min, unsigned int max,
938 unsigned int *ret)
939{
940 int result;
941 unsigned long number;
942
943 result = string_to_number_l(s, min, max, &number);
944 *ret = (unsigned int)number;
945
946 return result;
947}
948
Marc Bouchere6869a82000-03-20 06:03:29 +0000949static void
950set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
951 int invert)
952{
953 if (*options & option)
954 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
955 opt2char(option));
956 *options |= option;
957
958 if (invert) {
959 unsigned int i;
960 for (i = 0; 1 << i != option; i++);
961
962 if (!inverse_for_options[i])
963 exit_error(PARAMETER_PROBLEM,
964 "cannot have ! before -%c",
965 opt2char(option));
966 *invflg |= inverse_for_options[i];
967 }
968}
969
970struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000971find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000972{
973 struct iptables_target *ptr;
974
975 /* Standard target? */
976 if (strcmp(name, "") == 0
977 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
978 || strcmp(name, IPTC_LABEL_DROP) == 0
979 || strcmp(name, IPTC_LABEL_QUEUE) == 0
980 || strcmp(name, IPTC_LABEL_RETURN) == 0)
981 name = "standard";
982
983 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
984 if (strcmp(name, ptr->name) == 0)
985 break;
986 }
987
Harald Welte3efb6ea2001-08-06 18:50:21 +0000988#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000989 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000990 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000991 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000992 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000993 if (dlopen(path, RTLD_NOW)) {
994 /* Found library. If it didn't register itself,
995 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000996 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000997 if (!ptr)
998 exit_error(PARAMETER_PROBLEM,
999 "Couldn't load target `%s'\n",
1000 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001001 } else if (tryload == LOAD_MUST_SUCCEED)
1002 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001003 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001004 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001006#else
1007 if (ptr && !ptr->loaded) {
1008 if (tryload != DONT_LOAD)
1009 ptr->loaded = 1;
1010 else
1011 ptr = NULL;
1012 }
Marc Boucher067477b2002-03-24 15:09:31 +00001013 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1014 exit_error(PARAMETER_PROBLEM,
1015 "Couldn't find target `%s'\n", name);
1016 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001017#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001018
Harald Welteae1ff9f2000-12-01 14:26:20 +00001019 if (ptr)
1020 ptr->used = 1;
1021
Marc Bouchere6869a82000-03-20 06:03:29 +00001022 return ptr;
1023}
1024
1025static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001026merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001027 unsigned int *option_offset)
1028{
1029 unsigned int num_old, num_new, i;
1030 struct option *merge;
1031
Pablo Neiradfdcd642005-05-29 19:05:23 +00001032 /* Release previous options merged if any */
1033 free_opts(0);
1034
Marc Bouchere6869a82000-03-20 06:03:29 +00001035 for (num_old = 0; oldopts[num_old].name; num_old++);
1036 for (num_new = 0; newopts[num_new].name; num_new++);
1037
1038 global_option_offset += OPTION_OFFSET;
1039 *option_offset = global_option_offset;
1040
1041 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1042 memcpy(merge, oldopts, num_old * sizeof(struct option));
1043 for (i = 0; i < num_new; i++) {
1044 merge[num_old + i] = newopts[i];
1045 merge[num_old + i].val += *option_offset;
1046 }
1047 memset(merge + num_old + num_new, 0, sizeof(struct option));
1048
1049 return merge;
1050}
1051
Rusty Russell3aef54d2005-01-03 03:48:40 +00001052static int compatible_revision(const char *name, u_int8_t revision, int opt)
1053{
1054 struct ipt_get_revision rev;
1055 socklen_t s = sizeof(rev);
1056 int max_rev, sockfd;
1057
1058 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1059 if (sockfd < 0) {
1060 fprintf(stderr, "Could not open socket to kernel: %s\n",
1061 strerror(errno));
1062 exit(1);
1063 }
1064
1065 strcpy(rev.name, name);
1066 rev.revision = revision;
1067
1068 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1069 if (max_rev < 0) {
1070 /* Definitely don't support this? */
1071 if (errno == EPROTONOSUPPORT) {
1072 close(sockfd);
1073 return 0;
1074 } else if (errno == ENOPROTOOPT) {
1075 close(sockfd);
1076 /* Assume only revision 0 support (old kernel) */
1077 return (revision == 0);
1078 } else {
1079 fprintf(stderr, "getsockopt failed strangely: %s\n",
1080 strerror(errno));
1081 exit(1);
1082 }
1083 }
1084 close(sockfd);
1085 return 1;
1086}
1087
1088static int compatible_match_revision(const char *name, u_int8_t revision)
1089{
1090 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1091}
1092
1093static int compatible_target_revision(const char *name, u_int8_t revision)
1094{
1095 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1096}
1097
Marc Bouchere6869a82000-03-20 06:03:29 +00001098void
1099register_match(struct iptables_match *me)
1100{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001101 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001102
Marc Bouchere6869a82000-03-20 06:03:29 +00001103 if (strcmp(me->version, program_version) != 0) {
1104 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1105 program_name, me->name, me->version, program_version);
1106 exit(1);
1107 }
1108
Martin Josefsson93911bf2005-01-03 07:46:07 +00001109 /* Revision field stole a char from name. */
1110 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001111 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001112 program_name, me->name);
1113 exit(1);
1114 }
1115
Rusty Russell3aef54d2005-01-03 03:48:40 +00001116 old = find_match(me->name, DONT_LOAD, NULL);
1117 if (old) {
1118 if (old->revision == me->revision) {
1119 fprintf(stderr,
1120 "%s: match `%s' already registered.\n",
1121 program_name, me->name);
1122 exit(1);
1123 }
1124
1125 /* Now we have two (or more) options, check compatibility. */
1126 if (compatible_match_revision(old->name, old->revision)
1127 && old->revision > me->revision)
1128 return;
1129
1130 /* Replace if compatible. */
1131 if (!compatible_match_revision(me->name, me->revision))
1132 return;
1133
1134 /* Delete old one. */
1135 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1136 *i = old->next;
1137 }
1138
Rusty Russell73f72f52000-07-03 10:17:57 +00001139 if (me->size != IPT_ALIGN(me->size)) {
1140 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001141 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001142 exit(1);
1143 }
1144
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001145 /* Append to list. */
1146 for (i = &iptables_matches; *i; i = &(*i)->next);
1147 me->next = NULL;
1148 *i = me;
1149
Marc Bouchere6869a82000-03-20 06:03:29 +00001150 me->m = NULL;
1151 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001152}
1153
1154void
1155register_target(struct iptables_target *me)
1156{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001157 struct iptables_target *old;
1158
Marc Bouchere6869a82000-03-20 06:03:29 +00001159 if (strcmp(me->version, program_version) != 0) {
1160 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1161 program_name, me->name, me->version, program_version);
1162 exit(1);
1163 }
1164
Martin Josefsson93911bf2005-01-03 07:46:07 +00001165 /* Revision field stole a char from name. */
1166 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001167 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001168 program_name, me->name);
1169 exit(1);
1170 }
1171
Rusty Russell3aef54d2005-01-03 03:48:40 +00001172 old = find_target(me->name, DONT_LOAD);
1173 if (old) {
1174 struct iptables_target **i;
1175
1176 if (old->revision == me->revision) {
1177 fprintf(stderr,
1178 "%s: target `%s' already registered.\n",
1179 program_name, me->name);
1180 exit(1);
1181 }
1182
Rusty Russell3aef54d2005-01-03 03:48:40 +00001183 /* Now we have two (or more) options, check compatibility. */
1184 if (compatible_target_revision(old->name, old->revision)
1185 && old->revision > me->revision)
1186 return;
1187
1188 /* Replace if compatible. */
1189 if (!compatible_target_revision(me->name, me->revision))
1190 return;
1191
1192 /* Delete old one. */
1193 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1194 *i = old->next;
1195 }
1196
Rusty Russell73f72f52000-07-03 10:17:57 +00001197 if (me->size != IPT_ALIGN(me->size)) {
1198 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001199 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001200 exit(1);
1201 }
1202
Marc Bouchere6869a82000-03-20 06:03:29 +00001203 /* Prepend to list. */
1204 me->next = iptables_targets;
1205 iptables_targets = me;
1206 me->t = NULL;
1207 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001208}
1209
1210static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001211print_num(u_int64_t number, unsigned int format)
1212{
1213 if (format & FMT_KILOMEGAGIGA) {
1214 if (number > 99999) {
1215 number = (number + 500) / 1000;
1216 if (number > 9999) {
1217 number = (number + 500) / 1000;
1218 if (number > 9999) {
1219 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001220 if (number > 9999) {
1221 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001222 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001223 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001224 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001225 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001226 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001227 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001228 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001229 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001231 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001232 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001233}
1234
1235
1236static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001237print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1238{
1239 struct ipt_counters counters;
1240 const char *pol = iptc_get_policy(chain, &counters, handle);
1241 printf("Chain %s", chain);
1242 if (pol) {
1243 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001244 if (!(format & FMT_NOCOUNTS)) {
1245 fputc(' ', stdout);
1246 print_num(counters.pcnt, (format|FMT_NOTABLE));
1247 fputs("packets, ", stdout);
1248 print_num(counters.bcnt, (format|FMT_NOTABLE));
1249 fputs("bytes", stdout);
1250 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001251 printf(")\n");
1252 } else {
1253 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001254 if (!iptc_get_references(&refs, chain, handle))
1255 printf(" (ERROR obtaining refs)\n");
1256 else
1257 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001258 }
1259
1260 if (format & FMT_LINENUMBERS)
1261 printf(FMT("%-4s ", "%s "), "num");
1262 if (!(format & FMT_NOCOUNTS)) {
1263 if (format & FMT_KILOMEGAGIGA) {
1264 printf(FMT("%5s ","%s "), "pkts");
1265 printf(FMT("%5s ","%s "), "bytes");
1266 } else {
1267 printf(FMT("%8s ","%s "), "pkts");
1268 printf(FMT("%10s ","%s "), "bytes");
1269 }
1270 }
1271 if (!(format & FMT_NOTARGET))
1272 printf(FMT("%-9s ","%s "), "target");
1273 fputs(" prot ", stdout);
1274 if (format & FMT_OPTIONS)
1275 fputs("opt", stdout);
1276 if (format & FMT_VIA) {
1277 printf(FMT(" %-6s ","%s "), "in");
1278 printf(FMT("%-6s ","%s "), "out");
1279 }
1280 printf(FMT(" %-19s ","%s "), "source");
1281 printf(FMT(" %-19s "," %s "), "destination");
1282 printf("\n");
1283}
1284
Marc Bouchere6869a82000-03-20 06:03:29 +00001285
1286static int
1287print_match(const struct ipt_entry_match *m,
1288 const struct ipt_ip *ip,
1289 int numeric)
1290{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001291 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001292
1293 if (match) {
1294 if (match->print)
1295 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001296 else
Rusty Russellb039b022000-09-01 06:04:05 +00001297 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001298 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001299 if (m->u.user.name[0])
1300 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001301 }
1302 /* Don't stop iterating. */
1303 return 0;
1304}
1305
1306/* e is called `fw' here for hysterical raisins */
1307static void
1308print_firewall(const struct ipt_entry *fw,
1309 const char *targname,
1310 unsigned int num,
1311 unsigned int format,
1312 const iptc_handle_t handle)
1313{
1314 struct iptables_target *target = NULL;
1315 const struct ipt_entry_target *t;
1316 u_int8_t flags;
1317 char buf[BUFSIZ];
1318
Marc Bouchere6869a82000-03-20 06:03:29 +00001319 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001320 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001321 else
Rusty Russell52a51492000-05-02 16:44:29 +00001322 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001323
1324 t = ipt_get_target((struct ipt_entry *)fw);
1325 flags = fw->ip.flags;
1326
1327 if (format & FMT_LINENUMBERS)
1328 printf(FMT("%-4u ", "%u "), num+1);
1329
1330 if (!(format & FMT_NOCOUNTS)) {
1331 print_num(fw->counters.pcnt, format);
1332 print_num(fw->counters.bcnt, format);
1333 }
1334
1335 if (!(format & FMT_NOTARGET))
1336 printf(FMT("%-9s ", "%s "), targname);
1337
1338 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1339 {
Rusty Russell28381a42000-05-10 00:19:50 +00001340 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 if (pname)
1342 printf(FMT("%-5s", "%s "), pname);
1343 else
1344 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1345 }
1346
1347 if (format & FMT_OPTIONS) {
1348 if (format & FMT_NOTABLE)
1349 fputs("opt ", stdout);
1350 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1351 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1352 fputc(' ', stdout);
1353 }
1354
1355 if (format & FMT_VIA) {
1356 char iface[IFNAMSIZ+2];
1357
1358 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1359 iface[0] = '!';
1360 iface[1] = '\0';
1361 }
1362 else iface[0] = '\0';
1363
1364 if (fw->ip.iniface[0] != '\0') {
1365 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001366 }
1367 else if (format & FMT_NUMERIC) strcat(iface, "*");
1368 else strcat(iface, "any");
1369 printf(FMT(" %-6s ","in %s "), iface);
1370
1371 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1372 iface[0] = '!';
1373 iface[1] = '\0';
1374 }
1375 else iface[0] = '\0';
1376
1377 if (fw->ip.outiface[0] != '\0') {
1378 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001379 }
1380 else if (format & FMT_NUMERIC) strcat(iface, "*");
1381 else strcat(iface, "any");
1382 printf(FMT("%-6s ","out %s "), iface);
1383 }
1384
1385 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1386 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1387 printf(FMT("%-19s ","%s "), "anywhere");
1388 else {
1389 if (format & FMT_NUMERIC)
1390 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1391 else
1392 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1393 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1394 printf(FMT("%-19s ","%s "), buf);
1395 }
1396
1397 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1398 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001399 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001400 else {
1401 if (format & FMT_NUMERIC)
1402 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1403 else
1404 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1405 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001406 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001407 }
1408
1409 if (format & FMT_NOTABLE)
1410 fputs(" ", stdout);
1411
1412 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1413
1414 if (target) {
1415 if (target->print)
1416 /* Print the target information. */
1417 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001418 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001419 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001420 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001421
1422 if (!(format & FMT_NONEWLINE))
1423 fputc('\n', stdout);
1424}
1425
1426static void
1427print_firewall_line(const struct ipt_entry *fw,
1428 const iptc_handle_t h)
1429{
1430 struct ipt_entry_target *t;
1431
1432 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001433 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001434}
1435
1436static int
1437append_entry(const ipt_chainlabel chain,
1438 struct ipt_entry *fw,
1439 unsigned int nsaddrs,
1440 const struct in_addr saddrs[],
1441 unsigned int ndaddrs,
1442 const struct in_addr daddrs[],
1443 int verbose,
1444 iptc_handle_t *handle)
1445{
1446 unsigned int i, j;
1447 int ret = 1;
1448
1449 for (i = 0; i < nsaddrs; i++) {
1450 fw->ip.src.s_addr = saddrs[i].s_addr;
1451 for (j = 0; j < ndaddrs; j++) {
1452 fw->ip.dst.s_addr = daddrs[j].s_addr;
1453 if (verbose)
1454 print_firewall_line(fw, *handle);
1455 ret &= iptc_append_entry(chain, fw, handle);
1456 }
1457 }
1458
1459 return ret;
1460}
1461
1462static int
1463replace_entry(const ipt_chainlabel chain,
1464 struct ipt_entry *fw,
1465 unsigned int rulenum,
1466 const struct in_addr *saddr,
1467 const struct in_addr *daddr,
1468 int verbose,
1469 iptc_handle_t *handle)
1470{
1471 fw->ip.src.s_addr = saddr->s_addr;
1472 fw->ip.dst.s_addr = daddr->s_addr;
1473
1474 if (verbose)
1475 print_firewall_line(fw, *handle);
1476 return iptc_replace_entry(chain, fw, rulenum, handle);
1477}
1478
1479static int
1480insert_entry(const ipt_chainlabel chain,
1481 struct ipt_entry *fw,
1482 unsigned int rulenum,
1483 unsigned int nsaddrs,
1484 const struct in_addr saddrs[],
1485 unsigned int ndaddrs,
1486 const struct in_addr daddrs[],
1487 int verbose,
1488 iptc_handle_t *handle)
1489{
1490 unsigned int i, j;
1491 int ret = 1;
1492
1493 for (i = 0; i < nsaddrs; i++) {
1494 fw->ip.src.s_addr = saddrs[i].s_addr;
1495 for (j = 0; j < ndaddrs; j++) {
1496 fw->ip.dst.s_addr = daddrs[j].s_addr;
1497 if (verbose)
1498 print_firewall_line(fw, *handle);
1499 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1500 }
1501 }
1502
1503 return ret;
1504}
1505
Rusty Russell2e0a3212000-04-19 11:23:18 +00001506static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001507make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001508{
1509 /* Establish mask for comparison */
1510 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001511 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001512 unsigned char *mask, *mptr;
1513
1514 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001515 for (matchp = matches; matchp; matchp = matchp->next)
1516 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001517
Rusty Russell9e1d2142000-04-23 09:11:12 +00001518 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001519 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001520 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001521
Rusty Russell9e1d2142000-04-23 09:11:12 +00001522 memset(mask, 0xFF, sizeof(struct ipt_entry));
1523 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001524
Martin Josefsson78cafda2004-02-02 20:01:18 +00001525 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001526 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001527 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001528 + matchp->match->userspacesize);
1529 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001530 }
1531
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001532 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001533 IPT_ALIGN(sizeof(struct ipt_entry_target))
1534 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001535
1536 return mask;
1537}
1538
Marc Bouchere6869a82000-03-20 06:03:29 +00001539static int
1540delete_entry(const ipt_chainlabel chain,
1541 struct ipt_entry *fw,
1542 unsigned int nsaddrs,
1543 const struct in_addr saddrs[],
1544 unsigned int ndaddrs,
1545 const struct in_addr daddrs[],
1546 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001547 iptc_handle_t *handle,
1548 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001549{
1550 unsigned int i, j;
1551 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001552 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001553
Martin Josefsson78cafda2004-02-02 20:01:18 +00001554 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001555 for (i = 0; i < nsaddrs; i++) {
1556 fw->ip.src.s_addr = saddrs[i].s_addr;
1557 for (j = 0; j < ndaddrs; j++) {
1558 fw->ip.dst.s_addr = daddrs[j].s_addr;
1559 if (verbose)
1560 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001561 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001562 }
1563 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001564 free(mask);
1565
Marc Bouchere6869a82000-03-20 06:03:29 +00001566 return ret;
1567}
1568
Harald Welteae1ff9f2000-12-01 14:26:20 +00001569int
Marc Bouchere6869a82000-03-20 06:03:29 +00001570for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001571 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001572{
1573 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001574 const char *chain;
1575 char *chains;
1576 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001577
Rusty Russell9e1d2142000-04-23 09:11:12 +00001578 chain = iptc_first_chain(handle);
1579 while (chain) {
1580 chaincount++;
1581 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001582 }
1583
Rusty Russell9e1d2142000-04-23 09:11:12 +00001584 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1585 i = 0;
1586 chain = iptc_first_chain(handle);
1587 while (chain) {
1588 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1589 i++;
1590 chain = iptc_next_chain(handle);
1591 }
1592
1593 for (i = 0; i < chaincount; i++) {
1594 if (!builtinstoo
1595 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001596 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001597 continue;
1598 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1599 }
1600
1601 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 return ret;
1603}
1604
Harald Welteae1ff9f2000-12-01 14:26:20 +00001605int
Marc Bouchere6869a82000-03-20 06:03:29 +00001606flush_entries(const ipt_chainlabel chain, int verbose,
1607 iptc_handle_t *handle)
1608{
1609 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001610 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001611
1612 if (verbose)
1613 fprintf(stdout, "Flushing chain `%s'\n", chain);
1614 return iptc_flush_entries(chain, handle);
1615}
Marc Bouchere6869a82000-03-20 06:03:29 +00001616
1617static int
1618zero_entries(const ipt_chainlabel chain, int verbose,
1619 iptc_handle_t *handle)
1620{
1621 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001622 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001623
Marc Bouchere6869a82000-03-20 06:03:29 +00001624 if (verbose)
1625 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1626 return iptc_zero_entries(chain, handle);
1627}
1628
Harald Welteae1ff9f2000-12-01 14:26:20 +00001629int
Marc Bouchere6869a82000-03-20 06:03:29 +00001630delete_chain(const ipt_chainlabel chain, int verbose,
1631 iptc_handle_t *handle)
1632{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001633 if (!chain)
1634 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001635
1636 if (verbose)
1637 fprintf(stdout, "Deleting chain `%s'\n", chain);
1638 return iptc_delete_chain(chain, handle);
1639}
1640
1641static int
1642list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1643 int expanded, int linenumbers, iptc_handle_t *handle)
1644{
1645 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001646 unsigned int format;
1647 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001648
1649 format = FMT_OPTIONS;
1650 if (!verbose)
1651 format |= FMT_NOCOUNTS;
1652 else
1653 format |= FMT_VIA;
1654
1655 if (numeric)
1656 format |= FMT_NUMERIC;
1657
1658 if (!expanded)
1659 format |= FMT_KILOMEGAGIGA;
1660
1661 if (linenumbers)
1662 format |= FMT_LINENUMBERS;
1663
Rusty Russell9e1d2142000-04-23 09:11:12 +00001664 for (this = iptc_first_chain(handle);
1665 this;
1666 this = iptc_next_chain(handle)) {
1667 const struct ipt_entry *i;
1668 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001669
Marc Bouchere6869a82000-03-20 06:03:29 +00001670 if (chain && strcmp(chain, this) != 0)
1671 continue;
1672
1673 if (found) printf("\n");
1674
1675 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001676 i = iptc_first_rule(this, handle);
1677
1678 num = 0;
1679 while (i) {
1680 print_firewall(i,
1681 iptc_get_target(i, handle),
1682 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001683 format,
1684 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001685 i = iptc_next_rule(i, handle);
1686 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001687 found = 1;
1688 }
1689
1690 errno = ENOENT;
1691 return found;
1692}
1693
Harald Welte82dd2ec2000-12-19 05:18:15 +00001694static char *get_modprobe(void)
1695{
1696 int procfile;
1697 char *ret;
1698
Harald Welte10f7f142004-10-22 08:14:07 +00001699#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001700 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1701 if (procfile < 0)
1702 return NULL;
1703
Harald Welte10f7f142004-10-22 08:14:07 +00001704 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001705 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001706 memset(ret, 0, PROCFILE_BUFSIZ);
1707 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001708 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001709 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001710 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001711 if (ret[strlen(ret)-1]=='\n')
1712 ret[strlen(ret)-1]=0;
1713 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001714 return ret;
1715 }
1716 fail:
1717 free(ret);
1718 close(procfile);
1719 return NULL;
1720}
1721
Harald Welte58918652001-06-16 18:25:25 +00001722int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001723{
1724 char *buf = NULL;
1725 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001726 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001727
1728 /* If they don't explicitly set it, read out of kernel */
1729 if (!modprobe) {
1730 buf = get_modprobe();
1731 if (!buf)
1732 return -1;
1733 modprobe = buf;
1734 }
1735
1736 switch (fork()) {
1737 case 0:
1738 argv[0] = (char *)modprobe;
1739 argv[1] = (char *)modname;
1740 argv[2] = NULL;
1741 execv(argv[0], argv);
1742
1743 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001744 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001745 case -1:
1746 return -1;
1747
1748 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001749 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001750 }
1751
1752 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001753 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1754 return 0;
1755 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001756}
1757
Marc Bouchere6869a82000-03-20 06:03:29 +00001758static struct ipt_entry *
1759generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001760 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001761 struct ipt_entry_target *target)
1762{
1763 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001764 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001765 struct ipt_entry *e;
1766
1767 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001768 for (matchp = matches; matchp; matchp = matchp->next)
1769 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001770
Rusty Russell228e98d2000-04-27 10:28:06 +00001771 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001772 *e = *fw;
1773 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001774 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001775
1776 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001777 for (matchp = matches; matchp; matchp = matchp->next) {
1778 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1779 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001780 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001781 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001782
1783 return e;
1784}
1785
Martin Josefsson78cafda2004-02-02 20:01:18 +00001786void clear_rule_matches(struct iptables_rule_match **matches)
1787{
1788 struct iptables_rule_match *matchp, *tmp;
1789
1790 for (matchp = *matches; matchp;) {
1791 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001792 if (matchp->match->m)
1793 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001794 free(matchp);
1795 matchp = tmp;
1796 }
1797
1798 *matches = NULL;
1799}
1800
Rusty Russell3aef54d2005-01-03 03:48:40 +00001801static void set_revision(char *name, u_int8_t revision)
1802{
1803 /* Old kernel sources don't have ".revision" field,
1804 but we stole a byte from name. */
1805 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1806 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1807}
1808
Marc Bouchere6869a82000-03-20 06:03:29 +00001809int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1810{
1811 struct ipt_entry fw, *e = NULL;
1812 int invert = 0;
1813 unsigned int nsaddrs = 0, ndaddrs = 0;
1814 struct in_addr *saddrs = NULL, *daddrs = NULL;
1815
1816 int c, verbose = 0;
1817 const char *chain = NULL;
1818 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1819 const char *policy = NULL, *newname = NULL;
1820 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001821 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 int ret = 1;
1823 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001824 struct iptables_rule_match *matches = NULL;
1825 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001827 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001828 const char *jumpto = "";
1829 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001830 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001831 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001832
1833 memset(&fw, 0, sizeof(fw));
1834
Harald Welteae1ff9f2000-12-01 14:26:20 +00001835 /* re-set optind to 0 in case do_command gets called
1836 * a second time */
1837 optind = 0;
1838
1839 /* clear mflags in case do_command gets called a second time
1840 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001841 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001842 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001843
1844 for (t = iptables_targets; t; t = t->next) {
1845 t->tflags = 0;
1846 t->used = 0;
1847 }
1848
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 /* Suppress error messages: we may add new options if we
1850 demand-load a protocol. */
1851 opterr = 0;
1852
1853 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001854 "-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 +00001855 opts, NULL)) != -1) {
1856 switch (c) {
1857 /*
1858 * Command selection
1859 */
1860 case 'A':
1861 add_command(&command, CMD_APPEND, CMD_NONE,
1862 invert);
1863 chain = optarg;
1864 break;
1865
1866 case 'D':
1867 add_command(&command, CMD_DELETE, CMD_NONE,
1868 invert);
1869 chain = optarg;
1870 if (optind < argc && argv[optind][0] != '-'
1871 && argv[optind][0] != '!') {
1872 rulenum = parse_rulenumber(argv[optind++]);
1873 command = CMD_DELETE_NUM;
1874 }
1875 break;
1876
Marc Bouchere6869a82000-03-20 06:03:29 +00001877 case 'R':
1878 add_command(&command, CMD_REPLACE, CMD_NONE,
1879 invert);
1880 chain = optarg;
1881 if (optind < argc && argv[optind][0] != '-'
1882 && argv[optind][0] != '!')
1883 rulenum = parse_rulenumber(argv[optind++]);
1884 else
1885 exit_error(PARAMETER_PROBLEM,
1886 "-%c requires a rule number",
1887 cmd2char(CMD_REPLACE));
1888 break;
1889
1890 case 'I':
1891 add_command(&command, CMD_INSERT, CMD_NONE,
1892 invert);
1893 chain = optarg;
1894 if (optind < argc && argv[optind][0] != '-'
1895 && argv[optind][0] != '!')
1896 rulenum = parse_rulenumber(argv[optind++]);
1897 else rulenum = 1;
1898 break;
1899
1900 case 'L':
1901 add_command(&command, CMD_LIST, CMD_ZERO,
1902 invert);
1903 if (optarg) chain = optarg;
1904 else if (optind < argc && argv[optind][0] != '-'
1905 && argv[optind][0] != '!')
1906 chain = argv[optind++];
1907 break;
1908
1909 case 'F':
1910 add_command(&command, CMD_FLUSH, CMD_NONE,
1911 invert);
1912 if (optarg) chain = optarg;
1913 else if (optind < argc && argv[optind][0] != '-'
1914 && argv[optind][0] != '!')
1915 chain = argv[optind++];
1916 break;
1917
1918 case 'Z':
1919 add_command(&command, CMD_ZERO, CMD_LIST,
1920 invert);
1921 if (optarg) chain = optarg;
1922 else if (optind < argc && argv[optind][0] != '-'
1923 && argv[optind][0] != '!')
1924 chain = argv[optind++];
1925 break;
1926
1927 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001928 if (optarg && *optarg == '-')
1929 exit_error(PARAMETER_PROBLEM,
1930 "chain name not allowed to start "
1931 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001932 if (find_target(optarg, TRY_LOAD))
1933 exit_error(PARAMETER_PROBLEM,
1934 "chain name may not clash "
1935 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001936 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1937 invert);
1938 chain = optarg;
1939 break;
1940
1941 case 'X':
1942 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1943 invert);
1944 if (optarg) chain = optarg;
1945 else if (optind < argc && argv[optind][0] != '-'
1946 && argv[optind][0] != '!')
1947 chain = argv[optind++];
1948 break;
1949
1950 case 'E':
1951 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1952 invert);
1953 chain = optarg;
1954 if (optind < argc && argv[optind][0] != '-'
1955 && argv[optind][0] != '!')
1956 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001957 else
1958 exit_error(PARAMETER_PROBLEM,
1959 "-%c requires old-chain-name and "
1960 "new-chain-name",
1961 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001962 break;
1963
1964 case 'P':
1965 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1966 invert);
1967 chain = optarg;
1968 if (optind < argc && argv[optind][0] != '-'
1969 && argv[optind][0] != '!')
1970 policy = argv[optind++];
1971 else
1972 exit_error(PARAMETER_PROBLEM,
1973 "-%c requires a chain and a policy",
1974 cmd2char(CMD_SET_POLICY));
1975 break;
1976
1977 case 'h':
1978 if (!optarg)
1979 optarg = argv[optind];
1980
Rusty Russell2e0a3212000-04-19 11:23:18 +00001981 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001982 if (!matches && protocol)
1983 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001984
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001985 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001986
1987 /*
1988 * Option selection
1989 */
1990 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001991 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001992 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1993 invert);
1994
1995 /* Canonicalize into lower case */
1996 for (protocol = argv[optind-1]; *protocol; protocol++)
1997 *protocol = tolower(*protocol);
1998
1999 protocol = argv[optind-1];
2000 fw.ip.proto = parse_protocol(protocol);
2001
2002 if (fw.ip.proto == 0
2003 && (fw.ip.invflags & IPT_INV_PROTO))
2004 exit_error(PARAMETER_PROBLEM,
2005 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 break;
2007
2008 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002009 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002010 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2011 invert);
2012 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002013 break;
2014
2015 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002016 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002017 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2018 invert);
2019 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 break;
2021
2022 case 'j':
2023 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2024 invert);
2025 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002026 /* TRY_LOAD (may be chain name) */
2027 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002028
2029 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002030 size_t size;
2031
Rusty Russell73f72f52000-07-03 10:17:57 +00002032 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2033 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002034
Rusty Russell2e0a3212000-04-19 11:23:18 +00002035 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002036 target->t->u.target_size = size;
2037 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002038 set_revision(target->t->u.user.name,
2039 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002040 if (target->init != NULL)
2041 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002042 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 }
2044 break;
2045
2046
2047 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002048 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002049 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2050 invert);
2051 parse_interface(argv[optind-1],
2052 fw.ip.iniface,
2053 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002054 break;
2055
2056 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002057 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002058 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2059 invert);
2060 parse_interface(argv[optind-1],
2061 fw.ip.outiface,
2062 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 break;
2064
2065 case 'f':
2066 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2067 invert);
2068 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002069 break;
2070
2071 case 'v':
2072 if (!verbose)
2073 set_option(&options, OPT_VERBOSE,
2074 &fw.ip.invflags, invert);
2075 verbose++;
2076 break;
2077
Rusty Russell52a51492000-05-02 16:44:29 +00002078 case 'm': {
2079 size_t size;
2080
Marc Bouchere6869a82000-03-20 06:03:29 +00002081 if (invert)
2082 exit_error(PARAMETER_PROBLEM,
2083 "unexpected ! flag before --match");
2084
Martin Josefsson78cafda2004-02-02 20:01:18 +00002085 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002086 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2087 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002088 m->m = fw_calloc(1, size);
2089 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002090 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002091 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002092 if (m->init != NULL)
2093 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002094 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002095 }
2096 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002097
2098 case 'n':
2099 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2100 invert);
2101 break;
2102
2103 case 't':
2104 if (invert)
2105 exit_error(PARAMETER_PROBLEM,
2106 "unexpected ! flag before --table");
2107 *table = argv[optind-1];
2108 break;
2109
2110 case 'x':
2111 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2112 invert);
2113 break;
2114
2115 case 'V':
2116 if (invert)
2117 printf("Not %s ;-)\n", program_version);
2118 else
2119 printf("%s v%s\n",
2120 program_name, program_version);
2121 exit(0);
2122
2123 case '0':
2124 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2125 invert);
2126 break;
2127
Harald Welte82dd2ec2000-12-19 05:18:15 +00002128 case 'M':
2129 modprobe = optarg;
2130 break;
2131
Harald Welteccd49e52001-01-23 22:54:34 +00002132 case 'c':
2133
2134 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2135 invert);
2136 pcnt = optarg;
2137 if (optind < argc && argv[optind][0] != '-'
2138 && argv[optind][0] != '!')
2139 bcnt = argv[optind++];
2140 else
2141 exit_error(PARAMETER_PROBLEM,
2142 "-%c requires packet and byte counter",
2143 opt2char(OPT_COUNTERS));
2144
Martin Josefssona28d4952004-05-26 16:04:48 +00002145 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002146 exit_error(PARAMETER_PROBLEM,
2147 "-%c packet counter not numeric",
2148 opt2char(OPT_COUNTERS));
2149
Martin Josefssona28d4952004-05-26 16:04:48 +00002150 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002151 exit_error(PARAMETER_PROBLEM,
2152 "-%c byte counter not numeric",
2153 opt2char(OPT_COUNTERS));
2154
2155 break;
2156
2157
Marc Bouchere6869a82000-03-20 06:03:29 +00002158 case 1: /* non option */
2159 if (optarg[0] == '!' && optarg[1] == '\0') {
2160 if (invert)
2161 exit_error(PARAMETER_PROBLEM,
2162 "multiple consecutive ! not"
2163 " allowed");
2164 invert = TRUE;
2165 optarg[0] = '\0';
2166 continue;
2167 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002168 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002169 exit_tryhelp(2);
2170
2171 default:
2172 /* FIXME: This scheme doesn't allow two of the same
2173 matches --RR */
2174 if (!target
2175 || !(target->parse(c - target->option_offset,
2176 argv, invert,
2177 &target->tflags,
2178 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002179 for (matchp = matches; matchp; matchp = matchp->next) {
2180 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002181 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002182 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002183 &fw,
2184 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002185 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002186 break;
2187 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002188 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002189
2190 /* If you listen carefully, you can
2191 actually hear this code suck. */
2192
2193 /* some explanations (after four different bugs
2194 * in 3 different releases): If we encountere a
2195 * parameter, that has not been parsed yet,
2196 * it's not an option of an explicitly loaded
2197 * match or a target. However, we support
2198 * implicit loading of the protocol match
2199 * extension. '-p tcp' means 'l4 proto 6' and
2200 * at the same time 'load tcp protocol match on
2201 * demand if we specify --dport'.
2202 *
2203 * To make this work, we need to make sure:
2204 * - the parameter has not been parsed by
2205 * a match (m above)
2206 * - a protocol has been specified
2207 * - the protocol extension has not been
2208 * loaded yet, or is loaded and unused
2209 * [think of iptables-restore!]
2210 * - the protocol extension can be successively
2211 * loaded
2212 */
2213 if (m == NULL
2214 && protocol
2215 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002216 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002217 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002218 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002219 && (proto_used == 0))
2220 )
2221 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002222 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002223 /* Try loading protocol */
2224 size_t size;
2225
2226 proto_used = 1;
2227
2228 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2229 + m->size;
2230
2231 m->m = fw_calloc(1, size);
2232 m->m->u.match_size = size;
2233 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002234 set_revision(m->m->u.user.name,
2235 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002236 if (m->init != NULL)
2237 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002238
2239 opts = merge_options(opts,
2240 m->extra_opts, &m->option_offset);
2241
2242 optind--;
2243 continue;
2244 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002245 if (!m)
2246 exit_error(PARAMETER_PROBLEM,
2247 "Unknown arg `%s'",
2248 argv[optind-1]);
2249 }
2250 }
2251 invert = FALSE;
2252 }
2253
Martin Josefsson78cafda2004-02-02 20:01:18 +00002254 for (matchp = matches; matchp; matchp = matchp->next)
2255 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002256
Marc Bouchere6869a82000-03-20 06:03:29 +00002257 if (target)
2258 target->final_check(target->tflags);
2259
2260 /* Fix me: must put inverse options checking here --MN */
2261
2262 if (optind < argc)
2263 exit_error(PARAMETER_PROBLEM,
2264 "unknown arguments found on commandline");
2265 if (!command)
2266 exit_error(PARAMETER_PROBLEM, "no command specified");
2267 if (invert)
2268 exit_error(PARAMETER_PROBLEM,
2269 "nothing appropriate following !");
2270
Harald Welte6336bfd2002-05-07 14:41:43 +00002271 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002272 if (!(options & OPT_DESTINATION))
2273 dhostnetworkmask = "0.0.0.0/0";
2274 if (!(options & OPT_SOURCE))
2275 shostnetworkmask = "0.0.0.0/0";
2276 }
2277
2278 if (shostnetworkmask)
2279 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2280 &(fw.ip.smsk), &nsaddrs);
2281
2282 if (dhostnetworkmask)
2283 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2284 &(fw.ip.dmsk), &ndaddrs);
2285
2286 if ((nsaddrs > 1 || ndaddrs > 1) &&
2287 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2288 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2289 " source or destination IP addresses");
2290
Marc Bouchere6869a82000-03-20 06:03:29 +00002291 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2292 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2293 "specify a unique address");
2294
2295 generic_opt_check(command, options);
2296
2297 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2298 exit_error(PARAMETER_PROBLEM,
2299 "chain name `%s' too long (must be under %i chars)",
2300 chain, IPT_FUNCTION_MAXNAMELEN);
2301
Harald Welteae1ff9f2000-12-01 14:26:20 +00002302 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002303 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002304 *handle = iptc_init(*table);
2305
Rusty Russell8beb0492004-12-22 00:37:10 +00002306 /* try to insmod the module if iptc_init failed */
2307 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002308 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002309
Marc Bouchere6869a82000-03-20 06:03:29 +00002310 if (!*handle)
2311 exit_error(VERSION_PROBLEM,
2312 "can't initialize iptables table `%s': %s",
2313 *table, iptc_strerror(errno));
2314
Harald Welte6336bfd2002-05-07 14:41:43 +00002315 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002316 || command == CMD_DELETE
2317 || command == CMD_INSERT
2318 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002319 if (strcmp(chain, "PREROUTING") == 0
2320 || strcmp(chain, "INPUT") == 0) {
2321 /* -o not valid with incoming packets. */
2322 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002323 exit_error(PARAMETER_PROBLEM,
2324 "Can't use -%c with %s\n",
2325 opt2char(OPT_VIANAMEOUT),
2326 chain);
2327 }
2328
Rusty Russella4860fd2000-06-17 16:13:02 +00002329 if (strcmp(chain, "POSTROUTING") == 0
2330 || strcmp(chain, "OUTPUT") == 0) {
2331 /* -i not valid with outgoing packets */
2332 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002333 exit_error(PARAMETER_PROBLEM,
2334 "Can't use -%c with %s\n",
2335 opt2char(OPT_VIANAMEIN),
2336 chain);
2337 }
2338
2339 if (target && iptc_is_chain(jumpto, *handle)) {
2340 printf("Warning: using chain %s, not extension\n",
2341 jumpto);
2342
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002343 if (target->t)
2344 free(target->t);
2345
Marc Bouchere6869a82000-03-20 06:03:29 +00002346 target = NULL;
2347 }
2348
2349 /* If they didn't specify a target, or it's a chain
2350 name, use standard. */
2351 if (!target
2352 && (strlen(jumpto) == 0
2353 || iptc_is_chain(jumpto, *handle))) {
2354 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002355
Rusty Russell52a51492000-05-02 16:44:29 +00002356 target = find_target(IPT_STANDARD_TARGET,
2357 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002358
2359 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002360 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002361 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002362 target->t->u.target_size = size;
2363 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002364 set_revision(target->t->u.user.name, target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002365 if (target->init != NULL)
2366 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002367 }
2368
Rusty Russell7e53bf92000-03-20 07:03:28 +00002369 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002370 /* it is no chain, and we can't load a plugin.
2371 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002372 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002373 * chain. */
2374 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002375 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002376 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002377 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002378 }
2379 }
2380
2381 switch (command) {
2382 case CMD_APPEND:
2383 ret = append_entry(chain, e,
2384 nsaddrs, saddrs, ndaddrs, daddrs,
2385 options&OPT_VERBOSE,
2386 handle);
2387 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002388 case CMD_DELETE:
2389 ret = delete_entry(chain, e,
2390 nsaddrs, saddrs, ndaddrs, daddrs,
2391 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002392 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002393 break;
2394 case CMD_DELETE_NUM:
2395 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2396 break;
2397 case CMD_REPLACE:
2398 ret = replace_entry(chain, e, rulenum - 1,
2399 saddrs, daddrs, options&OPT_VERBOSE,
2400 handle);
2401 break;
2402 case CMD_INSERT:
2403 ret = insert_entry(chain, e, rulenum - 1,
2404 nsaddrs, saddrs, ndaddrs, daddrs,
2405 options&OPT_VERBOSE,
2406 handle);
2407 break;
2408 case CMD_LIST:
2409 ret = list_entries(chain,
2410 options&OPT_VERBOSE,
2411 options&OPT_NUMERIC,
2412 options&OPT_EXPANDED,
2413 options&OPT_LINENUMBERS,
2414 handle);
2415 break;
2416 case CMD_FLUSH:
2417 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2418 break;
2419 case CMD_ZERO:
2420 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2421 break;
2422 case CMD_LIST|CMD_ZERO:
2423 ret = list_entries(chain,
2424 options&OPT_VERBOSE,
2425 options&OPT_NUMERIC,
2426 options&OPT_EXPANDED,
2427 options&OPT_LINENUMBERS,
2428 handle);
2429 if (ret)
2430 ret = zero_entries(chain,
2431 options&OPT_VERBOSE, handle);
2432 break;
2433 case CMD_NEW_CHAIN:
2434 ret = iptc_create_chain(chain, handle);
2435 break;
2436 case CMD_DELETE_CHAIN:
2437 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2438 break;
2439 case CMD_RENAME_CHAIN:
2440 ret = iptc_rename_chain(chain, newname, handle);
2441 break;
2442 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002443 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002444 break;
2445 default:
2446 /* We should never reach this... */
2447 exit_tryhelp(2);
2448 }
2449
2450 if (verbose > 1)
2451 dump_entries(*handle);
2452
Martin Josefsson78cafda2004-02-02 20:01:18 +00002453 clear_rule_matches(&matches);
2454
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002455 if (e != NULL) {
2456 free(e);
2457 e = NULL;
2458 }
2459
keso6997cdf2004-07-04 15:20:53 +00002460 free(saddrs);
2461 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002462 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002463
Marc Bouchere6869a82000-03-20 06:03:29 +00002464 return ret;
2465}