blob: 28917cf1b669161d7001f1e0e75057b105838cbd [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
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000083#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' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000137 { "goto", 1, 0, 'g' },
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{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162 /* -n -s -d -p -j -v -x -i -o -f --line -c */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
165/*DELETE_NUM*/{'x','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',' ','x'},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x','x'},
175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000176};
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,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000190/*--line*/ 0,
191/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000192};
193
194const char *program_version;
195const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000196char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000197
Phil Oester8cf65912005-09-19 15:00:33 +0000198int kernel_version;
199
Rusty Russell2e0a3212000-04-19 11:23:18 +0000200/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000201struct iptables_match *iptables_matches = NULL;
202struct iptables_target *iptables_targets = NULL;
203
204/* Extra debugging from libiptc */
205extern void dump_entries(const iptc_handle_t handle);
206
207/* A few hardcoded protocols for 'all' and in case the user has no
208 /etc/protocols */
209struct pprot {
210 char *name;
211 u_int8_t num;
212};
213
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000215/* defined in netinet/in.h */
216#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000217#ifndef IPPROTO_ESP
218#define IPPROTO_ESP 50
219#endif
220#ifndef IPPROTO_AH
221#define IPPROTO_AH 51
222#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000223#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000224
Marc Bouchere6869a82000-03-20 06:03:29 +0000225static const struct pprot chain_protos[] = {
226 { "tcp", IPPROTO_TCP },
227 { "udp", IPPROTO_UDP },
228 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000231 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000232};
233
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000234static 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
Phil Oester58179b12006-07-20 17:00:19 +0000252int
253service_to_port(const char *name, const char *proto)
254{
255 struct servent *service;
256
257 if ((service = getservbyname(name, proto)) != NULL)
258 return ntohs((unsigned short) service->s_port);
259
260 return -1;
261}
262
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000263u_int16_t
264parse_port(const char *port, const char *proto)
265{
266 unsigned int portnum;
267
268 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
269 (portnum = service_to_port(port, proto)) != -1)
270 return (u_int16_t)portnum;
271
272 exit_error(PARAMETER_PROBLEM,
273 "invalid port/service `%s' specified", port);
274}
275
Marc Bouchere6869a82000-03-20 06:03:29 +0000276struct in_addr *
277dotted_to_addr(const char *dotted)
278{
279 static struct in_addr addr;
280 unsigned char *addrp;
281 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000282 unsigned int onebyte;
283 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000284 char buf[20];
285
286 /* copy dotted string, because we need to modify it */
287 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000288 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000289 addrp = (unsigned char *) &(addr.s_addr);
290
291 p = buf;
292 for (i = 0; i < 3; i++) {
293 if ((q = strchr(p, '.')) == NULL)
294 return (struct in_addr *) NULL;
295
296 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000297 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 return (struct in_addr *) NULL;
299
300 addrp[i] = (unsigned char) onebyte;
301 p = q + 1;
302 }
303
304 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000305 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000306 return (struct in_addr *) NULL;
307
308 addrp[3] = (unsigned char) onebyte;
309
310 return &addr;
311}
312
313static struct in_addr *
314network_to_addr(const char *name)
315{
316 struct netent *net;
317 static struct in_addr addr;
318
319 if ((net = getnetbyname(name)) != NULL) {
320 if (net->n_addrtype != AF_INET)
321 return (struct in_addr *) NULL;
322 addr.s_addr = htonl((unsigned long) net->n_net);
323 return &addr;
324 }
325
326 return (struct in_addr *) NULL;
327}
328
329static void
330inaddrcpy(struct in_addr *dst, struct in_addr *src)
331{
332 /* memcpy(dst, src, sizeof(struct in_addr)); */
333 dst->s_addr = src->s_addr;
334}
335
Pablo Neiradfdcd642005-05-29 19:05:23 +0000336static void free_opts(int reset_offset)
337{
338 if (opts != original_opts) {
339 free(opts);
340 opts = original_opts;
341 if (reset_offset)
342 global_option_offset = 0;
343 }
344}
345
Marc Bouchere6869a82000-03-20 06:03:29 +0000346void
347exit_error(enum exittype status, char *msg, ...)
348{
349 va_list args;
350
351 va_start(args, msg);
352 fprintf(stderr, "%s v%s: ", program_name, program_version);
353 vfprintf(stderr, msg, args);
354 va_end(args);
355 fprintf(stderr, "\n");
356 if (status == PARAMETER_PROBLEM)
357 exit_tryhelp(status);
358 if (status == VERSION_PROBLEM)
359 fprintf(stderr,
360 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000361 /* On error paths, make sure that we don't leak memory */
362 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000363 exit(status);
364}
365
366void
367exit_tryhelp(int status)
368{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000369 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000370 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000371 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
372 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000373 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000374 exit(status);
375}
376
377void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000378exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000379{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000380 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000381 struct iptables_target *t = NULL;
382
Marc Bouchere6869a82000-03-20 06:03:29 +0000383 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000384"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000385" %s -[RI] chain rulenum rule-specification [options]\n"
386" %s -D chain rulenum [options]\n"
387" %s -[LFZ] [chain] [options]\n"
388" %s -[NX] chain\n"
389" %s -E old-chain-name new-chain-name\n"
390" %s -P chain target [options]\n"
391" %s -h (print this help information)\n\n",
392 program_name, program_version, program_name, program_name,
393 program_name, program_name, program_name, program_name,
394 program_name, program_name);
395
396 printf(
397"Commands:\n"
398"Either long or short options are allowed.\n"
399" --append -A chain Append to chain\n"
400" --delete -D chain Delete matching rule from chain\n"
401" --delete -D chain rulenum\n"
402" Delete rule rulenum (1 = first) from chain\n"
403" --insert -I chain [rulenum]\n"
404" Insert in chain as rulenum (default 1=first)\n"
405" --replace -R chain rulenum\n"
406" Replace rule rulenum (1 = first) in chain\n"
407" --list -L [chain] List the rules in a chain or all chains\n"
408" --flush -F [chain] Delete all rules in chain or all chains\n"
409" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000410" --new -N chain Create a new user-defined chain\n"
411" --delete-chain\n"
412" -X [chain] Delete a user-defined chain\n"
413" --policy -P chain target\n"
414" Change policy on chain to target\n"
415" --rename-chain\n"
416" -E old-chain new-chain\n"
417" Change chain name, (moving any references)\n"
418
419"Options:\n"
420" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
421" --source -s [!] address[/mask]\n"
422" source specification\n"
423" --destination -d [!] address[/mask]\n"
424" destination specification\n"
425" --in-interface -i [!] input name[+]\n"
426" network interface name ([+] for wildcard)\n"
427" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000428" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000429#ifdef IPT_F_GOTO
430" --goto -g chain\n"
431" jump to chain with no return\n"
432#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000433" --match -m match\n"
434" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000435" --numeric -n numeric output of addresses and ports\n"
436" --out-interface -o [!] output name[+]\n"
437" network interface name ([+] for wildcard)\n"
438" --table -t table table to manipulate (default: `filter')\n"
439" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000440" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000441" --exact -x expand numbers (display exact values)\n"
442"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000443" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000444" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000445"[!] --version -V print package version.\n");
446
Rusty Russell363112d2000-08-11 13:49:26 +0000447 /* Print out any special helps. A user might like to be able
448 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000449 results. So we call help for all specified matches & targets */
450 for (t = iptables_targets; t ;t = t->next) {
451 if (t->used) {
452 printf("\n");
453 t->help();
454 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000455 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000456 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000457 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000458 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000459 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000460 exit(0);
461}
462
463static void
464generic_opt_check(int command, int options)
465{
466 int i, j, legal = 0;
467
468 /* Check that commands are valid with options. Complicated by the
469 * fact that if an option is legal with *any* command given, it is
470 * legal overall (ie. -z and -l).
471 */
472 for (i = 0; i < NUMBER_OF_OPT; i++) {
473 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
474
475 for (j = 0; j < NUMBER_OF_CMD; j++) {
476 if (!(command & (1<<j)))
477 continue;
478
479 if (!(options & (1<<i))) {
480 if (commands_v_options[j][i] == '+')
481 exit_error(PARAMETER_PROBLEM,
482 "You need to supply the `-%c' "
483 "option for this command\n",
484 optflags[i]);
485 } else {
486 if (commands_v_options[j][i] != 'x')
487 legal = 1;
488 else if (legal == 0)
489 legal = -1;
490 }
491 }
492 if (legal == -1)
493 exit_error(PARAMETER_PROBLEM,
494 "Illegal option `-%c' with this command\n",
495 optflags[i]);
496 }
497}
498
499static char
500opt2char(int option)
501{
502 const char *ptr;
503 for (ptr = optflags; option > 1; option >>= 1, ptr++);
504
505 return *ptr;
506}
507
508static char
509cmd2char(int option)
510{
511 const char *ptr;
512 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
513
514 return *ptr;
515}
516
517static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000518add_command(unsigned int *cmd, const int newcmd, const int othercmds,
519 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000520{
521 if (invert)
522 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
523 if (*cmd & (~othercmds))
524 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
525 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
526 *cmd |= newcmd;
527}
528
529int
Harald Welteb77f1da2002-03-14 11:35:58 +0000530check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000531{
532 if (option && strcmp(option, "!") == 0) {
533 if (*invert)
534 exit_error(PARAMETER_PROBLEM,
535 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000536 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000537 if (optind) {
538 *optind = *optind+1;
539 if (argc && *optind > argc)
540 exit_error(PARAMETER_PROBLEM,
541 "no argument following `!'");
542 }
543
Marc Bouchere6869a82000-03-20 06:03:29 +0000544 return TRUE;
545 }
546 return FALSE;
547}
548
549static void *
550fw_calloc(size_t count, size_t size)
551{
552 void *p;
553
554 if ((p = calloc(count, size)) == NULL) {
555 perror("iptables: calloc failed");
556 exit(1);
557 }
558 return p;
559}
560
561static void *
562fw_malloc(size_t size)
563{
564 void *p;
565
566 if ((p = malloc(size)) == NULL) {
567 perror("iptables: malloc failed");
568 exit(1);
569 }
570 return p;
571}
572
573static struct in_addr *
574host_to_addr(const char *name, unsigned int *naddr)
575{
576 struct hostent *host;
577 struct in_addr *addr;
578 unsigned int i;
579
580 *naddr = 0;
581 if ((host = gethostbyname(name)) != NULL) {
582 if (host->h_addrtype != AF_INET ||
583 host->h_length != sizeof(struct in_addr))
584 return (struct in_addr *) NULL;
585
586 while (host->h_addr_list[*naddr] != (char *) NULL)
587 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000588 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000589 for (i = 0; i < *naddr; i++)
590 inaddrcpy(&(addr[i]),
591 (struct in_addr *) host->h_addr_list[i]);
592 return addr;
593 }
594
595 return (struct in_addr *) NULL;
596}
597
598static char *
599addr_to_host(const struct in_addr *addr)
600{
601 struct hostent *host;
602
603 if ((host = gethostbyaddr((char *) addr,
604 sizeof(struct in_addr), AF_INET)) != NULL)
605 return (char *) host->h_name;
606
607 return (char *) NULL;
608}
609
Phil Oester5549ad02006-07-10 04:52:56 +0000610static void
611pad_cidr(char *cidr)
612{
613 char *p, *q;
614 unsigned int onebyte;
615 int i, j;
616 char buf[20];
617
618 /* copy dotted string, because we need to modify it */
619 strncpy(buf, cidr, sizeof(buf) - 1);
620 buf[sizeof(buf) - 1] = '\0';
621
622 p = buf;
623 for (i = 0; i <= 3; i++) {
624 if ((q = strchr(p, '.')) == NULL)
625 break;
626 *q = '\0';
627 if (string_to_number(p, 0, 255, &onebyte) == -1)
628 return;
629 p = q + 1;
630 }
631
632 /* pad remaining octets with zeros */
633 for (j = i; j < 3; j++) {
634 strcat(cidr, ".0");
635 }
636}
637
Marc Bouchere6869a82000-03-20 06:03:29 +0000638/*
639 * All functions starting with "parse" should succeed, otherwise
640 * the program fails.
641 * Most routines return pointers to static data that may change
642 * between calls to the same or other routines with a few exceptions:
643 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
644 * return global static data.
645*/
646
647static struct in_addr *
648parse_hostnetwork(const char *name, unsigned int *naddrs)
649{
650 struct in_addr *addrp, *addrptmp;
651
652 if ((addrptmp = dotted_to_addr(name)) != NULL ||
653 (addrptmp = network_to_addr(name)) != NULL) {
654 addrp = fw_malloc(sizeof(struct in_addr));
655 inaddrcpy(addrp, addrptmp);
656 *naddrs = 1;
657 return addrp;
658 }
659 if ((addrp = host_to_addr(name, naddrs)) != NULL)
660 return addrp;
661
662 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
663}
664
665static struct in_addr *
666parse_mask(char *mask)
667{
668 static struct in_addr maskaddr;
669 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000670 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000671
672 if (mask == NULL) {
673 /* no mask at all defaults to 32 bits */
674 maskaddr.s_addr = 0xFFFFFFFF;
675 return &maskaddr;
676 }
677 if ((addrp = dotted_to_addr(mask)) != NULL)
678 /* dotted_to_addr already returns a network byte order addr */
679 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000680 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000681 exit_error(PARAMETER_PROBLEM,
682 "invalid mask `%s' specified", mask);
683 if (bits != 0) {
684 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
685 return &maskaddr;
686 }
687
688 maskaddr.s_addr = 0L;
689 return &maskaddr;
690}
691
Marc Boucherb93c7982001-12-06 14:50:19 +0000692void
Marc Bouchere6869a82000-03-20 06:03:29 +0000693parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
694 struct in_addr *maskp, unsigned int *naddrs)
695{
696 struct in_addr *addrp;
697 char buf[256];
698 char *p;
699 int i, j, k, n;
700
701 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000702 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 if ((p = strrchr(buf, '/')) != NULL) {
704 *p = '\0';
705 addrp = parse_mask(p + 1);
Phil Oester5549ad02006-07-10 04:52:56 +0000706 if (strrchr(p + 1, '.') == NULL)
707 pad_cidr(buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000708 } else
709 addrp = parse_mask(NULL);
710 inaddrcpy(maskp, addrp);
711
712 /* if a null mask is given, the name is ignored, like in "any/0" */
713 if (maskp->s_addr == 0L)
714 strcpy(buf, "0.0.0.0");
715
716 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
717 n = *naddrs;
718 for (i = 0, j = 0; i < n; i++) {
719 addrp[j++].s_addr &= maskp->s_addr;
720 for (k = 0; k < j - 1; k++) {
721 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
722 (*naddrs)--;
723 j--;
724 break;
725 }
726 }
727 }
728}
729
730struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000731find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000732{
733 struct iptables_match *ptr;
734
735 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000736 if (strcmp(name, ptr->name) == 0) {
737 struct iptables_match *clone;
738
739 /* First match of this type: */
740 if (ptr->m == NULL)
741 break;
742
743 /* Second and subsequent clones */
744 clone = fw_malloc(sizeof(struct iptables_match));
745 memcpy(clone, ptr, sizeof(struct iptables_match));
746 clone->mflags = 0;
747 /* This is a clone: */
748 clone->next = clone;
749
750 ptr = clone;
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 break;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000752 }
753 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000754
Harald Welte3efb6ea2001-08-06 18:50:21 +0000755#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000756 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000757 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000758 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000759 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000760 if (dlopen(path, RTLD_NOW)) {
761 /* Found library. If it didn't register itself,
762 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000763 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000764
Rusty Russell9e1d2142000-04-23 09:11:12 +0000765 if (!ptr)
766 exit_error(PARAMETER_PROBLEM,
767 "Couldn't load match `%s'\n",
768 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000769 } else if (tryload == LOAD_MUST_SUCCEED)
770 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000771 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000772 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000774#else
775 if (ptr && !ptr->loaded) {
776 if (tryload != DONT_LOAD)
777 ptr->loaded = 1;
778 else
779 ptr = NULL;
780 }
Marc Boucher067477b2002-03-24 15:09:31 +0000781 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
782 exit_error(PARAMETER_PROBLEM,
783 "Couldn't find match `%s'\n", name);
784 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000785#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000786
Martin Josefsson78cafda2004-02-02 20:01:18 +0000787 if (ptr && matches) {
788 struct iptables_rule_match **i;
789 struct iptables_rule_match *newentry;
790
791 newentry = fw_malloc(sizeof(struct iptables_rule_match));
792
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000793 for (i = matches; *i; i = &(*i)->next) {
794 if (strcmp(name, (*i)->match->name) == 0)
795 (*i)->completed = 1;
796 }
Martin Josefsson78cafda2004-02-02 20:01:18 +0000797 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000798 newentry->completed = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000799 newentry->next = NULL;
800 *i = newentry;
801 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000802
Marc Bouchere6869a82000-03-20 06:03:29 +0000803 return ptr;
804}
805
Rusty Russell28381a42000-05-10 00:19:50 +0000806/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
807static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000808find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000809{
Harald Welteed498492001-07-23 01:24:22 +0000810 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000811
Harald Welte0b0013a2002-02-18 16:15:31 +0000812 if (string_to_number(pname, 0, 255, &proto) != -1) {
813 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000814
Harald Welte0b0013a2002-02-18 16:15:31 +0000815 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000816 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000817 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000818 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000819
820 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000821}
822
Marc Boucherb93c7982001-12-06 14:50:19 +0000823u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000824parse_protocol(const char *s)
825{
Harald Welteed498492001-07-23 01:24:22 +0000826 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000827
Harald Welteed498492001-07-23 01:24:22 +0000828 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000829 struct protoent *pent;
830
Harald Weltecbe1ec72006-02-11 09:50:11 +0000831 /* first deal with the special case of 'all' to prevent
832 * people from being able to redefine 'all' in nsswitch
833 * and/or provoke expensive [not working] ldap/nis/...
834 * lookups */
835 if (!strcmp(s, "all"))
836 return 0;
837
Marc Bouchere6869a82000-03-20 06:03:29 +0000838 if ((pent = getprotobyname(s)))
839 proto = pent->p_proto;
840 else {
841 unsigned int i;
842 for (i = 0;
843 i < sizeof(chain_protos)/sizeof(struct pprot);
844 i++) {
845 if (strcmp(s, chain_protos[i].name) == 0) {
846 proto = chain_protos[i].num;
847 break;
848 }
849 }
850 if (i == sizeof(chain_protos)/sizeof(struct pprot))
851 exit_error(PARAMETER_PROBLEM,
852 "unknown protocol `%s' specified",
853 s);
854 }
855 }
856
857 return (u_int16_t)proto;
858}
859
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000860void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000861{
862 int vialen = strlen(arg);
863 unsigned int i;
864
865 memset(mask, 0, IFNAMSIZ);
866 memset(vianame, 0, IFNAMSIZ);
867
868 if (vialen + 1 > IFNAMSIZ)
869 exit_error(PARAMETER_PROBLEM,
870 "interface name `%s' must be shorter than IFNAMSIZ"
871 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000872
Marc Bouchere6869a82000-03-20 06:03:29 +0000873 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000874 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000875 memset(mask, 0, IFNAMSIZ);
876 else if (vianame[vialen - 1] == '+') {
877 memset(mask, 0xFF, vialen - 1);
878 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000879 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000880 } else {
881 /* Include nul-terminator in match */
882 memset(mask, 0xFF, vialen + 1);
883 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000884 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000885 if (!isalnum(vianame[i])
886 && vianame[i] != '_'
887 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000888 printf("Warning: wierd character in interface"
889 " `%s' (No aliases, :, ! or *).\n",
890 vianame);
891 break;
892 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000893 }
894 }
895}
896
897/* Can't be zero. */
898static int
899parse_rulenumber(const char *rule)
900{
Harald Welteed498492001-07-23 01:24:22 +0000901 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000902
Harald Welteed498492001-07-23 01:24:22 +0000903 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000904 exit_error(PARAMETER_PROBLEM,
905 "Invalid rule number `%s'", rule);
906
907 return rulenum;
908}
909
910static const char *
911parse_target(const char *targetname)
912{
913 const char *ptr;
914
915 if (strlen(targetname) < 1)
916 exit_error(PARAMETER_PROBLEM,
917 "Invalid target name (too short)");
918
919 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
920 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000921 "Invalid target name `%s' (%u chars max)",
922 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000923
924 for (ptr = targetname; *ptr; ptr++)
925 if (isspace(*ptr))
926 exit_error(PARAMETER_PROBLEM,
927 "Invalid target name `%s'", targetname);
928 return targetname;
929}
930
931static char *
932addr_to_network(const struct in_addr *addr)
933{
934 struct netent *net;
935
936 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
937 return (char *) net->n_name;
938
939 return (char *) NULL;
940}
941
942char *
943addr_to_dotted(const struct in_addr *addrp)
944{
945 static char buf[20];
946 const unsigned char *bytep;
947
948 bytep = (const unsigned char *) &(addrp->s_addr);
949 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
950 return buf;
951}
Marc Boucherb93c7982001-12-06 14:50:19 +0000952
953char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000954addr_to_anyname(const struct in_addr *addr)
955{
956 char *name;
957
958 if ((name = addr_to_host(addr)) != NULL ||
959 (name = addr_to_network(addr)) != NULL)
960 return name;
961
962 return addr_to_dotted(addr);
963}
964
Marc Boucherb93c7982001-12-06 14:50:19 +0000965char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000966mask_to_dotted(const struct in_addr *mask)
967{
968 int i;
969 static char buf[20];
970 u_int32_t maskaddr, bits;
971
972 maskaddr = ntohl(mask->s_addr);
973
974 if (maskaddr == 0xFFFFFFFFL)
975 /* we don't want to see "/32" */
976 return "";
977
978 i = 32;
979 bits = 0xFFFFFFFEL;
980 while (--i >= 0 && maskaddr != bits)
981 bits <<= 1;
982 if (i >= 0)
983 sprintf(buf, "/%d", i);
984 else
985 /* mask was not a decent combination of 1's and 0's */
986 sprintf(buf, "/%s", addr_to_dotted(mask));
987
988 return buf;
989}
990
991int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000992string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
993 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000994{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000995 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000996 char *end;
997
998 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000999 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +00001000 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +00001001 if (*end == '\0' && end != s) {
1002 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +00001003 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +00001004 *ret = number;
1005 return 0;
1006 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001007 }
1008 return -1;
1009}
1010
Martin Josefssonb105bc92004-05-26 15:54:49 +00001011int
1012string_to_number_l(const char *s, unsigned long min, unsigned long max,
1013 unsigned long *ret)
1014{
1015 int result;
1016 unsigned long long number;
1017
1018 result = string_to_number_ll(s, min, max, &number);
1019 *ret = (unsigned long)number;
1020
1021 return result;
1022}
1023
1024int string_to_number(const char *s, unsigned int min, unsigned int max,
1025 unsigned int *ret)
1026{
1027 int result;
1028 unsigned long number;
1029
1030 result = string_to_number_l(s, min, max, &number);
1031 *ret = (unsigned int)number;
1032
1033 return result;
1034}
1035
Marc Bouchere6869a82000-03-20 06:03:29 +00001036static void
1037set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1038 int invert)
1039{
1040 if (*options & option)
1041 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1042 opt2char(option));
1043 *options |= option;
1044
1045 if (invert) {
1046 unsigned int i;
1047 for (i = 0; 1 << i != option; i++);
1048
1049 if (!inverse_for_options[i])
1050 exit_error(PARAMETER_PROBLEM,
1051 "cannot have ! before -%c",
1052 opt2char(option));
1053 *invflg |= inverse_for_options[i];
1054 }
1055}
1056
1057struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +00001058find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +00001059{
1060 struct iptables_target *ptr;
1061
1062 /* Standard target? */
1063 if (strcmp(name, "") == 0
1064 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
1065 || strcmp(name, IPTC_LABEL_DROP) == 0
1066 || strcmp(name, IPTC_LABEL_QUEUE) == 0
1067 || strcmp(name, IPTC_LABEL_RETURN) == 0)
1068 name = "standard";
1069
1070 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
1071 if (strcmp(name, ptr->name) == 0)
1072 break;
1073 }
1074
Harald Welte3efb6ea2001-08-06 18:50:21 +00001075#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001076 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001077 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001078 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001079 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001080 if (dlopen(path, RTLD_NOW)) {
1081 /* Found library. If it didn't register itself,
1082 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001083 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001084 if (!ptr)
1085 exit_error(PARAMETER_PROBLEM,
1086 "Couldn't load target `%s'\n",
1087 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001088 } else if (tryload == LOAD_MUST_SUCCEED)
1089 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001090 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001091 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001092 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001093#else
1094 if (ptr && !ptr->loaded) {
1095 if (tryload != DONT_LOAD)
1096 ptr->loaded = 1;
1097 else
1098 ptr = NULL;
1099 }
Marc Boucher067477b2002-03-24 15:09:31 +00001100 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1101 exit_error(PARAMETER_PROBLEM,
1102 "Couldn't find target `%s'\n", name);
1103 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001104#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001105
Harald Welteae1ff9f2000-12-01 14:26:20 +00001106 if (ptr)
1107 ptr->used = 1;
1108
Marc Bouchere6869a82000-03-20 06:03:29 +00001109 return ptr;
1110}
1111
1112static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001113merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001114 unsigned int *option_offset)
1115{
1116 unsigned int num_old, num_new, i;
1117 struct option *merge;
1118
1119 for (num_old = 0; oldopts[num_old].name; num_old++);
1120 for (num_new = 0; newopts[num_new].name; num_new++);
1121
1122 global_option_offset += OPTION_OFFSET;
1123 *option_offset = global_option_offset;
1124
1125 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1126 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001127 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001128 for (i = 0; i < num_new; i++) {
1129 merge[num_old + i] = newopts[i];
1130 merge[num_old + i].val += *option_offset;
1131 }
1132 memset(merge + num_old + num_new, 0, sizeof(struct option));
1133
1134 return merge;
1135}
1136
Rusty Russell3aef54d2005-01-03 03:48:40 +00001137static int compatible_revision(const char *name, u_int8_t revision, int opt)
1138{
1139 struct ipt_get_revision rev;
1140 socklen_t s = sizeof(rev);
1141 int max_rev, sockfd;
1142
1143 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1144 if (sockfd < 0) {
1145 fprintf(stderr, "Could not open socket to kernel: %s\n",
1146 strerror(errno));
1147 exit(1);
1148 }
1149
1150 strcpy(rev.name, name);
1151 rev.revision = revision;
1152
1153 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1154 if (max_rev < 0) {
1155 /* Definitely don't support this? */
1156 if (errno == EPROTONOSUPPORT) {
1157 close(sockfd);
1158 return 0;
1159 } else if (errno == ENOPROTOOPT) {
1160 close(sockfd);
1161 /* Assume only revision 0 support (old kernel) */
1162 return (revision == 0);
1163 } else {
1164 fprintf(stderr, "getsockopt failed strangely: %s\n",
1165 strerror(errno));
1166 exit(1);
1167 }
1168 }
1169 close(sockfd);
1170 return 1;
1171}
1172
1173static int compatible_match_revision(const char *name, u_int8_t revision)
1174{
1175 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1176}
1177
1178static int compatible_target_revision(const char *name, u_int8_t revision)
1179{
1180 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1181}
1182
Marc Bouchere6869a82000-03-20 06:03:29 +00001183void
1184register_match(struct iptables_match *me)
1185{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001186 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001187
Marc Bouchere6869a82000-03-20 06:03:29 +00001188 if (strcmp(me->version, program_version) != 0) {
1189 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1190 program_name, me->name, me->version, program_version);
1191 exit(1);
1192 }
1193
Martin Josefsson93911bf2005-01-03 07:46:07 +00001194 /* Revision field stole a char from name. */
1195 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001196 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001197 program_name, me->name);
1198 exit(1);
1199 }
1200
Jones Desougif5b86e62005-12-22 03:33:50 +00001201 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001202 if (old) {
1203 if (old->revision == me->revision) {
1204 fprintf(stderr,
1205 "%s: match `%s' already registered.\n",
1206 program_name, me->name);
1207 exit(1);
1208 }
1209
1210 /* Now we have two (or more) options, check compatibility. */
1211 if (compatible_match_revision(old->name, old->revision)
1212 && old->revision > me->revision)
1213 return;
1214
1215 /* Replace if compatible. */
1216 if (!compatible_match_revision(me->name, me->revision))
1217 return;
1218
1219 /* Delete old one. */
1220 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1221 *i = old->next;
1222 }
1223
Rusty Russell73f72f52000-07-03 10:17:57 +00001224 if (me->size != IPT_ALIGN(me->size)) {
1225 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001226 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001227 exit(1);
1228 }
1229
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001230 /* Append to list. */
1231 for (i = &iptables_matches; *i; i = &(*i)->next);
1232 me->next = NULL;
1233 *i = me;
1234
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 me->m = NULL;
1236 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001237}
1238
1239void
1240register_target(struct iptables_target *me)
1241{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001242 struct iptables_target *old;
1243
Marc Bouchere6869a82000-03-20 06:03:29 +00001244 if (strcmp(me->version, program_version) != 0) {
1245 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1246 program_name, me->name, me->version, program_version);
1247 exit(1);
1248 }
1249
Martin Josefsson93911bf2005-01-03 07:46:07 +00001250 /* Revision field stole a char from name. */
1251 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001252 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001253 program_name, me->name);
1254 exit(1);
1255 }
1256
Jones Desougif5b86e62005-12-22 03:33:50 +00001257 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001258 if (old) {
1259 struct iptables_target **i;
1260
1261 if (old->revision == me->revision) {
1262 fprintf(stderr,
1263 "%s: target `%s' already registered.\n",
1264 program_name, me->name);
1265 exit(1);
1266 }
1267
Rusty Russell3aef54d2005-01-03 03:48:40 +00001268 /* Now we have two (or more) options, check compatibility. */
1269 if (compatible_target_revision(old->name, old->revision)
1270 && old->revision > me->revision)
1271 return;
1272
1273 /* Replace if compatible. */
1274 if (!compatible_target_revision(me->name, me->revision))
1275 return;
1276
1277 /* Delete old one. */
1278 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1279 *i = old->next;
1280 }
1281
Rusty Russell73f72f52000-07-03 10:17:57 +00001282 if (me->size != IPT_ALIGN(me->size)) {
1283 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001284 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001285 exit(1);
1286 }
1287
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 /* Prepend to list. */
1289 me->next = iptables_targets;
1290 iptables_targets = me;
1291 me->t = NULL;
1292 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001293}
1294
1295static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001296print_num(u_int64_t number, unsigned int format)
1297{
1298 if (format & FMT_KILOMEGAGIGA) {
1299 if (number > 99999) {
1300 number = (number + 500) / 1000;
1301 if (number > 9999) {
1302 number = (number + 500) / 1000;
1303 if (number > 9999) {
1304 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001305 if (number > 9999) {
1306 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001307 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001308 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001309 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001310 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001311 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001312 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001313 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001314 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001315 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001316 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001317 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001318}
1319
1320
1321static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001322print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1323{
1324 struct ipt_counters counters;
1325 const char *pol = iptc_get_policy(chain, &counters, handle);
1326 printf("Chain %s", chain);
1327 if (pol) {
1328 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001329 if (!(format & FMT_NOCOUNTS)) {
1330 fputc(' ', stdout);
1331 print_num(counters.pcnt, (format|FMT_NOTABLE));
1332 fputs("packets, ", stdout);
1333 print_num(counters.bcnt, (format|FMT_NOTABLE));
1334 fputs("bytes", stdout);
1335 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001336 printf(")\n");
1337 } else {
1338 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001339 if (!iptc_get_references(&refs, chain, handle))
1340 printf(" (ERROR obtaining refs)\n");
1341 else
1342 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001343 }
1344
1345 if (format & FMT_LINENUMBERS)
1346 printf(FMT("%-4s ", "%s "), "num");
1347 if (!(format & FMT_NOCOUNTS)) {
1348 if (format & FMT_KILOMEGAGIGA) {
1349 printf(FMT("%5s ","%s "), "pkts");
1350 printf(FMT("%5s ","%s "), "bytes");
1351 } else {
1352 printf(FMT("%8s ","%s "), "pkts");
1353 printf(FMT("%10s ","%s "), "bytes");
1354 }
1355 }
1356 if (!(format & FMT_NOTARGET))
1357 printf(FMT("%-9s ","%s "), "target");
1358 fputs(" prot ", stdout);
1359 if (format & FMT_OPTIONS)
1360 fputs("opt", stdout);
1361 if (format & FMT_VIA) {
1362 printf(FMT(" %-6s ","%s "), "in");
1363 printf(FMT("%-6s ","%s "), "out");
1364 }
1365 printf(FMT(" %-19s ","%s "), "source");
1366 printf(FMT(" %-19s "," %s "), "destination");
1367 printf("\n");
1368}
1369
Marc Bouchere6869a82000-03-20 06:03:29 +00001370
1371static int
1372print_match(const struct ipt_entry_match *m,
1373 const struct ipt_ip *ip,
1374 int numeric)
1375{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001376 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001377
1378 if (match) {
1379 if (match->print)
1380 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001381 else
Rusty Russellb039b022000-09-01 06:04:05 +00001382 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001383 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001384 if (m->u.user.name[0])
1385 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001386 }
1387 /* Don't stop iterating. */
1388 return 0;
1389}
1390
1391/* e is called `fw' here for hysterical raisins */
1392static void
1393print_firewall(const struct ipt_entry *fw,
1394 const char *targname,
1395 unsigned int num,
1396 unsigned int format,
1397 const iptc_handle_t handle)
1398{
1399 struct iptables_target *target = NULL;
1400 const struct ipt_entry_target *t;
1401 u_int8_t flags;
1402 char buf[BUFSIZ];
1403
Marc Bouchere6869a82000-03-20 06:03:29 +00001404 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001405 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001406 else
Rusty Russell52a51492000-05-02 16:44:29 +00001407 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001408
1409 t = ipt_get_target((struct ipt_entry *)fw);
1410 flags = fw->ip.flags;
1411
1412 if (format & FMT_LINENUMBERS)
1413 printf(FMT("%-4u ", "%u "), num+1);
1414
1415 if (!(format & FMT_NOCOUNTS)) {
1416 print_num(fw->counters.pcnt, format);
1417 print_num(fw->counters.bcnt, format);
1418 }
1419
1420 if (!(format & FMT_NOTARGET))
1421 printf(FMT("%-9s ", "%s "), targname);
1422
1423 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1424 {
Rusty Russell28381a42000-05-10 00:19:50 +00001425 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001426 if (pname)
1427 printf(FMT("%-5s", "%s "), pname);
1428 else
1429 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1430 }
1431
1432 if (format & FMT_OPTIONS) {
1433 if (format & FMT_NOTABLE)
1434 fputs("opt ", stdout);
1435 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1436 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1437 fputc(' ', stdout);
1438 }
1439
1440 if (format & FMT_VIA) {
1441 char iface[IFNAMSIZ+2];
1442
1443 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1444 iface[0] = '!';
1445 iface[1] = '\0';
1446 }
1447 else iface[0] = '\0';
1448
1449 if (fw->ip.iniface[0] != '\0') {
1450 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001451 }
1452 else if (format & FMT_NUMERIC) strcat(iface, "*");
1453 else strcat(iface, "any");
1454 printf(FMT(" %-6s ","in %s "), iface);
1455
1456 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1457 iface[0] = '!';
1458 iface[1] = '\0';
1459 }
1460 else iface[0] = '\0';
1461
1462 if (fw->ip.outiface[0] != '\0') {
1463 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001464 }
1465 else if (format & FMT_NUMERIC) strcat(iface, "*");
1466 else strcat(iface, "any");
1467 printf(FMT("%-6s ","out %s "), iface);
1468 }
1469
1470 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1471 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1472 printf(FMT("%-19s ","%s "), "anywhere");
1473 else {
1474 if (format & FMT_NUMERIC)
1475 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1476 else
1477 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1478 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1479 printf(FMT("%-19s ","%s "), buf);
1480 }
1481
1482 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1483 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001484 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001485 else {
1486 if (format & FMT_NUMERIC)
1487 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1488 else
1489 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1490 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001491 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001492 }
1493
1494 if (format & FMT_NOTABLE)
1495 fputs(" ", stdout);
1496
Harald Welte72bd87e2005-11-24 17:04:05 +00001497#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001498 if(fw->ip.flags & IPT_F_GOTO)
1499 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001500#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001501
Marc Bouchere6869a82000-03-20 06:03:29 +00001502 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1503
1504 if (target) {
1505 if (target->print)
1506 /* Print the target information. */
1507 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001508 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001509 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001510 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001511
1512 if (!(format & FMT_NONEWLINE))
1513 fputc('\n', stdout);
1514}
1515
1516static void
1517print_firewall_line(const struct ipt_entry *fw,
1518 const iptc_handle_t h)
1519{
1520 struct ipt_entry_target *t;
1521
1522 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001523 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001524}
1525
1526static int
1527append_entry(const ipt_chainlabel chain,
1528 struct ipt_entry *fw,
1529 unsigned int nsaddrs,
1530 const struct in_addr saddrs[],
1531 unsigned int ndaddrs,
1532 const struct in_addr daddrs[],
1533 int verbose,
1534 iptc_handle_t *handle)
1535{
1536 unsigned int i, j;
1537 int ret = 1;
1538
1539 for (i = 0; i < nsaddrs; i++) {
1540 fw->ip.src.s_addr = saddrs[i].s_addr;
1541 for (j = 0; j < ndaddrs; j++) {
1542 fw->ip.dst.s_addr = daddrs[j].s_addr;
1543 if (verbose)
1544 print_firewall_line(fw, *handle);
1545 ret &= iptc_append_entry(chain, fw, handle);
1546 }
1547 }
1548
1549 return ret;
1550}
1551
1552static int
1553replace_entry(const ipt_chainlabel chain,
1554 struct ipt_entry *fw,
1555 unsigned int rulenum,
1556 const struct in_addr *saddr,
1557 const struct in_addr *daddr,
1558 int verbose,
1559 iptc_handle_t *handle)
1560{
1561 fw->ip.src.s_addr = saddr->s_addr;
1562 fw->ip.dst.s_addr = daddr->s_addr;
1563
1564 if (verbose)
1565 print_firewall_line(fw, *handle);
1566 return iptc_replace_entry(chain, fw, rulenum, handle);
1567}
1568
1569static int
1570insert_entry(const ipt_chainlabel chain,
1571 struct ipt_entry *fw,
1572 unsigned int rulenum,
1573 unsigned int nsaddrs,
1574 const struct in_addr saddrs[],
1575 unsigned int ndaddrs,
1576 const struct in_addr daddrs[],
1577 int verbose,
1578 iptc_handle_t *handle)
1579{
1580 unsigned int i, j;
1581 int ret = 1;
1582
1583 for (i = 0; i < nsaddrs; i++) {
1584 fw->ip.src.s_addr = saddrs[i].s_addr;
1585 for (j = 0; j < ndaddrs; j++) {
1586 fw->ip.dst.s_addr = daddrs[j].s_addr;
1587 if (verbose)
1588 print_firewall_line(fw, *handle);
1589 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1590 }
1591 }
1592
1593 return ret;
1594}
1595
Rusty Russell2e0a3212000-04-19 11:23:18 +00001596static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001597make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001598{
1599 /* Establish mask for comparison */
1600 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001601 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001602 unsigned char *mask, *mptr;
1603
1604 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001605 for (matchp = matches; matchp; matchp = matchp->next)
1606 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001607
Rusty Russell9e1d2142000-04-23 09:11:12 +00001608 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001609 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001610 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001611
Rusty Russell9e1d2142000-04-23 09:11:12 +00001612 memset(mask, 0xFF, sizeof(struct ipt_entry));
1613 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001614
Martin Josefsson78cafda2004-02-02 20:01:18 +00001615 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001616 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001617 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001618 + matchp->match->userspacesize);
1619 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001620 }
1621
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001622 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001623 IPT_ALIGN(sizeof(struct ipt_entry_target))
1624 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001625
1626 return mask;
1627}
1628
Marc Bouchere6869a82000-03-20 06:03:29 +00001629static int
1630delete_entry(const ipt_chainlabel chain,
1631 struct ipt_entry *fw,
1632 unsigned int nsaddrs,
1633 const struct in_addr saddrs[],
1634 unsigned int ndaddrs,
1635 const struct in_addr daddrs[],
1636 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001637 iptc_handle_t *handle,
1638 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001639{
1640 unsigned int i, j;
1641 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001642 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001643
Martin Josefsson78cafda2004-02-02 20:01:18 +00001644 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001645 for (i = 0; i < nsaddrs; i++) {
1646 fw->ip.src.s_addr = saddrs[i].s_addr;
1647 for (j = 0; j < ndaddrs; j++) {
1648 fw->ip.dst.s_addr = daddrs[j].s_addr;
1649 if (verbose)
1650 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001651 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 }
1653 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001654 free(mask);
1655
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 return ret;
1657}
1658
Harald Welteae1ff9f2000-12-01 14:26:20 +00001659int
Marc Bouchere6869a82000-03-20 06:03:29 +00001660for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001661 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001662{
1663 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001664 const char *chain;
1665 char *chains;
1666 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667
Rusty Russell9e1d2142000-04-23 09:11:12 +00001668 chain = iptc_first_chain(handle);
1669 while (chain) {
1670 chaincount++;
1671 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001672 }
1673
Rusty Russell9e1d2142000-04-23 09:11:12 +00001674 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1675 i = 0;
1676 chain = iptc_first_chain(handle);
1677 while (chain) {
1678 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1679 i++;
1680 chain = iptc_next_chain(handle);
1681 }
1682
1683 for (i = 0; i < chaincount; i++) {
1684 if (!builtinstoo
1685 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001686 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001687 continue;
1688 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1689 }
1690
1691 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001692 return ret;
1693}
1694
Harald Welteae1ff9f2000-12-01 14:26:20 +00001695int
Marc Bouchere6869a82000-03-20 06:03:29 +00001696flush_entries(const ipt_chainlabel chain, int verbose,
1697 iptc_handle_t *handle)
1698{
1699 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001700 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001701
1702 if (verbose)
1703 fprintf(stdout, "Flushing chain `%s'\n", chain);
1704 return iptc_flush_entries(chain, handle);
1705}
Marc Bouchere6869a82000-03-20 06:03:29 +00001706
1707static int
1708zero_entries(const ipt_chainlabel chain, int verbose,
1709 iptc_handle_t *handle)
1710{
1711 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001712 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001713
Marc Bouchere6869a82000-03-20 06:03:29 +00001714 if (verbose)
1715 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1716 return iptc_zero_entries(chain, handle);
1717}
1718
Harald Welteae1ff9f2000-12-01 14:26:20 +00001719int
Marc Bouchere6869a82000-03-20 06:03:29 +00001720delete_chain(const ipt_chainlabel chain, int verbose,
1721 iptc_handle_t *handle)
1722{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001723 if (!chain)
1724 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001725
1726 if (verbose)
1727 fprintf(stdout, "Deleting chain `%s'\n", chain);
1728 return iptc_delete_chain(chain, handle);
1729}
1730
1731static int
1732list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1733 int expanded, int linenumbers, iptc_handle_t *handle)
1734{
1735 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001736 unsigned int format;
1737 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001738
1739 format = FMT_OPTIONS;
1740 if (!verbose)
1741 format |= FMT_NOCOUNTS;
1742 else
1743 format |= FMT_VIA;
1744
1745 if (numeric)
1746 format |= FMT_NUMERIC;
1747
1748 if (!expanded)
1749 format |= FMT_KILOMEGAGIGA;
1750
1751 if (linenumbers)
1752 format |= FMT_LINENUMBERS;
1753
Rusty Russell9e1d2142000-04-23 09:11:12 +00001754 for (this = iptc_first_chain(handle);
1755 this;
1756 this = iptc_next_chain(handle)) {
1757 const struct ipt_entry *i;
1758 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001759
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 if (chain && strcmp(chain, this) != 0)
1761 continue;
1762
1763 if (found) printf("\n");
1764
1765 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001766 i = iptc_first_rule(this, handle);
1767
1768 num = 0;
1769 while (i) {
1770 print_firewall(i,
1771 iptc_get_target(i, handle),
1772 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001773 format,
1774 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001775 i = iptc_next_rule(i, handle);
1776 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 found = 1;
1778 }
1779
1780 errno = ENOENT;
1781 return found;
1782}
1783
Harald Welte82dd2ec2000-12-19 05:18:15 +00001784static char *get_modprobe(void)
1785{
1786 int procfile;
1787 char *ret;
1788
Harald Welte10f7f142004-10-22 08:14:07 +00001789#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001790 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1791 if (procfile < 0)
1792 return NULL;
1793
Harald Welte10f7f142004-10-22 08:14:07 +00001794 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001795 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001796 memset(ret, 0, PROCFILE_BUFSIZ);
1797 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001798 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001799 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001800 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001801 if (ret[strlen(ret)-1]=='\n')
1802 ret[strlen(ret)-1]=0;
1803 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001804 return ret;
1805 }
1806 fail:
1807 free(ret);
1808 close(procfile);
1809 return NULL;
1810}
1811
Harald Welte58918652001-06-16 18:25:25 +00001812int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001813{
1814 char *buf = NULL;
1815 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001816 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001817
1818 /* If they don't explicitly set it, read out of kernel */
1819 if (!modprobe) {
1820 buf = get_modprobe();
1821 if (!buf)
1822 return -1;
1823 modprobe = buf;
1824 }
1825
1826 switch (fork()) {
1827 case 0:
1828 argv[0] = (char *)modprobe;
1829 argv[1] = (char *)modname;
1830 argv[2] = NULL;
1831 execv(argv[0], argv);
1832
1833 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001834 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001835 case -1:
1836 return -1;
1837
1838 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001839 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001840 }
1841
1842 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001843 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1844 return 0;
1845 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001846}
1847
Marc Bouchere6869a82000-03-20 06:03:29 +00001848static struct ipt_entry *
1849generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001850 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001851 struct ipt_entry_target *target)
1852{
1853 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001854 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001855 struct ipt_entry *e;
1856
1857 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001858 for (matchp = matches; matchp; matchp = matchp->next)
1859 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001860
Rusty Russell228e98d2000-04-27 10:28:06 +00001861 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 *e = *fw;
1863 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001864 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001865
1866 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001867 for (matchp = matches; matchp; matchp = matchp->next) {
1868 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1869 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001870 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001871 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001872
1873 return e;
1874}
1875
Martin Josefsson78cafda2004-02-02 20:01:18 +00001876void clear_rule_matches(struct iptables_rule_match **matches)
1877{
1878 struct iptables_rule_match *matchp, *tmp;
1879
1880 for (matchp = *matches; matchp;) {
1881 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001882 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001883 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001884 matchp->match->m = NULL;
1885 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001886 if (matchp->match == matchp->match->next) {
1887 free(matchp->match);
1888 matchp->match = NULL;
1889 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001890 free(matchp);
1891 matchp = tmp;
1892 }
1893
1894 *matches = NULL;
1895}
1896
Rusty Russell3aef54d2005-01-03 03:48:40 +00001897static void set_revision(char *name, u_int8_t revision)
1898{
1899 /* Old kernel sources don't have ".revision" field,
1900 but we stole a byte from name. */
1901 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1902 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1903}
1904
Phil Oester8cf65912005-09-19 15:00:33 +00001905void
1906get_kernel_version(void) {
1907 static struct utsname uts;
1908 int x = 0, y = 0, z = 0;
1909
1910 if (uname(&uts) == -1) {
1911 fprintf(stderr, "Unable to retrieve kernel version.\n");
1912 free_opts(1);
1913 exit(1);
1914 }
1915
1916 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1917 kernel_version = LINUX_VERSION(x, y, z);
1918}
1919
Marc Bouchere6869a82000-03-20 06:03:29 +00001920int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1921{
1922 struct ipt_entry fw, *e = NULL;
1923 int invert = 0;
1924 unsigned int nsaddrs = 0, ndaddrs = 0;
1925 struct in_addr *saddrs = NULL, *daddrs = NULL;
1926
1927 int c, verbose = 0;
1928 const char *chain = NULL;
1929 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1930 const char *policy = NULL, *newname = NULL;
1931 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001932 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001933 int ret = 1;
1934 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001935 struct iptables_rule_match *matches = NULL;
1936 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001937 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001938 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001939 const char *jumpto = "";
1940 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001941 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001942 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001943
1944 memset(&fw, 0, sizeof(fw));
1945
Harald Welteae1ff9f2000-12-01 14:26:20 +00001946 /* re-set optind to 0 in case do_command gets called
1947 * a second time */
1948 optind = 0;
1949
1950 /* clear mflags in case do_command gets called a second time
1951 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001952 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001953 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001954
1955 for (t = iptables_targets; t; t = t->next) {
1956 t->tflags = 0;
1957 t->used = 0;
1958 }
1959
Marc Bouchere6869a82000-03-20 06:03:29 +00001960 /* Suppress error messages: we may add new options if we
1961 demand-load a protocol. */
1962 opterr = 0;
1963
1964 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001965 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001966 opts, NULL)) != -1) {
1967 switch (c) {
1968 /*
1969 * Command selection
1970 */
1971 case 'A':
1972 add_command(&command, CMD_APPEND, CMD_NONE,
1973 invert);
1974 chain = optarg;
1975 break;
1976
1977 case 'D':
1978 add_command(&command, CMD_DELETE, CMD_NONE,
1979 invert);
1980 chain = optarg;
1981 if (optind < argc && argv[optind][0] != '-'
1982 && argv[optind][0] != '!') {
1983 rulenum = parse_rulenumber(argv[optind++]);
1984 command = CMD_DELETE_NUM;
1985 }
1986 break;
1987
Marc Bouchere6869a82000-03-20 06:03:29 +00001988 case 'R':
1989 add_command(&command, CMD_REPLACE, CMD_NONE,
1990 invert);
1991 chain = optarg;
1992 if (optind < argc && argv[optind][0] != '-'
1993 && argv[optind][0] != '!')
1994 rulenum = parse_rulenumber(argv[optind++]);
1995 else
1996 exit_error(PARAMETER_PROBLEM,
1997 "-%c requires a rule number",
1998 cmd2char(CMD_REPLACE));
1999 break;
2000
2001 case 'I':
2002 add_command(&command, CMD_INSERT, CMD_NONE,
2003 invert);
2004 chain = optarg;
2005 if (optind < argc && argv[optind][0] != '-'
2006 && argv[optind][0] != '!')
2007 rulenum = parse_rulenumber(argv[optind++]);
2008 else rulenum = 1;
2009 break;
2010
2011 case 'L':
2012 add_command(&command, CMD_LIST, CMD_ZERO,
2013 invert);
2014 if (optarg) chain = optarg;
2015 else if (optind < argc && argv[optind][0] != '-'
2016 && argv[optind][0] != '!')
2017 chain = argv[optind++];
2018 break;
2019
2020 case 'F':
2021 add_command(&command, CMD_FLUSH, CMD_NONE,
2022 invert);
2023 if (optarg) chain = optarg;
2024 else if (optind < argc && argv[optind][0] != '-'
2025 && argv[optind][0] != '!')
2026 chain = argv[optind++];
2027 break;
2028
2029 case 'Z':
2030 add_command(&command, CMD_ZERO, CMD_LIST,
2031 invert);
2032 if (optarg) chain = optarg;
2033 else if (optind < argc && argv[optind][0] != '-'
2034 && argv[optind][0] != '!')
2035 chain = argv[optind++];
2036 break;
2037
2038 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00002039 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00002040 exit_error(PARAMETER_PROBLEM,
2041 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00002042 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00002043 if (find_target(optarg, TRY_LOAD))
2044 exit_error(PARAMETER_PROBLEM,
2045 "chain name may not clash "
2046 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
2048 invert);
2049 chain = optarg;
2050 break;
2051
2052 case 'X':
2053 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
2054 invert);
2055 if (optarg) chain = optarg;
2056 else if (optind < argc && argv[optind][0] != '-'
2057 && argv[optind][0] != '!')
2058 chain = argv[optind++];
2059 break;
2060
2061 case 'E':
2062 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2063 invert);
2064 chain = optarg;
2065 if (optind < argc && argv[optind][0] != '-'
2066 && argv[optind][0] != '!')
2067 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002068 else
2069 exit_error(PARAMETER_PROBLEM,
2070 "-%c requires old-chain-name and "
2071 "new-chain-name",
2072 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00002073 break;
2074
2075 case 'P':
2076 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2077 invert);
2078 chain = optarg;
2079 if (optind < argc && argv[optind][0] != '-'
2080 && argv[optind][0] != '!')
2081 policy = argv[optind++];
2082 else
2083 exit_error(PARAMETER_PROBLEM,
2084 "-%c requires a chain and a policy",
2085 cmd2char(CMD_SET_POLICY));
2086 break;
2087
2088 case 'h':
2089 if (!optarg)
2090 optarg = argv[optind];
2091
Rusty Russell2e0a3212000-04-19 11:23:18 +00002092 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002093 if (!matches && protocol)
2094 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002095
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002096 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002097
2098 /*
2099 * Option selection
2100 */
2101 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002102 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002103 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2104 invert);
2105
2106 /* Canonicalize into lower case */
2107 for (protocol = argv[optind-1]; *protocol; protocol++)
2108 *protocol = tolower(*protocol);
2109
2110 protocol = argv[optind-1];
2111 fw.ip.proto = parse_protocol(protocol);
2112
2113 if (fw.ip.proto == 0
2114 && (fw.ip.invflags & IPT_INV_PROTO))
2115 exit_error(PARAMETER_PROBLEM,
2116 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002117 break;
2118
2119 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002120 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002121 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2122 invert);
2123 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002124 break;
2125
2126 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002127 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002128 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2129 invert);
2130 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002131 break;
2132
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002133#ifdef IPT_F_GOTO
2134 case 'g':
2135 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2136 invert);
2137 fw.ip.flags |= IPT_F_GOTO;
2138 jumpto = parse_target(optarg);
2139 break;
2140#endif
2141
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 case 'j':
2143 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2144 invert);
2145 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002146 /* TRY_LOAD (may be chain name) */
2147 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002148
2149 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002150 size_t size;
2151
Rusty Russell73f72f52000-07-03 10:17:57 +00002152 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2153 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002154
Rusty Russell2e0a3212000-04-19 11:23:18 +00002155 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002156 target->t->u.target_size = size;
2157 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002158 set_revision(target->t->u.user.name,
2159 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002160 if (target->init != NULL)
2161 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002162 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002163 }
2164 break;
2165
2166
2167 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002168 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002169 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2170 invert);
2171 parse_interface(argv[optind-1],
2172 fw.ip.iniface,
2173 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002174 break;
2175
2176 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002177 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002178 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2179 invert);
2180 parse_interface(argv[optind-1],
2181 fw.ip.outiface,
2182 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002183 break;
2184
2185 case 'f':
2186 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2187 invert);
2188 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002189 break;
2190
2191 case 'v':
2192 if (!verbose)
2193 set_option(&options, OPT_VERBOSE,
2194 &fw.ip.invflags, invert);
2195 verbose++;
2196 break;
2197
Rusty Russell52a51492000-05-02 16:44:29 +00002198 case 'm': {
2199 size_t size;
2200
Marc Bouchere6869a82000-03-20 06:03:29 +00002201 if (invert)
2202 exit_error(PARAMETER_PROBLEM,
2203 "unexpected ! flag before --match");
2204
Martin Josefsson78cafda2004-02-02 20:01:18 +00002205 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002206 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2207 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002208 m->m = fw_calloc(1, size);
2209 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002210 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002211 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002212 if (m->init != NULL)
2213 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002214 if (m != m->next)
2215 /* Merge options for non-cloned matches */
2216 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002217 }
2218 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002219
2220 case 'n':
2221 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2222 invert);
2223 break;
2224
2225 case 't':
2226 if (invert)
2227 exit_error(PARAMETER_PROBLEM,
2228 "unexpected ! flag before --table");
2229 *table = argv[optind-1];
2230 break;
2231
2232 case 'x':
2233 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2234 invert);
2235 break;
2236
2237 case 'V':
2238 if (invert)
2239 printf("Not %s ;-)\n", program_version);
2240 else
2241 printf("%s v%s\n",
2242 program_name, program_version);
2243 exit(0);
2244
2245 case '0':
2246 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2247 invert);
2248 break;
2249
Harald Welte82dd2ec2000-12-19 05:18:15 +00002250 case 'M':
2251 modprobe = optarg;
2252 break;
2253
Harald Welteccd49e52001-01-23 22:54:34 +00002254 case 'c':
2255
2256 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2257 invert);
2258 pcnt = optarg;
2259 if (optind < argc && argv[optind][0] != '-'
2260 && argv[optind][0] != '!')
2261 bcnt = argv[optind++];
2262 else
2263 exit_error(PARAMETER_PROBLEM,
2264 "-%c requires packet and byte counter",
2265 opt2char(OPT_COUNTERS));
2266
Martin Josefssona28d4952004-05-26 16:04:48 +00002267 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002268 exit_error(PARAMETER_PROBLEM,
2269 "-%c packet counter not numeric",
2270 opt2char(OPT_COUNTERS));
2271
Martin Josefssona28d4952004-05-26 16:04:48 +00002272 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002273 exit_error(PARAMETER_PROBLEM,
2274 "-%c byte counter not numeric",
2275 opt2char(OPT_COUNTERS));
2276
2277 break;
2278
2279
Marc Bouchere6869a82000-03-20 06:03:29 +00002280 case 1: /* non option */
2281 if (optarg[0] == '!' && optarg[1] == '\0') {
2282 if (invert)
2283 exit_error(PARAMETER_PROBLEM,
2284 "multiple consecutive ! not"
2285 " allowed");
2286 invert = TRUE;
2287 optarg[0] = '\0';
2288 continue;
2289 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002290 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002291 exit_tryhelp(2);
2292
2293 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00002294 if (!target
2295 || !(target->parse(c - target->option_offset,
2296 argv, invert,
2297 &target->tflags,
2298 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002299 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002300 if (matchp->completed)
2301 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00002302 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002303 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002304 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002305 &fw,
2306 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002307 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002308 break;
2309 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002310 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002311
2312 /* If you listen carefully, you can
2313 actually hear this code suck. */
2314
2315 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002316 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00002317 * parameter, that has not been parsed yet,
2318 * it's not an option of an explicitly loaded
2319 * match or a target. However, we support
2320 * implicit loading of the protocol match
2321 * extension. '-p tcp' means 'l4 proto 6' and
2322 * at the same time 'load tcp protocol match on
2323 * demand if we specify --dport'.
2324 *
2325 * To make this work, we need to make sure:
2326 * - the parameter has not been parsed by
2327 * a match (m above)
2328 * - a protocol has been specified
2329 * - the protocol extension has not been
2330 * loaded yet, or is loaded and unused
2331 * [think of iptables-restore!]
2332 * - the protocol extension can be successively
2333 * loaded
2334 */
2335 if (m == NULL
2336 && protocol
2337 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002338 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002339 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002340 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002341 && (proto_used == 0))
2342 )
2343 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002344 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002345 /* Try loading protocol */
2346 size_t size;
2347
2348 proto_used = 1;
2349
2350 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2351 + m->size;
2352
2353 m->m = fw_calloc(1, size);
2354 m->m->u.match_size = size;
2355 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002356 set_revision(m->m->u.user.name,
2357 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002358 if (m->init != NULL)
2359 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002360
2361 opts = merge_options(opts,
2362 m->extra_opts, &m->option_offset);
2363
2364 optind--;
2365 continue;
2366 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002367 if (!m)
2368 exit_error(PARAMETER_PROBLEM,
2369 "Unknown arg `%s'",
2370 argv[optind-1]);
2371 }
2372 }
2373 invert = FALSE;
2374 }
2375
Martin Josefsson78cafda2004-02-02 20:01:18 +00002376 for (matchp = matches; matchp; matchp = matchp->next)
2377 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002378
Marc Bouchere6869a82000-03-20 06:03:29 +00002379 if (target)
2380 target->final_check(target->tflags);
2381
2382 /* Fix me: must put inverse options checking here --MN */
2383
2384 if (optind < argc)
2385 exit_error(PARAMETER_PROBLEM,
2386 "unknown arguments found on commandline");
2387 if (!command)
2388 exit_error(PARAMETER_PROBLEM, "no command specified");
2389 if (invert)
2390 exit_error(PARAMETER_PROBLEM,
2391 "nothing appropriate following !");
2392
Harald Welte6336bfd2002-05-07 14:41:43 +00002393 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002394 if (!(options & OPT_DESTINATION))
2395 dhostnetworkmask = "0.0.0.0/0";
2396 if (!(options & OPT_SOURCE))
2397 shostnetworkmask = "0.0.0.0/0";
2398 }
2399
2400 if (shostnetworkmask)
2401 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2402 &(fw.ip.smsk), &nsaddrs);
2403
2404 if (dhostnetworkmask)
2405 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2406 &(fw.ip.dmsk), &ndaddrs);
2407
2408 if ((nsaddrs > 1 || ndaddrs > 1) &&
2409 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2410 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2411 " source or destination IP addresses");
2412
Marc Bouchere6869a82000-03-20 06:03:29 +00002413 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2414 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2415 "specify a unique address");
2416
2417 generic_opt_check(command, options);
2418
2419 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2420 exit_error(PARAMETER_PROBLEM,
2421 "chain name `%s' too long (must be under %i chars)",
2422 chain, IPT_FUNCTION_MAXNAMELEN);
2423
Harald Welteae1ff9f2000-12-01 14:26:20 +00002424 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002425 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002426 *handle = iptc_init(*table);
2427
Rusty Russell8beb0492004-12-22 00:37:10 +00002428 /* try to insmod the module if iptc_init failed */
2429 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002430 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002431
Marc Bouchere6869a82000-03-20 06:03:29 +00002432 if (!*handle)
2433 exit_error(VERSION_PROBLEM,
2434 "can't initialize iptables table `%s': %s",
2435 *table, iptc_strerror(errno));
2436
Harald Welte6336bfd2002-05-07 14:41:43 +00002437 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002438 || command == CMD_DELETE
2439 || command == CMD_INSERT
2440 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002441 if (strcmp(chain, "PREROUTING") == 0
2442 || strcmp(chain, "INPUT") == 0) {
2443 /* -o not valid with incoming packets. */
2444 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002445 exit_error(PARAMETER_PROBLEM,
2446 "Can't use -%c with %s\n",
2447 opt2char(OPT_VIANAMEOUT),
2448 chain);
2449 }
2450
Rusty Russella4860fd2000-06-17 16:13:02 +00002451 if (strcmp(chain, "POSTROUTING") == 0
2452 || strcmp(chain, "OUTPUT") == 0) {
2453 /* -i not valid with outgoing packets */
2454 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002455 exit_error(PARAMETER_PROBLEM,
2456 "Can't use -%c with %s\n",
2457 opt2char(OPT_VIANAMEIN),
2458 chain);
2459 }
2460
2461 if (target && iptc_is_chain(jumpto, *handle)) {
2462 printf("Warning: using chain %s, not extension\n",
2463 jumpto);
2464
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002465 if (target->t)
2466 free(target->t);
2467
Marc Bouchere6869a82000-03-20 06:03:29 +00002468 target = NULL;
2469 }
2470
2471 /* If they didn't specify a target, or it's a chain
2472 name, use standard. */
2473 if (!target
2474 && (strlen(jumpto) == 0
2475 || iptc_is_chain(jumpto, *handle))) {
2476 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002477
Rusty Russell52a51492000-05-02 16:44:29 +00002478 target = find_target(IPT_STANDARD_TARGET,
2479 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002480
2481 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002482 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002483 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002484 target->t->u.target_size = size;
2485 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002486 if (!iptc_is_chain(jumpto, *handle))
2487 set_revision(target->t->u.user.name,
2488 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002489 if (target->init != NULL)
2490 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002491 }
2492
Rusty Russell7e53bf92000-03-20 07:03:28 +00002493 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002494 /* it is no chain, and we can't load a plugin.
2495 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002496 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002497 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002498#ifdef IPT_F_GOTO
2499 if (fw.ip.flags & IPT_F_GOTO)
2500 exit_error(PARAMETER_PROBLEM,
2501 "goto '%s' is not a chain\n", jumpto);
2502#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002503 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002504 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002505 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002506 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002507 }
2508 }
2509
2510 switch (command) {
2511 case CMD_APPEND:
2512 ret = append_entry(chain, e,
2513 nsaddrs, saddrs, ndaddrs, daddrs,
2514 options&OPT_VERBOSE,
2515 handle);
2516 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002517 case CMD_DELETE:
2518 ret = delete_entry(chain, e,
2519 nsaddrs, saddrs, ndaddrs, daddrs,
2520 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002521 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002522 break;
2523 case CMD_DELETE_NUM:
2524 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2525 break;
2526 case CMD_REPLACE:
2527 ret = replace_entry(chain, e, rulenum - 1,
2528 saddrs, daddrs, options&OPT_VERBOSE,
2529 handle);
2530 break;
2531 case CMD_INSERT:
2532 ret = insert_entry(chain, e, rulenum - 1,
2533 nsaddrs, saddrs, ndaddrs, daddrs,
2534 options&OPT_VERBOSE,
2535 handle);
2536 break;
2537 case CMD_LIST:
2538 ret = list_entries(chain,
2539 options&OPT_VERBOSE,
2540 options&OPT_NUMERIC,
2541 options&OPT_EXPANDED,
2542 options&OPT_LINENUMBERS,
2543 handle);
2544 break;
2545 case CMD_FLUSH:
2546 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2547 break;
2548 case CMD_ZERO:
2549 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2550 break;
2551 case CMD_LIST|CMD_ZERO:
2552 ret = list_entries(chain,
2553 options&OPT_VERBOSE,
2554 options&OPT_NUMERIC,
2555 options&OPT_EXPANDED,
2556 options&OPT_LINENUMBERS,
2557 handle);
2558 if (ret)
2559 ret = zero_entries(chain,
2560 options&OPT_VERBOSE, handle);
2561 break;
2562 case CMD_NEW_CHAIN:
2563 ret = iptc_create_chain(chain, handle);
2564 break;
2565 case CMD_DELETE_CHAIN:
2566 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2567 break;
2568 case CMD_RENAME_CHAIN:
2569 ret = iptc_rename_chain(chain, newname, handle);
2570 break;
2571 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002572 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002573 break;
2574 default:
2575 /* We should never reach this... */
2576 exit_tryhelp(2);
2577 }
2578
2579 if (verbose > 1)
2580 dump_entries(*handle);
2581
Martin Josefsson78cafda2004-02-02 20:01:18 +00002582 clear_rule_matches(&matches);
2583
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002584 if (e != NULL) {
2585 free(e);
2586 e = NULL;
2587 }
2588
keso6997cdf2004-07-04 15:20:53 +00002589 free(saddrs);
2590 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002591 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002592
Marc Bouchere6869a82000-03-20 06:03:29 +00002593 return ret;
2594}