blob: 166016e35e611ae23462ec3f60d344e0fceaf077 [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>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000040#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000041#include <fcntl.h>
42#include <sys/wait.h>
Phil Oester8cf65912005-09-19 15:00:33 +000043#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Harald Welte82dd2ec2000-12-19 05:18:15 +000052#ifndef PROC_SYS_MODPROBE
53#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
54#endif
55
Marc Bouchere6869a82000-03-20 06:03:29 +000056#define FMT_NUMERIC 0x0001
57#define FMT_NOCOUNTS 0x0002
58#define FMT_KILOMEGAGIGA 0x0004
59#define FMT_OPTIONS 0x0008
60#define FMT_NOTABLE 0x0010
61#define FMT_NOTARGET 0x0020
62#define FMT_VIA 0x0040
63#define FMT_NONEWLINE 0x0080
64#define FMT_LINENUMBERS 0x0100
65
66#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
67 | FMT_NUMERIC | FMT_NOTABLE)
68#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
69
70
71#define CMD_NONE 0x0000U
72#define CMD_INSERT 0x0001U
73#define CMD_DELETE 0x0002U
74#define CMD_DELETE_NUM 0x0004U
75#define CMD_REPLACE 0x0008U
76#define CMD_APPEND 0x0010U
77#define CMD_LIST 0x0020U
78#define CMD_FLUSH 0x0040U
79#define CMD_ZERO 0x0080U
80#define CMD_NEW_CHAIN 0x0100U
81#define CMD_DELETE_CHAIN 0x0200U
82#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000083#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000084#define NUMBER_OF_CMD 13
85static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000086 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000087
88#define OPTION_OFFSET 256
89
90#define OPT_NONE 0x00000U
91#define OPT_NUMERIC 0x00001U
92#define OPT_SOURCE 0x00002U
93#define OPT_DESTINATION 0x00004U
94#define OPT_PROTOCOL 0x00008U
95#define OPT_JUMP 0x00010U
96#define OPT_VERBOSE 0x00020U
97#define OPT_EXPANDED 0x00040U
98#define OPT_VIANAMEIN 0x00080U
99#define OPT_VIANAMEOUT 0x00100U
100#define OPT_FRAGMENT 0x00200U
101#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000102#define OPT_COUNTERS 0x00800U
103#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000123 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "fragments", 0, 0, 'f' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000136 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000137 { "set-counters", 1, 0, 'c' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000138 { "goto", 1, 0, 'g' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000139 { 0 }
140};
141
Illes Marci63e90632003-03-03 08:08:37 +0000142/* we need this for iptables-restore. iptables-restore.c sets line to the
143 * current line of the input file, in order to give a more precise error
144 * message. iptables itself doesn't need this, so it is initialized to the
145 * magic number of -1 */
146int line = -1;
147
Marc Bouchere6869a82000-03-20 06:03:29 +0000148static struct option *opts = original_opts;
149static unsigned int global_option_offset = 0;
150
151/* Table of legal combinations of commands and options. If any of the
152 * given commands make an option legal, that option is legal (applies to
153 * CMD_LIST and CMD_ZERO only).
154 * Key:
155 * + compulsory
156 * x illegal
157 * optional
158 */
159
160static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
161/* Well, it's better than "Re: Linux vs FreeBSD" */
162{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000163 /* -n -s -d -p -j -v -x -i -o -f --line -c */
164/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
165/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
166/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
167/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
168/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
169/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
170/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
171/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
172/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
174/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000175/*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
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +0000200/* the path to command to load kernel module */
201const char *modprobe = NULL;
202
Rusty Russell2e0a3212000-04-19 11:23:18 +0000203/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000204struct iptables_match *iptables_matches = NULL;
205struct iptables_target *iptables_targets = NULL;
206
207/* Extra debugging from libiptc */
208extern void dump_entries(const iptc_handle_t handle);
209
210/* A few hardcoded protocols for 'all' and in case the user has no
211 /etc/protocols */
212struct pprot {
213 char *name;
214 u_int8_t num;
215};
216
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000217/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218/* defined in netinet/in.h */
219#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000220#ifndef IPPROTO_ESP
221#define IPPROTO_ESP 50
222#endif
223#ifndef IPPROTO_AH
224#define IPPROTO_AH 51
225#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000226#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000227
Marc Bouchere6869a82000-03-20 06:03:29 +0000228static const struct pprot chain_protos[] = {
229 { "tcp", IPPROTO_TCP },
230 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000231 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000232 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000233 { "esp", IPPROTO_ESP },
234 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000235 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000236 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000237};
238
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000239static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000240proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000241{
242 unsigned int i;
243
Rusty Russell28381a42000-05-10 00:19:50 +0000244 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000245 struct protoent *pent = getprotobynumber(proto);
246 if (pent)
247 return pent->p_name;
248 }
249
250 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
251 if (chain_protos[i].num == proto)
252 return chain_protos[i].name;
253
254 return NULL;
255}
256
Phil Oester58179b12006-07-20 17:00:19 +0000257int
258service_to_port(const char *name, const char *proto)
259{
260 struct servent *service;
261
262 if ((service = getservbyname(name, proto)) != NULL)
263 return ntohs((unsigned short) service->s_port);
264
265 return -1;
266}
267
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000268u_int16_t
269parse_port(const char *port, const char *proto)
270{
271 unsigned int portnum;
272
273 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
274 (portnum = service_to_port(port, proto)) != -1)
275 return (u_int16_t)portnum;
276
277 exit_error(PARAMETER_PROBLEM,
278 "invalid port/service `%s' specified", port);
279}
280
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000281enum {
282 IPT_DOTTED_ADDR = 0,
283 IPT_DOTTED_MASK
284};
285
286static struct in_addr *
287__dotted_to_addr(const char *dotted, int type)
Marc Bouchere6869a82000-03-20 06:03:29 +0000288{
289 static struct in_addr addr;
290 unsigned char *addrp;
291 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000292 unsigned int onebyte;
293 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000294 char buf[20];
295
296 /* copy dotted string, because we need to modify it */
297 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000298 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000299 addrp = (unsigned char *) &(addr.s_addr);
300
301 p = buf;
302 for (i = 0; i < 3; i++) {
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000303 if ((q = strchr(p, '.')) == NULL) {
304 if (type == IPT_DOTTED_ADDR) {
305 /* autocomplete, this is a network address */
306 if (string_to_number(p, 0, 255, &onebyte) == -1)
307 return (struct in_addr *) NULL;
308
309 addrp[i] = (unsigned char) onebyte;
310 while (i < 3)
311 addrp[++i] = 0;
312
313 return &addr;
314 } else
315 return (struct in_addr *) NULL;
316 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000317
318 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000319 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000320 return (struct in_addr *) NULL;
321
322 addrp[i] = (unsigned char) onebyte;
323 p = q + 1;
324 }
325
326 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000327 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000328 return (struct in_addr *) NULL;
329
330 addrp[3] = (unsigned char) onebyte;
331
332 return &addr;
333}
334
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000335struct in_addr *
336dotted_to_addr(const char *dotted)
337{
338 return __dotted_to_addr(dotted, IPT_DOTTED_ADDR);
339}
340
341struct in_addr *
342dotted_to_mask(const char *dotted)
343{
344 return __dotted_to_addr(dotted, IPT_DOTTED_MASK);
345}
346
Marc Bouchere6869a82000-03-20 06:03:29 +0000347static struct in_addr *
348network_to_addr(const char *name)
349{
350 struct netent *net;
351 static struct in_addr addr;
352
353 if ((net = getnetbyname(name)) != NULL) {
354 if (net->n_addrtype != AF_INET)
355 return (struct in_addr *) NULL;
356 addr.s_addr = htonl((unsigned long) net->n_net);
357 return &addr;
358 }
359
360 return (struct in_addr *) NULL;
361}
362
363static void
364inaddrcpy(struct in_addr *dst, struct in_addr *src)
365{
366 /* memcpy(dst, src, sizeof(struct in_addr)); */
367 dst->s_addr = src->s_addr;
368}
369
Pablo Neiradfdcd642005-05-29 19:05:23 +0000370static void free_opts(int reset_offset)
371{
372 if (opts != original_opts) {
373 free(opts);
374 opts = original_opts;
375 if (reset_offset)
376 global_option_offset = 0;
377 }
378}
379
Marc Bouchere6869a82000-03-20 06:03:29 +0000380void
381exit_error(enum exittype status, char *msg, ...)
382{
383 va_list args;
384
385 va_start(args, msg);
386 fprintf(stderr, "%s v%s: ", program_name, program_version);
387 vfprintf(stderr, msg, args);
388 va_end(args);
389 fprintf(stderr, "\n");
390 if (status == PARAMETER_PROBLEM)
391 exit_tryhelp(status);
392 if (status == VERSION_PROBLEM)
393 fprintf(stderr,
394 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000395 /* On error paths, make sure that we don't leak memory */
396 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000397 exit(status);
398}
399
400void
401exit_tryhelp(int status)
402{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000403 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000404 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
406 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000407 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000408 exit(status);
409}
410
411void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000412exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000413{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000414 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000415 struct iptables_target *t = NULL;
416
Marc Bouchere6869a82000-03-20 06:03:29 +0000417 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000418"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000419" %s -[RI] chain rulenum rule-specification [options]\n"
420" %s -D chain rulenum [options]\n"
421" %s -[LFZ] [chain] [options]\n"
422" %s -[NX] chain\n"
423" %s -E old-chain-name new-chain-name\n"
424" %s -P chain target [options]\n"
425" %s -h (print this help information)\n\n",
426 program_name, program_version, program_name, program_name,
427 program_name, program_name, program_name, program_name,
428 program_name, program_name);
429
430 printf(
431"Commands:\n"
432"Either long or short options are allowed.\n"
433" --append -A chain Append to chain\n"
434" --delete -D chain Delete matching rule from chain\n"
435" --delete -D chain rulenum\n"
436" Delete rule rulenum (1 = first) from chain\n"
437" --insert -I chain [rulenum]\n"
438" Insert in chain as rulenum (default 1=first)\n"
439" --replace -R chain rulenum\n"
440" Replace rule rulenum (1 = first) in chain\n"
441" --list -L [chain] List the rules in a chain or all chains\n"
442" --flush -F [chain] Delete all rules in chain or all chains\n"
443" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000444" --new -N chain Create a new user-defined chain\n"
445" --delete-chain\n"
446" -X [chain] Delete a user-defined chain\n"
447" --policy -P chain target\n"
448" Change policy on chain to target\n"
449" --rename-chain\n"
450" -E old-chain new-chain\n"
451" Change chain name, (moving any references)\n"
452
453"Options:\n"
454" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
455" --source -s [!] address[/mask]\n"
456" source specification\n"
457" --destination -d [!] address[/mask]\n"
458" destination specification\n"
459" --in-interface -i [!] input name[+]\n"
460" network interface name ([+] for wildcard)\n"
461" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000462" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000463#ifdef IPT_F_GOTO
464" --goto -g chain\n"
465" jump to chain with no return\n"
466#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000467" --match -m match\n"
468" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000469" --numeric -n numeric output of addresses and ports\n"
470" --out-interface -o [!] output name[+]\n"
471" network interface name ([+] for wildcard)\n"
472" --table -t table table to manipulate (default: `filter')\n"
473" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000474" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000475" --exact -x expand numbers (display exact values)\n"
476"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000477" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000478" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000479"[!] --version -V print package version.\n");
480
Rusty Russell363112d2000-08-11 13:49:26 +0000481 /* Print out any special helps. A user might like to be able
482 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000483 results. So we call help for all specified matches & targets */
484 for (t = iptables_targets; t ;t = t->next) {
485 if (t->used) {
486 printf("\n");
487 t->help();
488 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000489 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000490 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000491 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000492 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000493 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000494 exit(0);
495}
496
497static void
498generic_opt_check(int command, int options)
499{
500 int i, j, legal = 0;
501
502 /* Check that commands are valid with options. Complicated by the
503 * fact that if an option is legal with *any* command given, it is
504 * legal overall (ie. -z and -l).
505 */
506 for (i = 0; i < NUMBER_OF_OPT; i++) {
507 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
508
509 for (j = 0; j < NUMBER_OF_CMD; j++) {
510 if (!(command & (1<<j)))
511 continue;
512
513 if (!(options & (1<<i))) {
514 if (commands_v_options[j][i] == '+')
515 exit_error(PARAMETER_PROBLEM,
516 "You need to supply the `-%c' "
517 "option for this command\n",
518 optflags[i]);
519 } else {
520 if (commands_v_options[j][i] != 'x')
521 legal = 1;
522 else if (legal == 0)
523 legal = -1;
524 }
525 }
526 if (legal == -1)
527 exit_error(PARAMETER_PROBLEM,
528 "Illegal option `-%c' with this command\n",
529 optflags[i]);
530 }
531}
532
533static char
534opt2char(int option)
535{
536 const char *ptr;
537 for (ptr = optflags; option > 1; option >>= 1, ptr++);
538
539 return *ptr;
540}
541
542static char
543cmd2char(int option)
544{
545 const char *ptr;
546 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
547
548 return *ptr;
549}
550
551static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000552add_command(unsigned int *cmd, const int newcmd, const int othercmds,
553 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000554{
555 if (invert)
556 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
557 if (*cmd & (~othercmds))
558 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
559 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
560 *cmd |= newcmd;
561}
562
563int
Harald Welteb77f1da2002-03-14 11:35:58 +0000564check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000565{
566 if (option && strcmp(option, "!") == 0) {
567 if (*invert)
568 exit_error(PARAMETER_PROBLEM,
569 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000570 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000571 if (optind) {
572 *optind = *optind+1;
573 if (argc && *optind > argc)
574 exit_error(PARAMETER_PROBLEM,
575 "no argument following `!'");
576 }
577
Marc Bouchere6869a82000-03-20 06:03:29 +0000578 return TRUE;
579 }
580 return FALSE;
581}
582
Marc Bouchere6869a82000-03-20 06:03:29 +0000583static struct in_addr *
584host_to_addr(const char *name, unsigned int *naddr)
585{
586 struct hostent *host;
587 struct in_addr *addr;
588 unsigned int i;
589
590 *naddr = 0;
591 if ((host = gethostbyname(name)) != NULL) {
592 if (host->h_addrtype != AF_INET ||
593 host->h_length != sizeof(struct in_addr))
594 return (struct in_addr *) NULL;
595
596 while (host->h_addr_list[*naddr] != (char *) NULL)
597 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000598 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000599 for (i = 0; i < *naddr; i++)
600 inaddrcpy(&(addr[i]),
601 (struct in_addr *) host->h_addr_list[i]);
602 return addr;
603 }
604
605 return (struct in_addr *) NULL;
606}
607
608static char *
609addr_to_host(const struct in_addr *addr)
610{
611 struct hostent *host;
612
613 if ((host = gethostbyaddr((char *) addr,
614 sizeof(struct in_addr), AF_INET)) != NULL)
615 return (char *) host->h_name;
616
617 return (char *) NULL;
618}
619
620/*
621 * All functions starting with "parse" should succeed, otherwise
622 * the program fails.
623 * Most routines return pointers to static data that may change
624 * between calls to the same or other routines with a few exceptions:
625 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
626 * return global static data.
627*/
628
629static struct in_addr *
630parse_hostnetwork(const char *name, unsigned int *naddrs)
631{
632 struct in_addr *addrp, *addrptmp;
633
634 if ((addrptmp = dotted_to_addr(name)) != NULL ||
635 (addrptmp = network_to_addr(name)) != NULL) {
636 addrp = fw_malloc(sizeof(struct in_addr));
637 inaddrcpy(addrp, addrptmp);
638 *naddrs = 1;
639 return addrp;
640 }
641 if ((addrp = host_to_addr(name, naddrs)) != NULL)
642 return addrp;
643
644 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
645}
646
647static struct in_addr *
648parse_mask(char *mask)
649{
650 static struct in_addr maskaddr;
651 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000652 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000653
654 if (mask == NULL) {
655 /* no mask at all defaults to 32 bits */
656 maskaddr.s_addr = 0xFFFFFFFF;
657 return &maskaddr;
658 }
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000659 if ((addrp = dotted_to_mask(mask)) != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +0000660 /* dotted_to_addr already returns a network byte order addr */
661 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000662 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000663 exit_error(PARAMETER_PROBLEM,
664 "invalid mask `%s' specified", mask);
665 if (bits != 0) {
666 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
667 return &maskaddr;
668 }
669
670 maskaddr.s_addr = 0L;
671 return &maskaddr;
672}
673
Marc Boucherb93c7982001-12-06 14:50:19 +0000674void
Marc Bouchere6869a82000-03-20 06:03:29 +0000675parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
676 struct in_addr *maskp, unsigned int *naddrs)
677{
678 struct in_addr *addrp;
679 char buf[256];
680 char *p;
681 int i, j, k, n;
682
683 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000684 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000685 if ((p = strrchr(buf, '/')) != NULL) {
686 *p = '\0';
687 addrp = parse_mask(p + 1);
688 } else
689 addrp = parse_mask(NULL);
690 inaddrcpy(maskp, addrp);
691
692 /* if a null mask is given, the name is ignored, like in "any/0" */
693 if (maskp->s_addr == 0L)
694 strcpy(buf, "0.0.0.0");
695
696 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
697 n = *naddrs;
698 for (i = 0, j = 0; i < n; i++) {
699 addrp[j++].s_addr &= maskp->s_addr;
700 for (k = 0; k < j - 1; k++) {
701 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
702 (*naddrs)--;
703 j--;
704 break;
705 }
706 }
707 }
708}
709
710struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000711find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000712{
713 struct iptables_match *ptr;
714
715 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000716 if (strcmp(name, ptr->name) == 0) {
717 struct iptables_match *clone;
718
719 /* First match of this type: */
720 if (ptr->m == NULL)
721 break;
722
723 /* Second and subsequent clones */
724 clone = fw_malloc(sizeof(struct iptables_match));
725 memcpy(clone, ptr, sizeof(struct iptables_match));
726 clone->mflags = 0;
727 /* This is a clone: */
728 clone->next = clone;
729
730 ptr = clone;
Marc Bouchere6869a82000-03-20 06:03:29 +0000731 break;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000732 }
733 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000734
Harald Welte3efb6ea2001-08-06 18:50:21 +0000735#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000736 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000737 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000738 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000739 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000740 if (dlopen(path, RTLD_NOW)) {
741 /* Found library. If it didn't register itself,
742 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000743 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000744
Rusty Russell9e1d2142000-04-23 09:11:12 +0000745 if (!ptr)
746 exit_error(PARAMETER_PROBLEM,
747 "Couldn't load match `%s'\n",
748 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000749 } else if (tryload == LOAD_MUST_SUCCEED)
750 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000751 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000752 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000753 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000754#else
755 if (ptr && !ptr->loaded) {
756 if (tryload != DONT_LOAD)
757 ptr->loaded = 1;
758 else
759 ptr = NULL;
760 }
Marc Boucher067477b2002-03-24 15:09:31 +0000761 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
762 exit_error(PARAMETER_PROBLEM,
763 "Couldn't find match `%s'\n", name);
764 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000765#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000766
Martin Josefsson78cafda2004-02-02 20:01:18 +0000767 if (ptr && matches) {
768 struct iptables_rule_match **i;
769 struct iptables_rule_match *newentry;
770
771 newentry = fw_malloc(sizeof(struct iptables_rule_match));
772
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000773 for (i = matches; *i; i = &(*i)->next) {
774 if (strcmp(name, (*i)->match->name) == 0)
775 (*i)->completed = 1;
776 }
Martin Josefsson78cafda2004-02-02 20:01:18 +0000777 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000778 newentry->completed = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000779 newentry->next = NULL;
780 *i = newentry;
781 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000782
Marc Bouchere6869a82000-03-20 06:03:29 +0000783 return ptr;
784}
785
Rusty Russell28381a42000-05-10 00:19:50 +0000786/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
787static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000788find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000789{
Harald Welteed498492001-07-23 01:24:22 +0000790 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000791
Harald Welte0b0013a2002-02-18 16:15:31 +0000792 if (string_to_number(pname, 0, 255, &proto) != -1) {
793 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000794
Harald Welte0b0013a2002-02-18 16:15:31 +0000795 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000796 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000797 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000798 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000799
800 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000801}
802
Marc Boucherb93c7982001-12-06 14:50:19 +0000803u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000804parse_protocol(const char *s)
805{
Harald Welteed498492001-07-23 01:24:22 +0000806 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000807
Harald Welteed498492001-07-23 01:24:22 +0000808 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 struct protoent *pent;
810
Harald Weltecbe1ec72006-02-11 09:50:11 +0000811 /* first deal with the special case of 'all' to prevent
812 * people from being able to redefine 'all' in nsswitch
813 * and/or provoke expensive [not working] ldap/nis/...
814 * lookups */
815 if (!strcmp(s, "all"))
816 return 0;
817
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 if ((pent = getprotobyname(s)))
819 proto = pent->p_proto;
820 else {
821 unsigned int i;
822 for (i = 0;
823 i < sizeof(chain_protos)/sizeof(struct pprot);
824 i++) {
825 if (strcmp(s, chain_protos[i].name) == 0) {
826 proto = chain_protos[i].num;
827 break;
828 }
829 }
830 if (i == sizeof(chain_protos)/sizeof(struct pprot))
831 exit_error(PARAMETER_PROBLEM,
832 "unknown protocol `%s' specified",
833 s);
834 }
835 }
836
837 return (u_int16_t)proto;
838}
839
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000840void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000841{
842 int vialen = strlen(arg);
843 unsigned int i;
844
845 memset(mask, 0, IFNAMSIZ);
846 memset(vianame, 0, IFNAMSIZ);
847
848 if (vialen + 1 > IFNAMSIZ)
849 exit_error(PARAMETER_PROBLEM,
850 "interface name `%s' must be shorter than IFNAMSIZ"
851 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000852
Marc Bouchere6869a82000-03-20 06:03:29 +0000853 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000854 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000855 memset(mask, 0, IFNAMSIZ);
856 else if (vianame[vialen - 1] == '+') {
857 memset(mask, 0xFF, vialen - 1);
858 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000859 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000860 } else {
861 /* Include nul-terminator in match */
862 memset(mask, 0xFF, vialen + 1);
863 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000864 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000865 if (vianame[i] == ':' ||
866 vianame[i] == '!' ||
867 vianame[i] == '*') {
Patrick McHardy42bd67b2006-10-11 07:37:26 +0000868 printf("Warning: weird character in interface"
Harald Weltede1578f2001-05-23 23:07:33 +0000869 " `%s' (No aliases, :, ! or *).\n",
870 vianame);
871 break;
872 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000873 }
874 }
875}
876
877/* Can't be zero. */
878static int
879parse_rulenumber(const char *rule)
880{
Harald Welteed498492001-07-23 01:24:22 +0000881 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000882
Harald Welteed498492001-07-23 01:24:22 +0000883 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000884 exit_error(PARAMETER_PROBLEM,
885 "Invalid rule number `%s'", rule);
886
887 return rulenum;
888}
889
890static const char *
891parse_target(const char *targetname)
892{
893 const char *ptr;
894
895 if (strlen(targetname) < 1)
896 exit_error(PARAMETER_PROBLEM,
897 "Invalid target name (too short)");
898
899 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
900 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000901 "Invalid target name `%s' (%u chars max)",
902 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000903
904 for (ptr = targetname; *ptr; ptr++)
905 if (isspace(*ptr))
906 exit_error(PARAMETER_PROBLEM,
907 "Invalid target name `%s'", targetname);
908 return targetname;
909}
910
911static char *
912addr_to_network(const struct in_addr *addr)
913{
914 struct netent *net;
915
916 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
917 return (char *) net->n_name;
918
919 return (char *) NULL;
920}
921
922char *
923addr_to_dotted(const struct in_addr *addrp)
924{
925 static char buf[20];
926 const unsigned char *bytep;
927
928 bytep = (const unsigned char *) &(addrp->s_addr);
929 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
930 return buf;
931}
Marc Boucherb93c7982001-12-06 14:50:19 +0000932
933char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000934addr_to_anyname(const struct in_addr *addr)
935{
936 char *name;
937
938 if ((name = addr_to_host(addr)) != NULL ||
939 (name = addr_to_network(addr)) != NULL)
940 return name;
941
942 return addr_to_dotted(addr);
943}
944
Marc Boucherb93c7982001-12-06 14:50:19 +0000945char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000946mask_to_dotted(const struct in_addr *mask)
947{
948 int i;
949 static char buf[20];
950 u_int32_t maskaddr, bits;
951
952 maskaddr = ntohl(mask->s_addr);
953
954 if (maskaddr == 0xFFFFFFFFL)
955 /* we don't want to see "/32" */
956 return "";
957
958 i = 32;
959 bits = 0xFFFFFFFEL;
960 while (--i >= 0 && maskaddr != bits)
961 bits <<= 1;
962 if (i >= 0)
963 sprintf(buf, "/%d", i);
964 else
965 /* mask was not a decent combination of 1's and 0's */
966 sprintf(buf, "/%s", addr_to_dotted(mask));
967
968 return buf;
969}
970
971int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000972string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
973 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000974{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000975 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000976 char *end;
977
978 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000979 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000980 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000981 if (*end == '\0' && end != s) {
982 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000983 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000984 *ret = number;
985 return 0;
986 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000987 }
988 return -1;
989}
990
Martin Josefssonb105bc92004-05-26 15:54:49 +0000991int
992string_to_number_l(const char *s, unsigned long min, unsigned long max,
993 unsigned long *ret)
994{
995 int result;
996 unsigned long long number;
997
998 result = string_to_number_ll(s, min, max, &number);
999 *ret = (unsigned long)number;
1000
1001 return result;
1002}
1003
1004int string_to_number(const char *s, unsigned int min, unsigned int max,
1005 unsigned int *ret)
1006{
1007 int result;
1008 unsigned long number;
1009
1010 result = string_to_number_l(s, min, max, &number);
1011 *ret = (unsigned int)number;
1012
1013 return result;
1014}
1015
Marc Bouchere6869a82000-03-20 06:03:29 +00001016static void
1017set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1018 int invert)
1019{
1020 if (*options & option)
1021 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1022 opt2char(option));
1023 *options |= option;
1024
1025 if (invert) {
1026 unsigned int i;
1027 for (i = 0; 1 << i != option; i++);
1028
1029 if (!inverse_for_options[i])
1030 exit_error(PARAMETER_PROBLEM,
1031 "cannot have ! before -%c",
1032 opt2char(option));
1033 *invflg |= inverse_for_options[i];
1034 }
1035}
1036
1037struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +00001038find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +00001039{
1040 struct iptables_target *ptr;
1041
1042 /* Standard target? */
1043 if (strcmp(name, "") == 0
1044 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
1045 || strcmp(name, IPTC_LABEL_DROP) == 0
1046 || strcmp(name, IPTC_LABEL_QUEUE) == 0
1047 || strcmp(name, IPTC_LABEL_RETURN) == 0)
1048 name = "standard";
1049
1050 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
1051 if (strcmp(name, ptr->name) == 0)
1052 break;
1053 }
1054
Harald Welte3efb6ea2001-08-06 18:50:21 +00001055#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001056 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001057 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001058 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001059 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001060 if (dlopen(path, RTLD_NOW)) {
1061 /* Found library. If it didn't register itself,
1062 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001063 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001064 if (!ptr)
1065 exit_error(PARAMETER_PROBLEM,
1066 "Couldn't load target `%s'\n",
1067 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001068 } else if (tryload == LOAD_MUST_SUCCEED)
1069 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001070 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001071 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001072 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001073#else
1074 if (ptr && !ptr->loaded) {
1075 if (tryload != DONT_LOAD)
1076 ptr->loaded = 1;
1077 else
1078 ptr = NULL;
1079 }
Marc Boucher067477b2002-03-24 15:09:31 +00001080 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1081 exit_error(PARAMETER_PROBLEM,
1082 "Couldn't find target `%s'\n", name);
1083 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001084#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001085
Harald Welteae1ff9f2000-12-01 14:26:20 +00001086 if (ptr)
1087 ptr->used = 1;
1088
Marc Bouchere6869a82000-03-20 06:03:29 +00001089 return ptr;
1090}
1091
1092static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001093merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001094 unsigned int *option_offset)
1095{
1096 unsigned int num_old, num_new, i;
1097 struct option *merge;
1098
1099 for (num_old = 0; oldopts[num_old].name; num_old++);
1100 for (num_new = 0; newopts[num_new].name; num_new++);
1101
1102 global_option_offset += OPTION_OFFSET;
1103 *option_offset = global_option_offset;
1104
1105 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1106 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001107 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001108 for (i = 0; i < num_new; i++) {
1109 merge[num_old + i] = newopts[i];
1110 merge[num_old + i].val += *option_offset;
1111 }
1112 memset(merge + num_old + num_new, 0, sizeof(struct option));
1113
1114 return merge;
1115}
1116
Rusty Russell3aef54d2005-01-03 03:48:40 +00001117static int compatible_revision(const char *name, u_int8_t revision, int opt)
1118{
1119 struct ipt_get_revision rev;
1120 socklen_t s = sizeof(rev);
1121 int max_rev, sockfd;
1122
1123 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1124 if (sockfd < 0) {
1125 fprintf(stderr, "Could not open socket to kernel: %s\n",
1126 strerror(errno));
1127 exit(1);
1128 }
1129
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001130 load_iptables_ko(modprobe, 1);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001131
Rusty Russell3aef54d2005-01-03 03:48:40 +00001132 strcpy(rev.name, name);
1133 rev.revision = revision;
1134
1135 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1136 if (max_rev < 0) {
1137 /* Definitely don't support this? */
Patrick McHardy76258722007-06-26 15:29:45 +00001138 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001139 close(sockfd);
1140 return 0;
1141 } else if (errno == ENOPROTOOPT) {
1142 close(sockfd);
1143 /* Assume only revision 0 support (old kernel) */
1144 return (revision == 0);
1145 } else {
1146 fprintf(stderr, "getsockopt failed strangely: %s\n",
1147 strerror(errno));
1148 exit(1);
1149 }
1150 }
1151 close(sockfd);
1152 return 1;
1153}
1154
1155static int compatible_match_revision(const char *name, u_int8_t revision)
1156{
1157 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1158}
1159
1160static int compatible_target_revision(const char *name, u_int8_t revision)
1161{
1162 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1163}
1164
Marc Bouchere6869a82000-03-20 06:03:29 +00001165void
1166register_match(struct iptables_match *me)
1167{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001168 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001169
Marc Bouchere6869a82000-03-20 06:03:29 +00001170 if (strcmp(me->version, program_version) != 0) {
1171 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1172 program_name, me->name, me->version, program_version);
1173 exit(1);
1174 }
1175
Martin Josefsson93911bf2005-01-03 07:46:07 +00001176 /* Revision field stole a char from name. */
1177 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001178 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001179 program_name, me->name);
1180 exit(1);
1181 }
1182
Jones Desougif5b86e62005-12-22 03:33:50 +00001183 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001184 if (old) {
1185 if (old->revision == me->revision) {
1186 fprintf(stderr,
1187 "%s: match `%s' already registered.\n",
1188 program_name, me->name);
1189 exit(1);
1190 }
1191
1192 /* Now we have two (or more) options, check compatibility. */
1193 if (compatible_match_revision(old->name, old->revision)
1194 && old->revision > me->revision)
1195 return;
1196
1197 /* Replace if compatible. */
1198 if (!compatible_match_revision(me->name, me->revision))
1199 return;
1200
1201 /* Delete old one. */
1202 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1203 *i = old->next;
1204 }
1205
Rusty Russell73f72f52000-07-03 10:17:57 +00001206 if (me->size != IPT_ALIGN(me->size)) {
1207 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001208 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001209 exit(1);
1210 }
1211
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001212 /* Append to list. */
1213 for (i = &iptables_matches; *i; i = &(*i)->next);
1214 me->next = NULL;
1215 *i = me;
1216
Marc Bouchere6869a82000-03-20 06:03:29 +00001217 me->m = NULL;
1218 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001219}
1220
1221void
1222register_target(struct iptables_target *me)
1223{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001224 struct iptables_target *old;
1225
Marc Bouchere6869a82000-03-20 06:03:29 +00001226 if (strcmp(me->version, program_version) != 0) {
1227 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1228 program_name, me->name, me->version, program_version);
1229 exit(1);
1230 }
1231
Martin Josefsson93911bf2005-01-03 07:46:07 +00001232 /* Revision field stole a char from name. */
1233 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001234 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 program_name, me->name);
1236 exit(1);
1237 }
1238
Jones Desougif5b86e62005-12-22 03:33:50 +00001239 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001240 if (old) {
1241 struct iptables_target **i;
1242
1243 if (old->revision == me->revision) {
1244 fprintf(stderr,
1245 "%s: target `%s' already registered.\n",
1246 program_name, me->name);
1247 exit(1);
1248 }
1249
Rusty Russell3aef54d2005-01-03 03:48:40 +00001250 /* Now we have two (or more) options, check compatibility. */
1251 if (compatible_target_revision(old->name, old->revision)
1252 && old->revision > me->revision)
1253 return;
1254
1255 /* Replace if compatible. */
1256 if (!compatible_target_revision(me->name, me->revision))
1257 return;
1258
1259 /* Delete old one. */
1260 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1261 *i = old->next;
1262 }
1263
Rusty Russell73f72f52000-07-03 10:17:57 +00001264 if (me->size != IPT_ALIGN(me->size)) {
1265 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001266 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001267 exit(1);
1268 }
1269
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 /* Prepend to list. */
1271 me->next = iptables_targets;
1272 iptables_targets = me;
1273 me->t = NULL;
1274 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001275}
1276
1277static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001278print_num(u_int64_t number, unsigned int format)
1279{
1280 if (format & FMT_KILOMEGAGIGA) {
1281 if (number > 99999) {
1282 number = (number + 500) / 1000;
1283 if (number > 9999) {
1284 number = (number + 500) / 1000;
1285 if (number > 9999) {
1286 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001287 if (number > 9999) {
1288 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001289 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001290 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001291 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001292 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001293 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001294 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001295 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001296 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001297 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001298 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001299 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001300}
1301
1302
1303static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001304print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1305{
1306 struct ipt_counters counters;
1307 const char *pol = iptc_get_policy(chain, &counters, handle);
1308 printf("Chain %s", chain);
1309 if (pol) {
1310 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001311 if (!(format & FMT_NOCOUNTS)) {
1312 fputc(' ', stdout);
1313 print_num(counters.pcnt, (format|FMT_NOTABLE));
1314 fputs("packets, ", stdout);
1315 print_num(counters.bcnt, (format|FMT_NOTABLE));
1316 fputs("bytes", stdout);
1317 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 printf(")\n");
1319 } else {
1320 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001321 if (!iptc_get_references(&refs, chain, handle))
1322 printf(" (ERROR obtaining refs)\n");
1323 else
1324 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001325 }
1326
1327 if (format & FMT_LINENUMBERS)
1328 printf(FMT("%-4s ", "%s "), "num");
1329 if (!(format & FMT_NOCOUNTS)) {
1330 if (format & FMT_KILOMEGAGIGA) {
1331 printf(FMT("%5s ","%s "), "pkts");
1332 printf(FMT("%5s ","%s "), "bytes");
1333 } else {
1334 printf(FMT("%8s ","%s "), "pkts");
1335 printf(FMT("%10s ","%s "), "bytes");
1336 }
1337 }
1338 if (!(format & FMT_NOTARGET))
1339 printf(FMT("%-9s ","%s "), "target");
1340 fputs(" prot ", stdout);
1341 if (format & FMT_OPTIONS)
1342 fputs("opt", stdout);
1343 if (format & FMT_VIA) {
1344 printf(FMT(" %-6s ","%s "), "in");
1345 printf(FMT("%-6s ","%s "), "out");
1346 }
1347 printf(FMT(" %-19s ","%s "), "source");
1348 printf(FMT(" %-19s "," %s "), "destination");
1349 printf("\n");
1350}
1351
Marc Bouchere6869a82000-03-20 06:03:29 +00001352
1353static int
1354print_match(const struct ipt_entry_match *m,
1355 const struct ipt_ip *ip,
1356 int numeric)
1357{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001358 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001359
1360 if (match) {
1361 if (match->print)
1362 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001363 else
Rusty Russellb039b022000-09-01 06:04:05 +00001364 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001365 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001366 if (m->u.user.name[0])
1367 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001368 }
1369 /* Don't stop iterating. */
1370 return 0;
1371}
1372
1373/* e is called `fw' here for hysterical raisins */
1374static void
1375print_firewall(const struct ipt_entry *fw,
1376 const char *targname,
1377 unsigned int num,
1378 unsigned int format,
1379 const iptc_handle_t handle)
1380{
1381 struct iptables_target *target = NULL;
1382 const struct ipt_entry_target *t;
1383 u_int8_t flags;
1384 char buf[BUFSIZ];
1385
Marc Bouchere6869a82000-03-20 06:03:29 +00001386 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001387 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001388 else
Rusty Russell52a51492000-05-02 16:44:29 +00001389 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001390
1391 t = ipt_get_target((struct ipt_entry *)fw);
1392 flags = fw->ip.flags;
1393
1394 if (format & FMT_LINENUMBERS)
1395 printf(FMT("%-4u ", "%u "), num+1);
1396
1397 if (!(format & FMT_NOCOUNTS)) {
1398 print_num(fw->counters.pcnt, format);
1399 print_num(fw->counters.bcnt, format);
1400 }
1401
1402 if (!(format & FMT_NOTARGET))
1403 printf(FMT("%-9s ", "%s "), targname);
1404
1405 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1406 {
Rusty Russell28381a42000-05-10 00:19:50 +00001407 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001408 if (pname)
1409 printf(FMT("%-5s", "%s "), pname);
1410 else
1411 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1412 }
1413
1414 if (format & FMT_OPTIONS) {
1415 if (format & FMT_NOTABLE)
1416 fputs("opt ", stdout);
1417 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1418 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1419 fputc(' ', stdout);
1420 }
1421
1422 if (format & FMT_VIA) {
1423 char iface[IFNAMSIZ+2];
1424
1425 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1426 iface[0] = '!';
1427 iface[1] = '\0';
1428 }
1429 else iface[0] = '\0';
1430
1431 if (fw->ip.iniface[0] != '\0') {
1432 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001433 }
1434 else if (format & FMT_NUMERIC) strcat(iface, "*");
1435 else strcat(iface, "any");
1436 printf(FMT(" %-6s ","in %s "), iface);
1437
1438 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1439 iface[0] = '!';
1440 iface[1] = '\0';
1441 }
1442 else iface[0] = '\0';
1443
1444 if (fw->ip.outiface[0] != '\0') {
1445 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001446 }
1447 else if (format & FMT_NUMERIC) strcat(iface, "*");
1448 else strcat(iface, "any");
1449 printf(FMT("%-6s ","out %s "), iface);
1450 }
1451
1452 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1453 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1454 printf(FMT("%-19s ","%s "), "anywhere");
1455 else {
1456 if (format & FMT_NUMERIC)
1457 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1458 else
1459 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1460 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1461 printf(FMT("%-19s ","%s "), buf);
1462 }
1463
1464 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1465 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001466 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001467 else {
1468 if (format & FMT_NUMERIC)
1469 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1470 else
1471 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1472 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001473 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001474 }
1475
1476 if (format & FMT_NOTABLE)
1477 fputs(" ", stdout);
1478
Harald Welte72bd87e2005-11-24 17:04:05 +00001479#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001480 if(fw->ip.flags & IPT_F_GOTO)
1481 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001482#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001483
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1485
1486 if (target) {
1487 if (target->print)
1488 /* Print the target information. */
1489 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001490 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001491 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001492 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001493
1494 if (!(format & FMT_NONEWLINE))
1495 fputc('\n', stdout);
1496}
1497
1498static void
1499print_firewall_line(const struct ipt_entry *fw,
1500 const iptc_handle_t h)
1501{
1502 struct ipt_entry_target *t;
1503
1504 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001505 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001506}
1507
1508static int
1509append_entry(const ipt_chainlabel chain,
1510 struct ipt_entry *fw,
1511 unsigned int nsaddrs,
1512 const struct in_addr saddrs[],
1513 unsigned int ndaddrs,
1514 const struct in_addr daddrs[],
1515 int verbose,
1516 iptc_handle_t *handle)
1517{
1518 unsigned int i, j;
1519 int ret = 1;
1520
1521 for (i = 0; i < nsaddrs; i++) {
1522 fw->ip.src.s_addr = saddrs[i].s_addr;
1523 for (j = 0; j < ndaddrs; j++) {
1524 fw->ip.dst.s_addr = daddrs[j].s_addr;
1525 if (verbose)
1526 print_firewall_line(fw, *handle);
1527 ret &= iptc_append_entry(chain, fw, handle);
1528 }
1529 }
1530
1531 return ret;
1532}
1533
1534static int
1535replace_entry(const ipt_chainlabel chain,
1536 struct ipt_entry *fw,
1537 unsigned int rulenum,
1538 const struct in_addr *saddr,
1539 const struct in_addr *daddr,
1540 int verbose,
1541 iptc_handle_t *handle)
1542{
1543 fw->ip.src.s_addr = saddr->s_addr;
1544 fw->ip.dst.s_addr = daddr->s_addr;
1545
1546 if (verbose)
1547 print_firewall_line(fw, *handle);
1548 return iptc_replace_entry(chain, fw, rulenum, handle);
1549}
1550
1551static int
1552insert_entry(const ipt_chainlabel chain,
1553 struct ipt_entry *fw,
1554 unsigned int rulenum,
1555 unsigned int nsaddrs,
1556 const struct in_addr saddrs[],
1557 unsigned int ndaddrs,
1558 const struct in_addr daddrs[],
1559 int verbose,
1560 iptc_handle_t *handle)
1561{
1562 unsigned int i, j;
1563 int ret = 1;
1564
1565 for (i = 0; i < nsaddrs; i++) {
1566 fw->ip.src.s_addr = saddrs[i].s_addr;
1567 for (j = 0; j < ndaddrs; j++) {
1568 fw->ip.dst.s_addr = daddrs[j].s_addr;
1569 if (verbose)
1570 print_firewall_line(fw, *handle);
1571 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1572 }
1573 }
1574
1575 return ret;
1576}
1577
Rusty Russell2e0a3212000-04-19 11:23:18 +00001578static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001579make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001580{
1581 /* Establish mask for comparison */
1582 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001583 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001584 unsigned char *mask, *mptr;
1585
1586 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001587 for (matchp = matches; matchp; matchp = matchp->next)
1588 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001589
Rusty Russell9e1d2142000-04-23 09:11:12 +00001590 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001591 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001592 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001593
Rusty Russell9e1d2142000-04-23 09:11:12 +00001594 memset(mask, 0xFF, sizeof(struct ipt_entry));
1595 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001596
Martin Josefsson78cafda2004-02-02 20:01:18 +00001597 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001598 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001599 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001600 + matchp->match->userspacesize);
1601 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001602 }
1603
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001604 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001605 IPT_ALIGN(sizeof(struct ipt_entry_target))
1606 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001607
1608 return mask;
1609}
1610
Marc Bouchere6869a82000-03-20 06:03:29 +00001611static int
1612delete_entry(const ipt_chainlabel chain,
1613 struct ipt_entry *fw,
1614 unsigned int nsaddrs,
1615 const struct in_addr saddrs[],
1616 unsigned int ndaddrs,
1617 const struct in_addr daddrs[],
1618 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001619 iptc_handle_t *handle,
1620 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001621{
1622 unsigned int i, j;
1623 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001624 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001625
Martin Josefsson78cafda2004-02-02 20:01:18 +00001626 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 for (i = 0; i < nsaddrs; i++) {
1628 fw->ip.src.s_addr = saddrs[i].s_addr;
1629 for (j = 0; j < ndaddrs; j++) {
1630 fw->ip.dst.s_addr = daddrs[j].s_addr;
1631 if (verbose)
1632 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001633 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001634 }
1635 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001636 free(mask);
1637
Marc Bouchere6869a82000-03-20 06:03:29 +00001638 return ret;
1639}
1640
Harald Welteae1ff9f2000-12-01 14:26:20 +00001641int
Marc Bouchere6869a82000-03-20 06:03:29 +00001642for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001643 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001644{
1645 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001646 const char *chain;
1647 char *chains;
1648 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001649
Rusty Russell9e1d2142000-04-23 09:11:12 +00001650 chain = iptc_first_chain(handle);
1651 while (chain) {
1652 chaincount++;
1653 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001654 }
1655
Rusty Russell9e1d2142000-04-23 09:11:12 +00001656 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1657 i = 0;
1658 chain = iptc_first_chain(handle);
1659 while (chain) {
1660 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1661 i++;
1662 chain = iptc_next_chain(handle);
1663 }
1664
1665 for (i = 0; i < chaincount; i++) {
1666 if (!builtinstoo
1667 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001668 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001669 continue;
1670 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1671 }
1672
1673 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 return ret;
1675}
1676
Harald Welteae1ff9f2000-12-01 14:26:20 +00001677int
Marc Bouchere6869a82000-03-20 06:03:29 +00001678flush_entries(const ipt_chainlabel chain, int verbose,
1679 iptc_handle_t *handle)
1680{
1681 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001682 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001683
1684 if (verbose)
1685 fprintf(stdout, "Flushing chain `%s'\n", chain);
1686 return iptc_flush_entries(chain, handle);
1687}
Marc Bouchere6869a82000-03-20 06:03:29 +00001688
1689static int
1690zero_entries(const ipt_chainlabel chain, int verbose,
1691 iptc_handle_t *handle)
1692{
1693 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001694 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001695
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 if (verbose)
1697 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1698 return iptc_zero_entries(chain, handle);
1699}
1700
Harald Welteae1ff9f2000-12-01 14:26:20 +00001701int
Marc Bouchere6869a82000-03-20 06:03:29 +00001702delete_chain(const ipt_chainlabel chain, int verbose,
1703 iptc_handle_t *handle)
1704{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001705 if (!chain)
1706 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001707
1708 if (verbose)
1709 fprintf(stdout, "Deleting chain `%s'\n", chain);
1710 return iptc_delete_chain(chain, handle);
1711}
1712
1713static int
1714list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1715 int expanded, int linenumbers, iptc_handle_t *handle)
1716{
1717 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001718 unsigned int format;
1719 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001720
1721 format = FMT_OPTIONS;
1722 if (!verbose)
1723 format |= FMT_NOCOUNTS;
1724 else
1725 format |= FMT_VIA;
1726
1727 if (numeric)
1728 format |= FMT_NUMERIC;
1729
1730 if (!expanded)
1731 format |= FMT_KILOMEGAGIGA;
1732
1733 if (linenumbers)
1734 format |= FMT_LINENUMBERS;
1735
Rusty Russell9e1d2142000-04-23 09:11:12 +00001736 for (this = iptc_first_chain(handle);
1737 this;
1738 this = iptc_next_chain(handle)) {
1739 const struct ipt_entry *i;
1740 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001741
Marc Bouchere6869a82000-03-20 06:03:29 +00001742 if (chain && strcmp(chain, this) != 0)
1743 continue;
1744
1745 if (found) printf("\n");
1746
1747 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001748 i = iptc_first_rule(this, handle);
1749
1750 num = 0;
1751 while (i) {
1752 print_firewall(i,
1753 iptc_get_target(i, handle),
1754 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001755 format,
1756 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001757 i = iptc_next_rule(i, handle);
1758 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001759 found = 1;
1760 }
1761
1762 errno = ENOENT;
1763 return found;
1764}
1765
Harald Welte82dd2ec2000-12-19 05:18:15 +00001766static char *get_modprobe(void)
1767{
1768 int procfile;
1769 char *ret;
1770
Harald Welte10f7f142004-10-22 08:14:07 +00001771#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001772 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1773 if (procfile < 0)
1774 return NULL;
1775
Harald Welte10f7f142004-10-22 08:14:07 +00001776 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001777 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001778 memset(ret, 0, PROCFILE_BUFSIZ);
1779 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001780 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001781 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001782 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001783 if (ret[strlen(ret)-1]=='\n')
1784 ret[strlen(ret)-1]=0;
1785 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001786 return ret;
1787 }
1788 fail:
1789 free(ret);
1790 close(procfile);
1791 return NULL;
1792}
1793
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001794int iptables_insmod(const char *modname, const char *modprobe, int quiet)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001795{
1796 char *buf = NULL;
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001797 char *argv[4];
Rusty Russell8beb0492004-12-22 00:37:10 +00001798 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001799
1800 /* If they don't explicitly set it, read out of kernel */
1801 if (!modprobe) {
1802 buf = get_modprobe();
1803 if (!buf)
1804 return -1;
1805 modprobe = buf;
1806 }
1807
1808 switch (fork()) {
1809 case 0:
1810 argv[0] = (char *)modprobe;
1811 argv[1] = (char *)modname;
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001812 if (quiet) {
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001813 argv[2] = "-q";
1814 argv[3] = NULL;
1815 } else {
1816 argv[2] = NULL;
1817 argv[3] = NULL;
1818 }
Harald Welte82dd2ec2000-12-19 05:18:15 +00001819 execv(argv[0], argv);
1820
1821 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001822 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001823 case -1:
1824 return -1;
1825
1826 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001827 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001828 }
1829
1830 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001831 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1832 return 0;
1833 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001834}
1835
Yasuyuki KOZAKAI29647c82007-03-20 15:51:41 +00001836int load_iptables_ko(const char *modprobe, int quiet)
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001837{
1838 static int loaded = 0;
1839 static int ret = -1;
1840
1841 if (!loaded) {
Pablo Neira AyusoMaurice van der Potcd2f6d22007-04-16 17:15:22 +00001842 ret = iptables_insmod("ip_tables", modprobe, quiet);
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00001843 loaded = (ret == 0);
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001844 }
1845
1846 return ret;
1847}
1848
Marc Bouchere6869a82000-03-20 06:03:29 +00001849static struct ipt_entry *
1850generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001851 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001852 struct ipt_entry_target *target)
1853{
1854 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001855 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 struct ipt_entry *e;
1857
1858 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001859 for (matchp = matches; matchp; matchp = matchp->next)
1860 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001861
Rusty Russell228e98d2000-04-27 10:28:06 +00001862 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001863 *e = *fw;
1864 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001865 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
1867 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001868 for (matchp = matches; matchp; matchp = matchp->next) {
1869 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1870 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001871 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001872 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001873
1874 return e;
1875}
1876
Martin Josefsson78cafda2004-02-02 20:01:18 +00001877void clear_rule_matches(struct iptables_rule_match **matches)
1878{
1879 struct iptables_rule_match *matchp, *tmp;
1880
1881 for (matchp = *matches; matchp;) {
1882 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001883 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001884 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001885 matchp->match->m = NULL;
1886 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001887 if (matchp->match == matchp->match->next) {
1888 free(matchp->match);
1889 matchp->match = NULL;
1890 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001891 free(matchp);
1892 matchp = tmp;
1893 }
1894
1895 *matches = NULL;
1896}
1897
Rusty Russell3aef54d2005-01-03 03:48:40 +00001898static void set_revision(char *name, u_int8_t revision)
1899{
1900 /* Old kernel sources don't have ".revision" field,
1901 but we stole a byte from name. */
1902 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1903 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1904}
1905
Phil Oester8cf65912005-09-19 15:00:33 +00001906void
1907get_kernel_version(void) {
1908 static struct utsname uts;
1909 int x = 0, y = 0, z = 0;
1910
1911 if (uname(&uts) == -1) {
1912 fprintf(stderr, "Unable to retrieve kernel version.\n");
1913 free_opts(1);
1914 exit(1);
1915 }
1916
1917 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1918 kernel_version = LINUX_VERSION(x, y, z);
1919}
1920
Marc Bouchere6869a82000-03-20 06:03:29 +00001921int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1922{
1923 struct ipt_entry fw, *e = NULL;
1924 int invert = 0;
1925 unsigned int nsaddrs = 0, ndaddrs = 0;
1926 struct in_addr *saddrs = NULL, *daddrs = NULL;
1927
1928 int c, verbose = 0;
1929 const char *chain = NULL;
1930 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1931 const char *policy = NULL, *newname = NULL;
1932 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001933 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 int ret = 1;
1935 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001936 struct iptables_rule_match *matches = NULL;
1937 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001938 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001939 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001940 const char *jumpto = "";
1941 char *protocol = 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 */
Yasuyuki KOZAKAI0e9480b2007-03-13 08:17:59 +00002429 if (!*handle && load_iptables_ko(modprobe, 0) != -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}