blob: aec9613755a3f8b412b3abdbe1c2b201804572ca [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>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000043
44#ifndef TRUE
45#define TRUE 1
46#endif
47#ifndef FALSE
48#define FALSE 0
49#endif
50
Harald Welte82dd2ec2000-12-19 05:18:15 +000051#ifndef PROC_SYS_MODPROBE
52#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
82#define CMD_CHECK 0x0800U
83#define CMD_RENAME_CHAIN 0x1000U
84#define NUMBER_OF_CMD 13
85static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000086 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000087
88#define OPTION_OFFSET 256
89
90#define OPT_NONE 0x00000U
91#define OPT_NUMERIC 0x00001U
92#define OPT_SOURCE 0x00002U
93#define OPT_DESTINATION 0x00004U
94#define OPT_PROTOCOL 0x00008U
95#define OPT_JUMP 0x00010U
96#define OPT_VERBOSE 0x00020U
97#define OPT_EXPANDED 0x00040U
98#define OPT_VIANAMEIN 0x00080U
99#define OPT_VIANAMEOUT 0x00100U
100#define OPT_FRAGMENT 0x00200U
101#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000102#define OPT_COUNTERS 0x00800U
103#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000123 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "fragments", 0, 0, 'f' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000136 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000137 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 { 0 }
139};
140
Illes Marci63e90632003-03-03 08:08:37 +0000141/* we need this for iptables-restore. iptables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. iptables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
146
Marc Bouchere6869a82000-03-20 06:03:29 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
162 /* -n -s -d -p -j -v -x -i -o -f --line */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
176};
177
178static int inverse_for_options[NUMBER_OF_OPT] =
179{
180/* -n */ 0,
181/* -s */ IPT_INV_SRCIP,
182/* -d */ IPT_INV_DSTIP,
183/* -p */ IPT_INV_PROTO,
184/* -j */ 0,
185/* -v */ 0,
186/* -x */ 0,
187/* -i */ IPT_INV_VIA_IN,
188/* -o */ IPT_INV_VIA_OUT,
189/* -f */ IPT_INV_FRAG,
190/*--line*/ 0
191};
192
193const char *program_version;
194const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000195char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196
Phil Oester8cf65912005-09-19 15:00:33 +0000197int kernel_version;
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 Russella3e6aaa2000-12-19 04:45:23 +0000213/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000214/* defined in netinet/in.h */
215#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000216#ifndef IPPROTO_ESP
217#define IPPROTO_ESP 50
218#endif
219#ifndef IPPROTO_AH
220#define IPPROTO_AH 51
221#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000222#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000223
Marc Bouchere6869a82000-03-20 06:03:29 +0000224static const struct pprot chain_protos[] = {
225 { "tcp", IPPROTO_TCP },
226 { "udp", IPPROTO_UDP },
227 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000228 { "esp", IPPROTO_ESP },
229 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000230 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231 { "all", 0 },
232};
233
234static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000235proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000236{
237 unsigned int i;
238
Rusty Russell28381a42000-05-10 00:19:50 +0000239 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000240 struct protoent *pent = getprotobynumber(proto);
241 if (pent)
242 return pent->p_name;
243 }
244
245 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
246 if (chain_protos[i].num == proto)
247 return chain_protos[i].name;
248
249 return NULL;
250}
251
252struct in_addr *
253dotted_to_addr(const char *dotted)
254{
255 static struct in_addr addr;
256 unsigned char *addrp;
257 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000258 unsigned int onebyte;
259 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000260 char buf[20];
261
262 /* copy dotted string, because we need to modify it */
263 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000264 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000265 addrp = (unsigned char *) &(addr.s_addr);
266
267 p = buf;
268 for (i = 0; i < 3; i++) {
269 if ((q = strchr(p, '.')) == NULL)
270 return (struct in_addr *) NULL;
271
272 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000273 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 return (struct in_addr *) NULL;
275
276 addrp[i] = (unsigned char) onebyte;
277 p = q + 1;
278 }
279
280 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000281 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 return (struct in_addr *) NULL;
283
284 addrp[3] = (unsigned char) onebyte;
285
286 return &addr;
287}
288
289static struct in_addr *
290network_to_addr(const char *name)
291{
292 struct netent *net;
293 static struct in_addr addr;
294
295 if ((net = getnetbyname(name)) != NULL) {
296 if (net->n_addrtype != AF_INET)
297 return (struct in_addr *) NULL;
298 addr.s_addr = htonl((unsigned long) net->n_net);
299 return &addr;
300 }
301
302 return (struct in_addr *) NULL;
303}
304
305static void
306inaddrcpy(struct in_addr *dst, struct in_addr *src)
307{
308 /* memcpy(dst, src, sizeof(struct in_addr)); */
309 dst->s_addr = src->s_addr;
310}
311
Pablo Neiradfdcd642005-05-29 19:05:23 +0000312static void free_opts(int reset_offset)
313{
314 if (opts != original_opts) {
315 free(opts);
316 opts = original_opts;
317 if (reset_offset)
318 global_option_offset = 0;
319 }
320}
321
Marc Bouchere6869a82000-03-20 06:03:29 +0000322void
323exit_error(enum exittype status, char *msg, ...)
324{
325 va_list args;
326
327 va_start(args, msg);
328 fprintf(stderr, "%s v%s: ", program_name, program_version);
329 vfprintf(stderr, msg, args);
330 va_end(args);
331 fprintf(stderr, "\n");
332 if (status == PARAMETER_PROBLEM)
333 exit_tryhelp(status);
334 if (status == VERSION_PROBLEM)
335 fprintf(stderr,
336 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000337 /* On error paths, make sure that we don't leak memory */
338 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 exit(status);
340}
341
342void
343exit_tryhelp(int status)
344{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000345 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000346 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
348 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000349 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000350 exit(status);
351}
352
353void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000355{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000357 struct iptables_target *t = NULL;
358
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000360"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000361" %s -[RI] chain rulenum rule-specification [options]\n"
362" %s -D chain rulenum [options]\n"
363" %s -[LFZ] [chain] [options]\n"
364" %s -[NX] chain\n"
365" %s -E old-chain-name new-chain-name\n"
366" %s -P chain target [options]\n"
367" %s -h (print this help information)\n\n",
368 program_name, program_version, program_name, program_name,
369 program_name, program_name, program_name, program_name,
370 program_name, program_name);
371
372 printf(
373"Commands:\n"
374"Either long or short options are allowed.\n"
375" --append -A chain Append to chain\n"
376" --delete -D chain Delete matching rule from chain\n"
377" --delete -D chain rulenum\n"
378" Delete rule rulenum (1 = first) from chain\n"
379" --insert -I chain [rulenum]\n"
380" Insert in chain as rulenum (default 1=first)\n"
381" --replace -R chain rulenum\n"
382" Replace rule rulenum (1 = first) in chain\n"
383" --list -L [chain] List the rules in a chain or all chains\n"
384" --flush -F [chain] Delete all rules in chain or all chains\n"
385" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000386" --new -N chain Create a new user-defined chain\n"
387" --delete-chain\n"
388" -X [chain] Delete a user-defined chain\n"
389" --policy -P chain target\n"
390" Change policy on chain to target\n"
391" --rename-chain\n"
392" -E old-chain new-chain\n"
393" Change chain name, (moving any references)\n"
394
395"Options:\n"
396" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
397" --source -s [!] address[/mask]\n"
398" source specification\n"
399" --destination -d [!] address[/mask]\n"
400" destination specification\n"
401" --in-interface -i [!] input name[+]\n"
402" network interface name ([+] for wildcard)\n"
403" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000404" target for rule (may load target extension)\n"
405" --match -m match\n"
406" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000407" --numeric -n numeric output of addresses and ports\n"
408" --out-interface -o [!] output name[+]\n"
409" network interface name ([+] for wildcard)\n"
410" --table -t table table to manipulate (default: `filter')\n"
411" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000412" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000413" --exact -x expand numbers (display exact values)\n"
414"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000415" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000416" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000417"[!] --version -V print package version.\n");
418
Rusty Russell363112d2000-08-11 13:49:26 +0000419 /* Print out any special helps. A user might like to be able
420 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000421 results. So we call help for all specified matches & targets */
422 for (t = iptables_targets; t ;t = t->next) {
423 if (t->used) {
424 printf("\n");
425 t->help();
426 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000427 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000428 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000429 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000430 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000431 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000432 exit(0);
433}
434
435static void
436generic_opt_check(int command, int options)
437{
438 int i, j, legal = 0;
439
440 /* Check that commands are valid with options. Complicated by the
441 * fact that if an option is legal with *any* command given, it is
442 * legal overall (ie. -z and -l).
443 */
444 for (i = 0; i < NUMBER_OF_OPT; i++) {
445 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
446
447 for (j = 0; j < NUMBER_OF_CMD; j++) {
448 if (!(command & (1<<j)))
449 continue;
450
451 if (!(options & (1<<i))) {
452 if (commands_v_options[j][i] == '+')
453 exit_error(PARAMETER_PROBLEM,
454 "You need to supply the `-%c' "
455 "option for this command\n",
456 optflags[i]);
457 } else {
458 if (commands_v_options[j][i] != 'x')
459 legal = 1;
460 else if (legal == 0)
461 legal = -1;
462 }
463 }
464 if (legal == -1)
465 exit_error(PARAMETER_PROBLEM,
466 "Illegal option `-%c' with this command\n",
467 optflags[i]);
468 }
469}
470
471static char
472opt2char(int option)
473{
474 const char *ptr;
475 for (ptr = optflags; option > 1; option >>= 1, ptr++);
476
477 return *ptr;
478}
479
480static char
481cmd2char(int option)
482{
483 const char *ptr;
484 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
485
486 return *ptr;
487}
488
489static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000490add_command(unsigned int *cmd, const int newcmd, const int othercmds,
491 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000492{
493 if (invert)
494 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
495 if (*cmd & (~othercmds))
496 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
497 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
498 *cmd |= newcmd;
499}
500
501int
Harald Welteb77f1da2002-03-14 11:35:58 +0000502check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000503{
504 if (option && strcmp(option, "!") == 0) {
505 if (*invert)
506 exit_error(PARAMETER_PROBLEM,
507 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000508 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000509 if (optind) {
510 *optind = *optind+1;
511 if (argc && *optind > argc)
512 exit_error(PARAMETER_PROBLEM,
513 "no argument following `!'");
514 }
515
Marc Bouchere6869a82000-03-20 06:03:29 +0000516 return TRUE;
517 }
518 return FALSE;
519}
520
521static void *
522fw_calloc(size_t count, size_t size)
523{
524 void *p;
525
526 if ((p = calloc(count, size)) == NULL) {
527 perror("iptables: calloc failed");
528 exit(1);
529 }
530 return p;
531}
532
533static void *
534fw_malloc(size_t size)
535{
536 void *p;
537
538 if ((p = malloc(size)) == NULL) {
539 perror("iptables: malloc failed");
540 exit(1);
541 }
542 return p;
543}
544
545static struct in_addr *
546host_to_addr(const char *name, unsigned int *naddr)
547{
548 struct hostent *host;
549 struct in_addr *addr;
550 unsigned int i;
551
552 *naddr = 0;
553 if ((host = gethostbyname(name)) != NULL) {
554 if (host->h_addrtype != AF_INET ||
555 host->h_length != sizeof(struct in_addr))
556 return (struct in_addr *) NULL;
557
558 while (host->h_addr_list[*naddr] != (char *) NULL)
559 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000560 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000561 for (i = 0; i < *naddr; i++)
562 inaddrcpy(&(addr[i]),
563 (struct in_addr *) host->h_addr_list[i]);
564 return addr;
565 }
566
567 return (struct in_addr *) NULL;
568}
569
570static char *
571addr_to_host(const struct in_addr *addr)
572{
573 struct hostent *host;
574
575 if ((host = gethostbyaddr((char *) addr,
576 sizeof(struct in_addr), AF_INET)) != NULL)
577 return (char *) host->h_name;
578
579 return (char *) NULL;
580}
581
582/*
583 * All functions starting with "parse" should succeed, otherwise
584 * the program fails.
585 * Most routines return pointers to static data that may change
586 * between calls to the same or other routines with a few exceptions:
587 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
588 * return global static data.
589*/
590
591static struct in_addr *
592parse_hostnetwork(const char *name, unsigned int *naddrs)
593{
594 struct in_addr *addrp, *addrptmp;
595
596 if ((addrptmp = dotted_to_addr(name)) != NULL ||
597 (addrptmp = network_to_addr(name)) != NULL) {
598 addrp = fw_malloc(sizeof(struct in_addr));
599 inaddrcpy(addrp, addrptmp);
600 *naddrs = 1;
601 return addrp;
602 }
603 if ((addrp = host_to_addr(name, naddrs)) != NULL)
604 return addrp;
605
606 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
607}
608
609static struct in_addr *
610parse_mask(char *mask)
611{
612 static struct in_addr maskaddr;
613 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000614 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000615
616 if (mask == NULL) {
617 /* no mask at all defaults to 32 bits */
618 maskaddr.s_addr = 0xFFFFFFFF;
619 return &maskaddr;
620 }
621 if ((addrp = dotted_to_addr(mask)) != NULL)
622 /* dotted_to_addr already returns a network byte order addr */
623 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000624 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000625 exit_error(PARAMETER_PROBLEM,
626 "invalid mask `%s' specified", mask);
627 if (bits != 0) {
628 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
629 return &maskaddr;
630 }
631
632 maskaddr.s_addr = 0L;
633 return &maskaddr;
634}
635
Marc Boucherb93c7982001-12-06 14:50:19 +0000636void
Marc Bouchere6869a82000-03-20 06:03:29 +0000637parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
638 struct in_addr *maskp, unsigned int *naddrs)
639{
640 struct in_addr *addrp;
641 char buf[256];
642 char *p;
643 int i, j, k, n;
644
645 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000646 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000647 if ((p = strrchr(buf, '/')) != NULL) {
648 *p = '\0';
649 addrp = parse_mask(p + 1);
650 } else
651 addrp = parse_mask(NULL);
652 inaddrcpy(maskp, addrp);
653
654 /* if a null mask is given, the name is ignored, like in "any/0" */
655 if (maskp->s_addr == 0L)
656 strcpy(buf, "0.0.0.0");
657
658 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
659 n = *naddrs;
660 for (i = 0, j = 0; i < n; i++) {
661 addrp[j++].s_addr &= maskp->s_addr;
662 for (k = 0; k < j - 1; k++) {
663 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
664 (*naddrs)--;
665 j--;
666 break;
667 }
668 }
669 }
670}
671
672struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000673find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000674{
675 struct iptables_match *ptr;
676
677 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
678 if (strcmp(name, ptr->name) == 0)
679 break;
680 }
681
Harald Welte3efb6ea2001-08-06 18:50:21 +0000682#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000683 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000684 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000685 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000686 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000687 if (dlopen(path, RTLD_NOW)) {
688 /* Found library. If it didn't register itself,
689 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000690 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000691
Rusty Russell9e1d2142000-04-23 09:11:12 +0000692 if (!ptr)
693 exit_error(PARAMETER_PROBLEM,
694 "Couldn't load match `%s'\n",
695 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000696 } else if (tryload == LOAD_MUST_SUCCEED)
697 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000698 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000699 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000700 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000701#else
702 if (ptr && !ptr->loaded) {
703 if (tryload != DONT_LOAD)
704 ptr->loaded = 1;
705 else
706 ptr = NULL;
707 }
Marc Boucher067477b2002-03-24 15:09:31 +0000708 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
709 exit_error(PARAMETER_PROBLEM,
710 "Couldn't find match `%s'\n", name);
711 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000712#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000713
Martin Josefsson78cafda2004-02-02 20:01:18 +0000714 if (ptr && matches) {
715 struct iptables_rule_match **i;
716 struct iptables_rule_match *newentry;
717
718 newentry = fw_malloc(sizeof(struct iptables_rule_match));
719
720 for (i = matches; *i; i = &(*i)->next);
721 newentry->match = ptr;
722 newentry->next = NULL;
723 *i = newentry;
724 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000725
Marc Bouchere6869a82000-03-20 06:03:29 +0000726 return ptr;
727}
728
Rusty Russell28381a42000-05-10 00:19:50 +0000729/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
730static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000731find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000732{
Harald Welteed498492001-07-23 01:24:22 +0000733 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000734
Harald Welte0b0013a2002-02-18 16:15:31 +0000735 if (string_to_number(pname, 0, 255, &proto) != -1) {
736 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000737
Harald Welte0b0013a2002-02-18 16:15:31 +0000738 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000739 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000740 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000741 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000742
743 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000744}
745
Marc Boucherb93c7982001-12-06 14:50:19 +0000746u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000747parse_protocol(const char *s)
748{
Harald Welteed498492001-07-23 01:24:22 +0000749 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000750
Harald Welteed498492001-07-23 01:24:22 +0000751 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000752 struct protoent *pent;
753
754 if ((pent = getprotobyname(s)))
755 proto = pent->p_proto;
756 else {
757 unsigned int i;
758 for (i = 0;
759 i < sizeof(chain_protos)/sizeof(struct pprot);
760 i++) {
761 if (strcmp(s, chain_protos[i].name) == 0) {
762 proto = chain_protos[i].num;
763 break;
764 }
765 }
766 if (i == sizeof(chain_protos)/sizeof(struct pprot))
767 exit_error(PARAMETER_PROBLEM,
768 "unknown protocol `%s' specified",
769 s);
770 }
771 }
772
773 return (u_int16_t)proto;
774}
775
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000776void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000777{
778 int vialen = strlen(arg);
779 unsigned int i;
780
781 memset(mask, 0, IFNAMSIZ);
782 memset(vianame, 0, IFNAMSIZ);
783
784 if (vialen + 1 > IFNAMSIZ)
785 exit_error(PARAMETER_PROBLEM,
786 "interface name `%s' must be shorter than IFNAMSIZ"
787 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000788
Marc Bouchere6869a82000-03-20 06:03:29 +0000789 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000790 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000791 memset(mask, 0, IFNAMSIZ);
792 else if (vianame[vialen - 1] == '+') {
793 memset(mask, 0xFF, vialen - 1);
794 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000795 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 } else {
797 /* Include nul-terminator in match */
798 memset(mask, 0xFF, vialen + 1);
799 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000800 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000801 if (!isalnum(vianame[i])
802 && vianame[i] != '_'
803 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000804 printf("Warning: wierd character in interface"
805 " `%s' (No aliases, :, ! or *).\n",
806 vianame);
807 break;
808 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 }
810 }
811}
812
813/* Can't be zero. */
814static int
815parse_rulenumber(const char *rule)
816{
Harald Welteed498492001-07-23 01:24:22 +0000817 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818
Harald Welteed498492001-07-23 01:24:22 +0000819 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 exit_error(PARAMETER_PROBLEM,
821 "Invalid rule number `%s'", rule);
822
823 return rulenum;
824}
825
826static const char *
827parse_target(const char *targetname)
828{
829 const char *ptr;
830
831 if (strlen(targetname) < 1)
832 exit_error(PARAMETER_PROBLEM,
833 "Invalid target name (too short)");
834
835 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
836 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000837 "Invalid target name `%s' (%u chars max)",
838 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000839
840 for (ptr = targetname; *ptr; ptr++)
841 if (isspace(*ptr))
842 exit_error(PARAMETER_PROBLEM,
843 "Invalid target name `%s'", targetname);
844 return targetname;
845}
846
847static char *
848addr_to_network(const struct in_addr *addr)
849{
850 struct netent *net;
851
852 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
853 return (char *) net->n_name;
854
855 return (char *) NULL;
856}
857
858char *
859addr_to_dotted(const struct in_addr *addrp)
860{
861 static char buf[20];
862 const unsigned char *bytep;
863
864 bytep = (const unsigned char *) &(addrp->s_addr);
865 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
866 return buf;
867}
Marc Boucherb93c7982001-12-06 14:50:19 +0000868
869char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000870addr_to_anyname(const struct in_addr *addr)
871{
872 char *name;
873
874 if ((name = addr_to_host(addr)) != NULL ||
875 (name = addr_to_network(addr)) != NULL)
876 return name;
877
878 return addr_to_dotted(addr);
879}
880
Marc Boucherb93c7982001-12-06 14:50:19 +0000881char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000882mask_to_dotted(const struct in_addr *mask)
883{
884 int i;
885 static char buf[20];
886 u_int32_t maskaddr, bits;
887
888 maskaddr = ntohl(mask->s_addr);
889
890 if (maskaddr == 0xFFFFFFFFL)
891 /* we don't want to see "/32" */
892 return "";
893
894 i = 32;
895 bits = 0xFFFFFFFEL;
896 while (--i >= 0 && maskaddr != bits)
897 bits <<= 1;
898 if (i >= 0)
899 sprintf(buf, "/%d", i);
900 else
901 /* mask was not a decent combination of 1's and 0's */
902 sprintf(buf, "/%s", addr_to_dotted(mask));
903
904 return buf;
905}
906
907int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
909 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000910{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000911 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000912 char *end;
913
914 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000915 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000917 if (*end == '\0' && end != s) {
918 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000919 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000920 *ret = number;
921 return 0;
922 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000923 }
924 return -1;
925}
926
Martin Josefssonb105bc92004-05-26 15:54:49 +0000927int
928string_to_number_l(const char *s, unsigned long min, unsigned long max,
929 unsigned long *ret)
930{
931 int result;
932 unsigned long long number;
933
934 result = string_to_number_ll(s, min, max, &number);
935 *ret = (unsigned long)number;
936
937 return result;
938}
939
940int string_to_number(const char *s, unsigned int min, unsigned int max,
941 unsigned int *ret)
942{
943 int result;
944 unsigned long number;
945
946 result = string_to_number_l(s, min, max, &number);
947 *ret = (unsigned int)number;
948
949 return result;
950}
951
Marc Bouchere6869a82000-03-20 06:03:29 +0000952static void
953set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
954 int invert)
955{
956 if (*options & option)
957 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
958 opt2char(option));
959 *options |= option;
960
961 if (invert) {
962 unsigned int i;
963 for (i = 0; 1 << i != option; i++);
964
965 if (!inverse_for_options[i])
966 exit_error(PARAMETER_PROBLEM,
967 "cannot have ! before -%c",
968 opt2char(option));
969 *invflg |= inverse_for_options[i];
970 }
971}
972
973struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000974find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000975{
976 struct iptables_target *ptr;
977
978 /* Standard target? */
979 if (strcmp(name, "") == 0
980 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
981 || strcmp(name, IPTC_LABEL_DROP) == 0
982 || strcmp(name, IPTC_LABEL_QUEUE) == 0
983 || strcmp(name, IPTC_LABEL_RETURN) == 0)
984 name = "standard";
985
986 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
987 if (strcmp(name, ptr->name) == 0)
988 break;
989 }
990
Harald Welte3efb6ea2001-08-06 18:50:21 +0000991#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000992 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000993 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000994 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000995 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000996 if (dlopen(path, RTLD_NOW)) {
997 /* Found library. If it didn't register itself,
998 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000999 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001000 if (!ptr)
1001 exit_error(PARAMETER_PROBLEM,
1002 "Couldn't load target `%s'\n",
1003 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001004 } else if (tryload == LOAD_MUST_SUCCEED)
1005 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001006 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001007 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001008 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001009#else
1010 if (ptr && !ptr->loaded) {
1011 if (tryload != DONT_LOAD)
1012 ptr->loaded = 1;
1013 else
1014 ptr = NULL;
1015 }
Marc Boucher067477b2002-03-24 15:09:31 +00001016 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1017 exit_error(PARAMETER_PROBLEM,
1018 "Couldn't find target `%s'\n", name);
1019 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001020#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001021
Harald Welteae1ff9f2000-12-01 14:26:20 +00001022 if (ptr)
1023 ptr->used = 1;
1024
Marc Bouchere6869a82000-03-20 06:03:29 +00001025 return ptr;
1026}
1027
1028static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001029merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001030 unsigned int *option_offset)
1031{
1032 unsigned int num_old, num_new, i;
1033 struct option *merge;
1034
1035 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));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001043 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001044 for (i = 0; i < num_new; i++) {
1045 merge[num_old + i] = newopts[i];
1046 merge[num_old + i].val += *option_offset;
1047 }
1048 memset(merge + num_old + num_new, 0, sizeof(struct option));
1049
1050 return merge;
1051}
1052
Rusty Russell3aef54d2005-01-03 03:48:40 +00001053static int compatible_revision(const char *name, u_int8_t revision, int opt)
1054{
1055 struct ipt_get_revision rev;
1056 socklen_t s = sizeof(rev);
1057 int max_rev, sockfd;
1058
1059 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1060 if (sockfd < 0) {
1061 fprintf(stderr, "Could not open socket to kernel: %s\n",
1062 strerror(errno));
1063 exit(1);
1064 }
1065
1066 strcpy(rev.name, name);
1067 rev.revision = revision;
1068
1069 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1070 if (max_rev < 0) {
1071 /* Definitely don't support this? */
1072 if (errno == EPROTONOSUPPORT) {
1073 close(sockfd);
1074 return 0;
1075 } else if (errno == ENOPROTOOPT) {
1076 close(sockfd);
1077 /* Assume only revision 0 support (old kernel) */
1078 return (revision == 0);
1079 } else {
1080 fprintf(stderr, "getsockopt failed strangely: %s\n",
1081 strerror(errno));
1082 exit(1);
1083 }
1084 }
1085 close(sockfd);
1086 return 1;
1087}
1088
1089static int compatible_match_revision(const char *name, u_int8_t revision)
1090{
1091 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1092}
1093
1094static int compatible_target_revision(const char *name, u_int8_t revision)
1095{
1096 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1097}
1098
Marc Bouchere6869a82000-03-20 06:03:29 +00001099void
1100register_match(struct iptables_match *me)
1101{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001102 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001103
Marc Bouchere6869a82000-03-20 06:03:29 +00001104 if (strcmp(me->version, program_version) != 0) {
1105 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1106 program_name, me->name, me->version, program_version);
1107 exit(1);
1108 }
1109
Martin Josefsson93911bf2005-01-03 07:46:07 +00001110 /* Revision field stole a char from name. */
1111 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001112 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001113 program_name, me->name);
1114 exit(1);
1115 }
1116
Rusty Russell3aef54d2005-01-03 03:48:40 +00001117 old = find_match(me->name, DONT_LOAD, NULL);
1118 if (old) {
1119 if (old->revision == me->revision) {
1120 fprintf(stderr,
1121 "%s: match `%s' already registered.\n",
1122 program_name, me->name);
1123 exit(1);
1124 }
1125
1126 /* Now we have two (or more) options, check compatibility. */
1127 if (compatible_match_revision(old->name, old->revision)
1128 && old->revision > me->revision)
1129 return;
1130
1131 /* Replace if compatible. */
1132 if (!compatible_match_revision(me->name, me->revision))
1133 return;
1134
1135 /* Delete old one. */
1136 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1137 *i = old->next;
1138 }
1139
Rusty Russell73f72f52000-07-03 10:17:57 +00001140 if (me->size != IPT_ALIGN(me->size)) {
1141 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001142 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001143 exit(1);
1144 }
1145
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001146 /* Append to list. */
1147 for (i = &iptables_matches; *i; i = &(*i)->next);
1148 me->next = NULL;
1149 *i = me;
1150
Marc Bouchere6869a82000-03-20 06:03:29 +00001151 me->m = NULL;
1152 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001153}
1154
1155void
1156register_target(struct iptables_target *me)
1157{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001158 struct iptables_target *old;
1159
Marc Bouchere6869a82000-03-20 06:03:29 +00001160 if (strcmp(me->version, program_version) != 0) {
1161 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1162 program_name, me->name, me->version, program_version);
1163 exit(1);
1164 }
1165
Martin Josefsson93911bf2005-01-03 07:46:07 +00001166 /* Revision field stole a char from name. */
1167 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001168 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001169 program_name, me->name);
1170 exit(1);
1171 }
1172
Rusty Russell3aef54d2005-01-03 03:48:40 +00001173 old = find_target(me->name, DONT_LOAD);
1174 if (old) {
1175 struct iptables_target **i;
1176
1177 if (old->revision == me->revision) {
1178 fprintf(stderr,
1179 "%s: target `%s' already registered.\n",
1180 program_name, me->name);
1181 exit(1);
1182 }
1183
Rusty Russell3aef54d2005-01-03 03:48:40 +00001184 /* Now we have two (or more) options, check compatibility. */
1185 if (compatible_target_revision(old->name, old->revision)
1186 && old->revision > me->revision)
1187 return;
1188
1189 /* Replace if compatible. */
1190 if (!compatible_target_revision(me->name, me->revision))
1191 return;
1192
1193 /* Delete old one. */
1194 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1195 *i = old->next;
1196 }
1197
Rusty Russell73f72f52000-07-03 10:17:57 +00001198 if (me->size != IPT_ALIGN(me->size)) {
1199 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001200 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001201 exit(1);
1202 }
1203
Marc Bouchere6869a82000-03-20 06:03:29 +00001204 /* Prepend to list. */
1205 me->next = iptables_targets;
1206 iptables_targets = me;
1207 me->t = NULL;
1208 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001209}
1210
1211static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001212print_num(u_int64_t number, unsigned int format)
1213{
1214 if (format & FMT_KILOMEGAGIGA) {
1215 if (number > 99999) {
1216 number = (number + 500) / 1000;
1217 if (number > 9999) {
1218 number = (number + 500) / 1000;
1219 if (number > 9999) {
1220 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001221 if (number > 9999) {
1222 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001223 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001224 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001225 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001226 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001227 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001228 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001229 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001230 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001231 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001232 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001233 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001234}
1235
1236
1237static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001238print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1239{
1240 struct ipt_counters counters;
1241 const char *pol = iptc_get_policy(chain, &counters, handle);
1242 printf("Chain %s", chain);
1243 if (pol) {
1244 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001245 if (!(format & FMT_NOCOUNTS)) {
1246 fputc(' ', stdout);
1247 print_num(counters.pcnt, (format|FMT_NOTABLE));
1248 fputs("packets, ", stdout);
1249 print_num(counters.bcnt, (format|FMT_NOTABLE));
1250 fputs("bytes", stdout);
1251 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001252 printf(")\n");
1253 } else {
1254 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001255 if (!iptc_get_references(&refs, chain, handle))
1256 printf(" (ERROR obtaining refs)\n");
1257 else
1258 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001259 }
1260
1261 if (format & FMT_LINENUMBERS)
1262 printf(FMT("%-4s ", "%s "), "num");
1263 if (!(format & FMT_NOCOUNTS)) {
1264 if (format & FMT_KILOMEGAGIGA) {
1265 printf(FMT("%5s ","%s "), "pkts");
1266 printf(FMT("%5s ","%s "), "bytes");
1267 } else {
1268 printf(FMT("%8s ","%s "), "pkts");
1269 printf(FMT("%10s ","%s "), "bytes");
1270 }
1271 }
1272 if (!(format & FMT_NOTARGET))
1273 printf(FMT("%-9s ","%s "), "target");
1274 fputs(" prot ", stdout);
1275 if (format & FMT_OPTIONS)
1276 fputs("opt", stdout);
1277 if (format & FMT_VIA) {
1278 printf(FMT(" %-6s ","%s "), "in");
1279 printf(FMT("%-6s ","%s "), "out");
1280 }
1281 printf(FMT(" %-19s ","%s "), "source");
1282 printf(FMT(" %-19s "," %s "), "destination");
1283 printf("\n");
1284}
1285
Marc Bouchere6869a82000-03-20 06:03:29 +00001286
1287static int
1288print_match(const struct ipt_entry_match *m,
1289 const struct ipt_ip *ip,
1290 int numeric)
1291{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001292 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001293
1294 if (match) {
1295 if (match->print)
1296 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001297 else
Rusty Russellb039b022000-09-01 06:04:05 +00001298 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001299 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001300 if (m->u.user.name[0])
1301 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001302 }
1303 /* Don't stop iterating. */
1304 return 0;
1305}
1306
1307/* e is called `fw' here for hysterical raisins */
1308static void
1309print_firewall(const struct ipt_entry *fw,
1310 const char *targname,
1311 unsigned int num,
1312 unsigned int format,
1313 const iptc_handle_t handle)
1314{
1315 struct iptables_target *target = NULL;
1316 const struct ipt_entry_target *t;
1317 u_int8_t flags;
1318 char buf[BUFSIZ];
1319
Marc Bouchere6869a82000-03-20 06:03:29 +00001320 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001321 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001322 else
Rusty Russell52a51492000-05-02 16:44:29 +00001323 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001324
1325 t = ipt_get_target((struct ipt_entry *)fw);
1326 flags = fw->ip.flags;
1327
1328 if (format & FMT_LINENUMBERS)
1329 printf(FMT("%-4u ", "%u "), num+1);
1330
1331 if (!(format & FMT_NOCOUNTS)) {
1332 print_num(fw->counters.pcnt, format);
1333 print_num(fw->counters.bcnt, format);
1334 }
1335
1336 if (!(format & FMT_NOTARGET))
1337 printf(FMT("%-9s ", "%s "), targname);
1338
1339 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1340 {
Rusty Russell28381a42000-05-10 00:19:50 +00001341 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001342 if (pname)
1343 printf(FMT("%-5s", "%s "), pname);
1344 else
1345 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1346 }
1347
1348 if (format & FMT_OPTIONS) {
1349 if (format & FMT_NOTABLE)
1350 fputs("opt ", stdout);
1351 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1352 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1353 fputc(' ', stdout);
1354 }
1355
1356 if (format & FMT_VIA) {
1357 char iface[IFNAMSIZ+2];
1358
1359 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1360 iface[0] = '!';
1361 iface[1] = '\0';
1362 }
1363 else iface[0] = '\0';
1364
1365 if (fw->ip.iniface[0] != '\0') {
1366 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001367 }
1368 else if (format & FMT_NUMERIC) strcat(iface, "*");
1369 else strcat(iface, "any");
1370 printf(FMT(" %-6s ","in %s "), iface);
1371
1372 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1373 iface[0] = '!';
1374 iface[1] = '\0';
1375 }
1376 else iface[0] = '\0';
1377
1378 if (fw->ip.outiface[0] != '\0') {
1379 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001380 }
1381 else if (format & FMT_NUMERIC) strcat(iface, "*");
1382 else strcat(iface, "any");
1383 printf(FMT("%-6s ","out %s "), iface);
1384 }
1385
1386 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1387 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1388 printf(FMT("%-19s ","%s "), "anywhere");
1389 else {
1390 if (format & FMT_NUMERIC)
1391 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1392 else
1393 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1394 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1395 printf(FMT("%-19s ","%s "), buf);
1396 }
1397
1398 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1399 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001400 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001401 else {
1402 if (format & FMT_NUMERIC)
1403 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1404 else
1405 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1406 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001407 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001408 }
1409
1410 if (format & FMT_NOTABLE)
1411 fputs(" ", stdout);
1412
1413 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1414
1415 if (target) {
1416 if (target->print)
1417 /* Print the target information. */
1418 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001419 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001421 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
1423 if (!(format & FMT_NONEWLINE))
1424 fputc('\n', stdout);
1425}
1426
1427static void
1428print_firewall_line(const struct ipt_entry *fw,
1429 const iptc_handle_t h)
1430{
1431 struct ipt_entry_target *t;
1432
1433 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001434 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001435}
1436
1437static int
1438append_entry(const ipt_chainlabel chain,
1439 struct ipt_entry *fw,
1440 unsigned int nsaddrs,
1441 const struct in_addr saddrs[],
1442 unsigned int ndaddrs,
1443 const struct in_addr daddrs[],
1444 int verbose,
1445 iptc_handle_t *handle)
1446{
1447 unsigned int i, j;
1448 int ret = 1;
1449
1450 for (i = 0; i < nsaddrs; i++) {
1451 fw->ip.src.s_addr = saddrs[i].s_addr;
1452 for (j = 0; j < ndaddrs; j++) {
1453 fw->ip.dst.s_addr = daddrs[j].s_addr;
1454 if (verbose)
1455 print_firewall_line(fw, *handle);
1456 ret &= iptc_append_entry(chain, fw, handle);
1457 }
1458 }
1459
1460 return ret;
1461}
1462
1463static int
1464replace_entry(const ipt_chainlabel chain,
1465 struct ipt_entry *fw,
1466 unsigned int rulenum,
1467 const struct in_addr *saddr,
1468 const struct in_addr *daddr,
1469 int verbose,
1470 iptc_handle_t *handle)
1471{
1472 fw->ip.src.s_addr = saddr->s_addr;
1473 fw->ip.dst.s_addr = daddr->s_addr;
1474
1475 if (verbose)
1476 print_firewall_line(fw, *handle);
1477 return iptc_replace_entry(chain, fw, rulenum, handle);
1478}
1479
1480static int
1481insert_entry(const ipt_chainlabel chain,
1482 struct ipt_entry *fw,
1483 unsigned int rulenum,
1484 unsigned int nsaddrs,
1485 const struct in_addr saddrs[],
1486 unsigned int ndaddrs,
1487 const struct in_addr daddrs[],
1488 int verbose,
1489 iptc_handle_t *handle)
1490{
1491 unsigned int i, j;
1492 int ret = 1;
1493
1494 for (i = 0; i < nsaddrs; i++) {
1495 fw->ip.src.s_addr = saddrs[i].s_addr;
1496 for (j = 0; j < ndaddrs; j++) {
1497 fw->ip.dst.s_addr = daddrs[j].s_addr;
1498 if (verbose)
1499 print_firewall_line(fw, *handle);
1500 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1501 }
1502 }
1503
1504 return ret;
1505}
1506
Rusty Russell2e0a3212000-04-19 11:23:18 +00001507static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001508make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001509{
1510 /* Establish mask for comparison */
1511 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001512 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001513 unsigned char *mask, *mptr;
1514
1515 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001516 for (matchp = matches; matchp; matchp = matchp->next)
1517 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001518
Rusty Russell9e1d2142000-04-23 09:11:12 +00001519 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001520 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001521 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001522
Rusty Russell9e1d2142000-04-23 09:11:12 +00001523 memset(mask, 0xFF, sizeof(struct ipt_entry));
1524 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001525
Martin Josefsson78cafda2004-02-02 20:01:18 +00001526 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001527 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001528 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001529 + matchp->match->userspacesize);
1530 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001531 }
1532
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001533 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001534 IPT_ALIGN(sizeof(struct ipt_entry_target))
1535 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001536
1537 return mask;
1538}
1539
Marc Bouchere6869a82000-03-20 06:03:29 +00001540static int
1541delete_entry(const ipt_chainlabel chain,
1542 struct ipt_entry *fw,
1543 unsigned int nsaddrs,
1544 const struct in_addr saddrs[],
1545 unsigned int ndaddrs,
1546 const struct in_addr daddrs[],
1547 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001548 iptc_handle_t *handle,
1549 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001550{
1551 unsigned int i, j;
1552 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001553 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001554
Martin Josefsson78cafda2004-02-02 20:01:18 +00001555 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001556 for (i = 0; i < nsaddrs; i++) {
1557 fw->ip.src.s_addr = saddrs[i].s_addr;
1558 for (j = 0; j < ndaddrs; j++) {
1559 fw->ip.dst.s_addr = daddrs[j].s_addr;
1560 if (verbose)
1561 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001562 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001563 }
1564 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001565 free(mask);
1566
Marc Bouchere6869a82000-03-20 06:03:29 +00001567 return ret;
1568}
1569
Harald Welteae1ff9f2000-12-01 14:26:20 +00001570int
Marc Bouchere6869a82000-03-20 06:03:29 +00001571for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001572 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001573{
1574 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001575 const char *chain;
1576 char *chains;
1577 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001578
Rusty Russell9e1d2142000-04-23 09:11:12 +00001579 chain = iptc_first_chain(handle);
1580 while (chain) {
1581 chaincount++;
1582 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001583 }
1584
Rusty Russell9e1d2142000-04-23 09:11:12 +00001585 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1586 i = 0;
1587 chain = iptc_first_chain(handle);
1588 while (chain) {
1589 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1590 i++;
1591 chain = iptc_next_chain(handle);
1592 }
1593
1594 for (i = 0; i < chaincount; i++) {
1595 if (!builtinstoo
1596 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001597 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001598 continue;
1599 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1600 }
1601
1602 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001603 return ret;
1604}
1605
Harald Welteae1ff9f2000-12-01 14:26:20 +00001606int
Marc Bouchere6869a82000-03-20 06:03:29 +00001607flush_entries(const ipt_chainlabel chain, int verbose,
1608 iptc_handle_t *handle)
1609{
1610 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001611 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001612
1613 if (verbose)
1614 fprintf(stdout, "Flushing chain `%s'\n", chain);
1615 return iptc_flush_entries(chain, handle);
1616}
Marc Bouchere6869a82000-03-20 06:03:29 +00001617
1618static int
1619zero_entries(const ipt_chainlabel chain, int verbose,
1620 iptc_handle_t *handle)
1621{
1622 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001623 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001624
Marc Bouchere6869a82000-03-20 06:03:29 +00001625 if (verbose)
1626 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1627 return iptc_zero_entries(chain, handle);
1628}
1629
Harald Welteae1ff9f2000-12-01 14:26:20 +00001630int
Marc Bouchere6869a82000-03-20 06:03:29 +00001631delete_chain(const ipt_chainlabel chain, int verbose,
1632 iptc_handle_t *handle)
1633{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001634 if (!chain)
1635 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001636
1637 if (verbose)
1638 fprintf(stdout, "Deleting chain `%s'\n", chain);
1639 return iptc_delete_chain(chain, handle);
1640}
1641
1642static int
1643list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1644 int expanded, int linenumbers, iptc_handle_t *handle)
1645{
1646 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001647 unsigned int format;
1648 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001649
1650 format = FMT_OPTIONS;
1651 if (!verbose)
1652 format |= FMT_NOCOUNTS;
1653 else
1654 format |= FMT_VIA;
1655
1656 if (numeric)
1657 format |= FMT_NUMERIC;
1658
1659 if (!expanded)
1660 format |= FMT_KILOMEGAGIGA;
1661
1662 if (linenumbers)
1663 format |= FMT_LINENUMBERS;
1664
Rusty Russell9e1d2142000-04-23 09:11:12 +00001665 for (this = iptc_first_chain(handle);
1666 this;
1667 this = iptc_next_chain(handle)) {
1668 const struct ipt_entry *i;
1669 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001670
Marc Bouchere6869a82000-03-20 06:03:29 +00001671 if (chain && strcmp(chain, this) != 0)
1672 continue;
1673
1674 if (found) printf("\n");
1675
1676 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001677 i = iptc_first_rule(this, handle);
1678
1679 num = 0;
1680 while (i) {
1681 print_firewall(i,
1682 iptc_get_target(i, handle),
1683 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001684 format,
1685 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001686 i = iptc_next_rule(i, handle);
1687 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001688 found = 1;
1689 }
1690
1691 errno = ENOENT;
1692 return found;
1693}
1694
Harald Welte82dd2ec2000-12-19 05:18:15 +00001695static char *get_modprobe(void)
1696{
1697 int procfile;
1698 char *ret;
1699
Harald Welte10f7f142004-10-22 08:14:07 +00001700#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001701 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1702 if (procfile < 0)
1703 return NULL;
1704
Harald Welte10f7f142004-10-22 08:14:07 +00001705 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001706 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001707 memset(ret, 0, PROCFILE_BUFSIZ);
1708 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001709 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001710 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001711 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001712 if (ret[strlen(ret)-1]=='\n')
1713 ret[strlen(ret)-1]=0;
1714 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001715 return ret;
1716 }
1717 fail:
1718 free(ret);
1719 close(procfile);
1720 return NULL;
1721}
1722
Harald Welte58918652001-06-16 18:25:25 +00001723int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001724{
1725 char *buf = NULL;
1726 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001727 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001728
1729 /* If they don't explicitly set it, read out of kernel */
1730 if (!modprobe) {
1731 buf = get_modprobe();
1732 if (!buf)
1733 return -1;
1734 modprobe = buf;
1735 }
1736
1737 switch (fork()) {
1738 case 0:
1739 argv[0] = (char *)modprobe;
1740 argv[1] = (char *)modname;
1741 argv[2] = NULL;
1742 execv(argv[0], argv);
1743
1744 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001745 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001746 case -1:
1747 return -1;
1748
1749 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001750 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001751 }
1752
1753 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001754 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1755 return 0;
1756 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001757}
1758
Marc Bouchere6869a82000-03-20 06:03:29 +00001759static struct ipt_entry *
1760generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001761 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001762 struct ipt_entry_target *target)
1763{
1764 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001765 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001766 struct ipt_entry *e;
1767
1768 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001769 for (matchp = matches; matchp; matchp = matchp->next)
1770 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001771
Rusty Russell228e98d2000-04-27 10:28:06 +00001772 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001773 *e = *fw;
1774 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001775 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001776
1777 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001778 for (matchp = matches; matchp; matchp = matchp->next) {
1779 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1780 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001782 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001783
1784 return e;
1785}
1786
Martin Josefsson78cafda2004-02-02 20:01:18 +00001787void clear_rule_matches(struct iptables_rule_match **matches)
1788{
1789 struct iptables_rule_match *matchp, *tmp;
1790
1791 for (matchp = *matches; matchp;) {
1792 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001793 if (matchp->match->m)
1794 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001795 free(matchp);
1796 matchp = tmp;
1797 }
1798
1799 *matches = NULL;
1800}
1801
Rusty Russell3aef54d2005-01-03 03:48:40 +00001802static void set_revision(char *name, u_int8_t revision)
1803{
1804 /* Old kernel sources don't have ".revision" field,
1805 but we stole a byte from name. */
1806 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1807 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1808}
1809
Phil Oester8cf65912005-09-19 15:00:33 +00001810void
1811get_kernel_version(void) {
1812 static struct utsname uts;
1813 int x = 0, y = 0, z = 0;
1814
1815 if (uname(&uts) == -1) {
1816 fprintf(stderr, "Unable to retrieve kernel version.\n");
1817 free_opts(1);
1818 exit(1);
1819 }
1820
1821 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1822 kernel_version = LINUX_VERSION(x, y, z);
1823}
1824
Marc Bouchere6869a82000-03-20 06:03:29 +00001825int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1826{
1827 struct ipt_entry fw, *e = NULL;
1828 int invert = 0;
1829 unsigned int nsaddrs = 0, ndaddrs = 0;
1830 struct in_addr *saddrs = NULL, *daddrs = NULL;
1831
1832 int c, verbose = 0;
1833 const char *chain = NULL;
1834 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1835 const char *policy = NULL, *newname = NULL;
1836 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001837 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001838 int ret = 1;
1839 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001840 struct iptables_rule_match *matches = NULL;
1841 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001842 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001843 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 const char *jumpto = "";
1845 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001846 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001847 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001848
1849 memset(&fw, 0, sizeof(fw));
1850
Harald Welteae1ff9f2000-12-01 14:26:20 +00001851 /* re-set optind to 0 in case do_command gets called
1852 * a second time */
1853 optind = 0;
1854
1855 /* clear mflags in case do_command gets called a second time
1856 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001857 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001858 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001859
1860 for (t = iptables_targets; t; t = t->next) {
1861 t->tflags = 0;
1862 t->used = 0;
1863 }
1864
Marc Bouchere6869a82000-03-20 06:03:29 +00001865 /* Suppress error messages: we may add new options if we
1866 demand-load a protocol. */
1867 opterr = 0;
1868
1869 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001870 "-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 +00001871 opts, NULL)) != -1) {
1872 switch (c) {
1873 /*
1874 * Command selection
1875 */
1876 case 'A':
1877 add_command(&command, CMD_APPEND, CMD_NONE,
1878 invert);
1879 chain = optarg;
1880 break;
1881
1882 case 'D':
1883 add_command(&command, CMD_DELETE, CMD_NONE,
1884 invert);
1885 chain = optarg;
1886 if (optind < argc && argv[optind][0] != '-'
1887 && argv[optind][0] != '!') {
1888 rulenum = parse_rulenumber(argv[optind++]);
1889 command = CMD_DELETE_NUM;
1890 }
1891 break;
1892
Marc Bouchere6869a82000-03-20 06:03:29 +00001893 case 'R':
1894 add_command(&command, CMD_REPLACE, CMD_NONE,
1895 invert);
1896 chain = optarg;
1897 if (optind < argc && argv[optind][0] != '-'
1898 && argv[optind][0] != '!')
1899 rulenum = parse_rulenumber(argv[optind++]);
1900 else
1901 exit_error(PARAMETER_PROBLEM,
1902 "-%c requires a rule number",
1903 cmd2char(CMD_REPLACE));
1904 break;
1905
1906 case 'I':
1907 add_command(&command, CMD_INSERT, CMD_NONE,
1908 invert);
1909 chain = optarg;
1910 if (optind < argc && argv[optind][0] != '-'
1911 && argv[optind][0] != '!')
1912 rulenum = parse_rulenumber(argv[optind++]);
1913 else rulenum = 1;
1914 break;
1915
1916 case 'L':
1917 add_command(&command, CMD_LIST, CMD_ZERO,
1918 invert);
1919 if (optarg) chain = optarg;
1920 else if (optind < argc && argv[optind][0] != '-'
1921 && argv[optind][0] != '!')
1922 chain = argv[optind++];
1923 break;
1924
1925 case 'F':
1926 add_command(&command, CMD_FLUSH, CMD_NONE,
1927 invert);
1928 if (optarg) chain = optarg;
1929 else if (optind < argc && argv[optind][0] != '-'
1930 && argv[optind][0] != '!')
1931 chain = argv[optind++];
1932 break;
1933
1934 case 'Z':
1935 add_command(&command, CMD_ZERO, CMD_LIST,
1936 invert);
1937 if (optarg) chain = optarg;
1938 else if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 chain = argv[optind++];
1941 break;
1942
1943 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001944 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001945 exit_error(PARAMETER_PROBLEM,
1946 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001947 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001948 if (find_target(optarg, TRY_LOAD))
1949 exit_error(PARAMETER_PROBLEM,
1950 "chain name may not clash "
1951 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001952 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1953 invert);
1954 chain = optarg;
1955 break;
1956
1957 case 'X':
1958 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1959 invert);
1960 if (optarg) chain = optarg;
1961 else if (optind < argc && argv[optind][0] != '-'
1962 && argv[optind][0] != '!')
1963 chain = argv[optind++];
1964 break;
1965
1966 case 'E':
1967 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1968 invert);
1969 chain = optarg;
1970 if (optind < argc && argv[optind][0] != '-'
1971 && argv[optind][0] != '!')
1972 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001973 else
1974 exit_error(PARAMETER_PROBLEM,
1975 "-%c requires old-chain-name and "
1976 "new-chain-name",
1977 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001978 break;
1979
1980 case 'P':
1981 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1982 invert);
1983 chain = optarg;
1984 if (optind < argc && argv[optind][0] != '-'
1985 && argv[optind][0] != '!')
1986 policy = argv[optind++];
1987 else
1988 exit_error(PARAMETER_PROBLEM,
1989 "-%c requires a chain and a policy",
1990 cmd2char(CMD_SET_POLICY));
1991 break;
1992
1993 case 'h':
1994 if (!optarg)
1995 optarg = argv[optind];
1996
Rusty Russell2e0a3212000-04-19 11:23:18 +00001997 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001998 if (!matches && protocol)
1999 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002000
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002001 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002002
2003 /*
2004 * Option selection
2005 */
2006 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002007 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2009 invert);
2010
2011 /* Canonicalize into lower case */
2012 for (protocol = argv[optind-1]; *protocol; protocol++)
2013 *protocol = tolower(*protocol);
2014
2015 protocol = argv[optind-1];
2016 fw.ip.proto = parse_protocol(protocol);
2017
2018 if (fw.ip.proto == 0
2019 && (fw.ip.invflags & IPT_INV_PROTO))
2020 exit_error(PARAMETER_PROBLEM,
2021 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002022 break;
2023
2024 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002025 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2027 invert);
2028 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002029 break;
2030
2031 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002032 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002033 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2034 invert);
2035 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002036 break;
2037
2038 case 'j':
2039 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2040 invert);
2041 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002042 /* TRY_LOAD (may be chain name) */
2043 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002044
2045 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002046 size_t size;
2047
Rusty Russell73f72f52000-07-03 10:17:57 +00002048 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2049 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002050
Rusty Russell2e0a3212000-04-19 11:23:18 +00002051 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002052 target->t->u.target_size = size;
2053 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002054 set_revision(target->t->u.user.name,
2055 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002056 if (target->init != NULL)
2057 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002058 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002059 }
2060 break;
2061
2062
2063 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002064 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002065 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2066 invert);
2067 parse_interface(argv[optind-1],
2068 fw.ip.iniface,
2069 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 break;
2071
2072 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002073 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2075 invert);
2076 parse_interface(argv[optind-1],
2077 fw.ip.outiface,
2078 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002079 break;
2080
2081 case 'f':
2082 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2083 invert);
2084 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002085 break;
2086
2087 case 'v':
2088 if (!verbose)
2089 set_option(&options, OPT_VERBOSE,
2090 &fw.ip.invflags, invert);
2091 verbose++;
2092 break;
2093
Rusty Russell52a51492000-05-02 16:44:29 +00002094 case 'm': {
2095 size_t size;
2096
Marc Bouchere6869a82000-03-20 06:03:29 +00002097 if (invert)
2098 exit_error(PARAMETER_PROBLEM,
2099 "unexpected ! flag before --match");
2100
Martin Josefsson78cafda2004-02-02 20:01:18 +00002101 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002102 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2103 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002104 m->m = fw_calloc(1, size);
2105 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002106 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002107 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002108 if (m->init != NULL)
2109 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002110 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002111 }
2112 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002113
2114 case 'n':
2115 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2116 invert);
2117 break;
2118
2119 case 't':
2120 if (invert)
2121 exit_error(PARAMETER_PROBLEM,
2122 "unexpected ! flag before --table");
2123 *table = argv[optind-1];
2124 break;
2125
2126 case 'x':
2127 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2128 invert);
2129 break;
2130
2131 case 'V':
2132 if (invert)
2133 printf("Not %s ;-)\n", program_version);
2134 else
2135 printf("%s v%s\n",
2136 program_name, program_version);
2137 exit(0);
2138
2139 case '0':
2140 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2141 invert);
2142 break;
2143
Harald Welte82dd2ec2000-12-19 05:18:15 +00002144 case 'M':
2145 modprobe = optarg;
2146 break;
2147
Harald Welteccd49e52001-01-23 22:54:34 +00002148 case 'c':
2149
2150 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2151 invert);
2152 pcnt = optarg;
2153 if (optind < argc && argv[optind][0] != '-'
2154 && argv[optind][0] != '!')
2155 bcnt = argv[optind++];
2156 else
2157 exit_error(PARAMETER_PROBLEM,
2158 "-%c requires packet and byte counter",
2159 opt2char(OPT_COUNTERS));
2160
Martin Josefssona28d4952004-05-26 16:04:48 +00002161 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002162 exit_error(PARAMETER_PROBLEM,
2163 "-%c packet counter not numeric",
2164 opt2char(OPT_COUNTERS));
2165
Martin Josefssona28d4952004-05-26 16:04:48 +00002166 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002167 exit_error(PARAMETER_PROBLEM,
2168 "-%c byte counter not numeric",
2169 opt2char(OPT_COUNTERS));
2170
2171 break;
2172
2173
Marc Bouchere6869a82000-03-20 06:03:29 +00002174 case 1: /* non option */
2175 if (optarg[0] == '!' && optarg[1] == '\0') {
2176 if (invert)
2177 exit_error(PARAMETER_PROBLEM,
2178 "multiple consecutive ! not"
2179 " allowed");
2180 invert = TRUE;
2181 optarg[0] = '\0';
2182 continue;
2183 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002184 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002185 exit_tryhelp(2);
2186
2187 default:
2188 /* FIXME: This scheme doesn't allow two of the same
2189 matches --RR */
2190 if (!target
2191 || !(target->parse(c - target->option_offset,
2192 argv, invert,
2193 &target->tflags,
2194 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002195 for (matchp = matches; matchp; matchp = matchp->next) {
2196 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002197 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002198 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002199 &fw,
2200 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002201 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002202 break;
2203 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002204 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002205
2206 /* If you listen carefully, you can
2207 actually hear this code suck. */
2208
2209 /* some explanations (after four different bugs
2210 * in 3 different releases): If we encountere a
2211 * parameter, that has not been parsed yet,
2212 * it's not an option of an explicitly loaded
2213 * match or a target. However, we support
2214 * implicit loading of the protocol match
2215 * extension. '-p tcp' means 'l4 proto 6' and
2216 * at the same time 'load tcp protocol match on
2217 * demand if we specify --dport'.
2218 *
2219 * To make this work, we need to make sure:
2220 * - the parameter has not been parsed by
2221 * a match (m above)
2222 * - a protocol has been specified
2223 * - the protocol extension has not been
2224 * loaded yet, or is loaded and unused
2225 * [think of iptables-restore!]
2226 * - the protocol extension can be successively
2227 * loaded
2228 */
2229 if (m == NULL
2230 && protocol
2231 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002232 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002233 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002234 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002235 && (proto_used == 0))
2236 )
2237 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002238 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002239 /* Try loading protocol */
2240 size_t size;
2241
2242 proto_used = 1;
2243
2244 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2245 + m->size;
2246
2247 m->m = fw_calloc(1, size);
2248 m->m->u.match_size = size;
2249 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002250 set_revision(m->m->u.user.name,
2251 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002252 if (m->init != NULL)
2253 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002254
2255 opts = merge_options(opts,
2256 m->extra_opts, &m->option_offset);
2257
2258 optind--;
2259 continue;
2260 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002261 if (!m)
2262 exit_error(PARAMETER_PROBLEM,
2263 "Unknown arg `%s'",
2264 argv[optind-1]);
2265 }
2266 }
2267 invert = FALSE;
2268 }
2269
Martin Josefsson78cafda2004-02-02 20:01:18 +00002270 for (matchp = matches; matchp; matchp = matchp->next)
2271 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002272
Marc Bouchere6869a82000-03-20 06:03:29 +00002273 if (target)
2274 target->final_check(target->tflags);
2275
2276 /* Fix me: must put inverse options checking here --MN */
2277
2278 if (optind < argc)
2279 exit_error(PARAMETER_PROBLEM,
2280 "unknown arguments found on commandline");
2281 if (!command)
2282 exit_error(PARAMETER_PROBLEM, "no command specified");
2283 if (invert)
2284 exit_error(PARAMETER_PROBLEM,
2285 "nothing appropriate following !");
2286
Harald Welte6336bfd2002-05-07 14:41:43 +00002287 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002288 if (!(options & OPT_DESTINATION))
2289 dhostnetworkmask = "0.0.0.0/0";
2290 if (!(options & OPT_SOURCE))
2291 shostnetworkmask = "0.0.0.0/0";
2292 }
2293
2294 if (shostnetworkmask)
2295 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2296 &(fw.ip.smsk), &nsaddrs);
2297
2298 if (dhostnetworkmask)
2299 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2300 &(fw.ip.dmsk), &ndaddrs);
2301
2302 if ((nsaddrs > 1 || ndaddrs > 1) &&
2303 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2304 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2305 " source or destination IP addresses");
2306
Marc Bouchere6869a82000-03-20 06:03:29 +00002307 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2308 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2309 "specify a unique address");
2310
2311 generic_opt_check(command, options);
2312
2313 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2314 exit_error(PARAMETER_PROBLEM,
2315 "chain name `%s' too long (must be under %i chars)",
2316 chain, IPT_FUNCTION_MAXNAMELEN);
2317
Harald Welteae1ff9f2000-12-01 14:26:20 +00002318 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002319 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002320 *handle = iptc_init(*table);
2321
Rusty Russell8beb0492004-12-22 00:37:10 +00002322 /* try to insmod the module if iptc_init failed */
2323 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002324 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002325
Marc Bouchere6869a82000-03-20 06:03:29 +00002326 if (!*handle)
2327 exit_error(VERSION_PROBLEM,
2328 "can't initialize iptables table `%s': %s",
2329 *table, iptc_strerror(errno));
2330
Harald Welte6336bfd2002-05-07 14:41:43 +00002331 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002332 || command == CMD_DELETE
2333 || command == CMD_INSERT
2334 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002335 if (strcmp(chain, "PREROUTING") == 0
2336 || strcmp(chain, "INPUT") == 0) {
2337 /* -o not valid with incoming packets. */
2338 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002339 exit_error(PARAMETER_PROBLEM,
2340 "Can't use -%c with %s\n",
2341 opt2char(OPT_VIANAMEOUT),
2342 chain);
2343 }
2344
Rusty Russella4860fd2000-06-17 16:13:02 +00002345 if (strcmp(chain, "POSTROUTING") == 0
2346 || strcmp(chain, "OUTPUT") == 0) {
2347 /* -i not valid with outgoing packets */
2348 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002349 exit_error(PARAMETER_PROBLEM,
2350 "Can't use -%c with %s\n",
2351 opt2char(OPT_VIANAMEIN),
2352 chain);
2353 }
2354
2355 if (target && iptc_is_chain(jumpto, *handle)) {
2356 printf("Warning: using chain %s, not extension\n",
2357 jumpto);
2358
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002359 if (target->t)
2360 free(target->t);
2361
Marc Bouchere6869a82000-03-20 06:03:29 +00002362 target = NULL;
2363 }
2364
2365 /* If they didn't specify a target, or it's a chain
2366 name, use standard. */
2367 if (!target
2368 && (strlen(jumpto) == 0
2369 || iptc_is_chain(jumpto, *handle))) {
2370 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002371
Rusty Russell52a51492000-05-02 16:44:29 +00002372 target = find_target(IPT_STANDARD_TARGET,
2373 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002374
2375 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002376 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002377 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002378 target->t->u.target_size = size;
2379 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002380 set_revision(target->t->u.user.name, target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002381 if (target->init != NULL)
2382 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002383 }
2384
Rusty Russell7e53bf92000-03-20 07:03:28 +00002385 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002386 /* it is no chain, and we can't load a plugin.
2387 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002388 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002389 * chain. */
2390 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002391 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002392 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002393 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002394 }
2395 }
2396
2397 switch (command) {
2398 case CMD_APPEND:
2399 ret = append_entry(chain, e,
2400 nsaddrs, saddrs, ndaddrs, daddrs,
2401 options&OPT_VERBOSE,
2402 handle);
2403 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002404 case CMD_DELETE:
2405 ret = delete_entry(chain, e,
2406 nsaddrs, saddrs, ndaddrs, daddrs,
2407 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002408 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002409 break;
2410 case CMD_DELETE_NUM:
2411 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2412 break;
2413 case CMD_REPLACE:
2414 ret = replace_entry(chain, e, rulenum - 1,
2415 saddrs, daddrs, options&OPT_VERBOSE,
2416 handle);
2417 break;
2418 case CMD_INSERT:
2419 ret = insert_entry(chain, e, rulenum - 1,
2420 nsaddrs, saddrs, ndaddrs, daddrs,
2421 options&OPT_VERBOSE,
2422 handle);
2423 break;
2424 case CMD_LIST:
2425 ret = list_entries(chain,
2426 options&OPT_VERBOSE,
2427 options&OPT_NUMERIC,
2428 options&OPT_EXPANDED,
2429 options&OPT_LINENUMBERS,
2430 handle);
2431 break;
2432 case CMD_FLUSH:
2433 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2434 break;
2435 case CMD_ZERO:
2436 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2437 break;
2438 case CMD_LIST|CMD_ZERO:
2439 ret = list_entries(chain,
2440 options&OPT_VERBOSE,
2441 options&OPT_NUMERIC,
2442 options&OPT_EXPANDED,
2443 options&OPT_LINENUMBERS,
2444 handle);
2445 if (ret)
2446 ret = zero_entries(chain,
2447 options&OPT_VERBOSE, handle);
2448 break;
2449 case CMD_NEW_CHAIN:
2450 ret = iptc_create_chain(chain, handle);
2451 break;
2452 case CMD_DELETE_CHAIN:
2453 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2454 break;
2455 case CMD_RENAME_CHAIN:
2456 ret = iptc_rename_chain(chain, newname, handle);
2457 break;
2458 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002459 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002460 break;
2461 default:
2462 /* We should never reach this... */
2463 exit_tryhelp(2);
2464 }
2465
2466 if (verbose > 1)
2467 dump_entries(*handle);
2468
Martin Josefsson78cafda2004-02-02 20:01:18 +00002469 clear_rule_matches(&matches);
2470
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002471 if (e != NULL) {
2472 free(e);
2473 e = NULL;
2474 }
2475
keso6997cdf2004-07-04 15:20:53 +00002476 free(saddrs);
2477 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002478 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002479
Marc Bouchere6869a82000-03-20 06:03:29 +00002480 return ret;
2481}