blob: d4aa26fd54019daa435399d18d217704c3380ab3 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
41#include <sys/wait.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000043
44#ifndef TRUE
45#define TRUE 1
46#endif
47#ifndef FALSE
48#define FALSE 0
49#endif
50
Harald Welte82dd2ec2000-12-19 05:18:15 +000051#ifndef PROC_SYS_MODPROBE
52#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000083#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000085 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000086
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_FRAGMENT 0x00200U
100#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000101#define OPT_COUNTERS 0x00800U
102#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000103static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000104= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000105
106static struct option original_opts[] = {
107 { "append", 1, 0, 'A' },
108 { "delete", 1, 0, 'D' },
109 { "insert", 1, 0, 'I' },
110 { "replace", 1, 0, 'R' },
111 { "list", 2, 0, 'L' },
112 { "flush", 2, 0, 'F' },
113 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000116 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000122 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "fragments", 0, 0, 'f' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000135 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000136 { "set-counters", 1, 0, 'c' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000137 { "goto", 1, 0, 'g' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 { 0 }
139};
140
Illes Marci63e90632003-03-03 08:08:37 +0000141/* we need this for iptables-restore. iptables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. iptables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
146
Marc Bouchere6869a82000-03-20 06:03:29 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162 /* -n -s -d -p -j -v -x -i -o -f --line -c */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000189/*--line*/ 0,
190/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000191};
192
193const char *program_version;
194const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000195char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196
Phil Oester8cf65912005-09-19 15:00:33 +0000197int kernel_version;
198
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +0000199/* the path to command to load kernel module */
200const char *modprobe = NULL;
201
Rusty Russell2e0a3212000-04-19 11:23:18 +0000202/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000203struct iptables_match *iptables_matches = NULL;
204struct iptables_target *iptables_targets = NULL;
205
206/* Extra debugging from libiptc */
207extern void dump_entries(const iptc_handle_t handle);
208
209/* A few hardcoded protocols for 'all' and in case the user has no
210 /etc/protocols */
211struct pprot {
212 char *name;
213 u_int8_t num;
214};
215
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000216/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000217/* defined in netinet/in.h */
218#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219#ifndef IPPROTO_ESP
220#define IPPROTO_ESP 50
221#endif
222#ifndef IPPROTO_AH
223#define IPPROTO_AH 51
224#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000225#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000226
Marc Bouchere6869a82000-03-20 06:03:29 +0000227static const struct pprot chain_protos[] = {
228 { "tcp", IPPROTO_TCP },
229 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000230 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000232 { "esp", IPPROTO_ESP },
233 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000234 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000235};
236
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000237static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000238proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000239{
240 unsigned int i;
241
Rusty Russell28381a42000-05-10 00:19:50 +0000242 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000243 struct protoent *pent = getprotobynumber(proto);
244 if (pent)
245 return pent->p_name;
246 }
247
248 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
249 if (chain_protos[i].num == proto)
250 return chain_protos[i].name;
251
252 return NULL;
253}
254
Phil Oester58179b12006-07-20 17:00:19 +0000255int
256service_to_port(const char *name, const char *proto)
257{
258 struct servent *service;
259
260 if ((service = getservbyname(name, proto)) != NULL)
261 return ntohs((unsigned short) service->s_port);
262
263 return -1;
264}
265
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000266u_int16_t
267parse_port(const char *port, const char *proto)
268{
269 unsigned int portnum;
270
271 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
272 (portnum = service_to_port(port, proto)) != -1)
273 return (u_int16_t)portnum;
274
275 exit_error(PARAMETER_PROBLEM,
276 "invalid port/service `%s' specified", port);
277}
278
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000279enum {
280 IPT_DOTTED_ADDR = 0,
281 IPT_DOTTED_MASK
282};
283
284static struct in_addr *
285__dotted_to_addr(const char *dotted, int type)
Marc Bouchere6869a82000-03-20 06:03:29 +0000286{
287 static struct in_addr addr;
288 unsigned char *addrp;
289 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000290 unsigned int onebyte;
291 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000292 char buf[20];
293
294 /* copy dotted string, because we need to modify it */
295 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000296 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000297 addrp = (unsigned char *) &(addr.s_addr);
298
299 p = buf;
300 for (i = 0; i < 3; i++) {
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000301 if ((q = strchr(p, '.')) == NULL) {
302 if (type == IPT_DOTTED_ADDR) {
303 /* autocomplete, this is a network address */
304 if (string_to_number(p, 0, 255, &onebyte) == -1)
305 return (struct in_addr *) NULL;
306
307 addrp[i] = (unsigned char) onebyte;
308 while (i < 3)
309 addrp[++i] = 0;
310
311 return &addr;
312 } else
313 return (struct in_addr *) NULL;
314 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000315
316 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000317 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000318 return (struct in_addr *) NULL;
319
320 addrp[i] = (unsigned char) onebyte;
321 p = q + 1;
322 }
323
324 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000325 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000326 return (struct in_addr *) NULL;
327
328 addrp[3] = (unsigned char) onebyte;
329
330 return &addr;
331}
332
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000333struct in_addr *
334dotted_to_addr(const char *dotted)
335{
336 return __dotted_to_addr(dotted, IPT_DOTTED_ADDR);
337}
338
339struct in_addr *
340dotted_to_mask(const char *dotted)
341{
342 return __dotted_to_addr(dotted, IPT_DOTTED_MASK);
343}
344
Marc Bouchere6869a82000-03-20 06:03:29 +0000345static struct in_addr *
346network_to_addr(const char *name)
347{
348 struct netent *net;
349 static struct in_addr addr;
350
351 if ((net = getnetbyname(name)) != NULL) {
352 if (net->n_addrtype != AF_INET)
353 return (struct in_addr *) NULL;
354 addr.s_addr = htonl((unsigned long) net->n_net);
355 return &addr;
356 }
357
358 return (struct in_addr *) NULL;
359}
360
361static void
362inaddrcpy(struct in_addr *dst, struct in_addr *src)
363{
364 /* memcpy(dst, src, sizeof(struct in_addr)); */
365 dst->s_addr = src->s_addr;
366}
367
Pablo Neiradfdcd642005-05-29 19:05:23 +0000368static void free_opts(int reset_offset)
369{
370 if (opts != original_opts) {
371 free(opts);
372 opts = original_opts;
373 if (reset_offset)
374 global_option_offset = 0;
375 }
376}
377
Marc Bouchere6869a82000-03-20 06:03:29 +0000378void
379exit_error(enum exittype status, char *msg, ...)
380{
381 va_list args;
382
383 va_start(args, msg);
384 fprintf(stderr, "%s v%s: ", program_name, program_version);
385 vfprintf(stderr, msg, args);
386 va_end(args);
387 fprintf(stderr, "\n");
388 if (status == PARAMETER_PROBLEM)
389 exit_tryhelp(status);
390 if (status == VERSION_PROBLEM)
391 fprintf(stderr,
392 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000393 /* On error paths, make sure that we don't leak memory */
394 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000395 exit(status);
396}
397
398void
399exit_tryhelp(int status)
400{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000401 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000402 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
404 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000405 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000406 exit(status);
407}
408
409void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000410exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000411{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000412 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000413 struct iptables_target *t = NULL;
414
Marc Bouchere6869a82000-03-20 06:03:29 +0000415 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000416"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000417" %s -[RI] chain rulenum rule-specification [options]\n"
418" %s -D chain rulenum [options]\n"
419" %s -[LFZ] [chain] [options]\n"
420" %s -[NX] chain\n"
421" %s -E old-chain-name new-chain-name\n"
422" %s -P chain target [options]\n"
423" %s -h (print this help information)\n\n",
424 program_name, program_version, program_name, program_name,
425 program_name, program_name, program_name, program_name,
426 program_name, program_name);
427
428 printf(
429"Commands:\n"
430"Either long or short options are allowed.\n"
431" --append -A chain Append to chain\n"
432" --delete -D chain Delete matching rule from chain\n"
433" --delete -D chain rulenum\n"
434" Delete rule rulenum (1 = first) from chain\n"
435" --insert -I chain [rulenum]\n"
436" Insert in chain as rulenum (default 1=first)\n"
437" --replace -R chain rulenum\n"
438" Replace rule rulenum (1 = first) in chain\n"
439" --list -L [chain] List the rules in a chain or all chains\n"
440" --flush -F [chain] Delete all rules in chain or all chains\n"
441" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000442" --new -N chain Create a new user-defined chain\n"
443" --delete-chain\n"
444" -X [chain] Delete a user-defined chain\n"
445" --policy -P chain target\n"
446" Change policy on chain to target\n"
447" --rename-chain\n"
448" -E old-chain new-chain\n"
449" Change chain name, (moving any references)\n"
450
451"Options:\n"
452" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
453" --source -s [!] address[/mask]\n"
454" source specification\n"
455" --destination -d [!] address[/mask]\n"
456" destination specification\n"
457" --in-interface -i [!] input name[+]\n"
458" network interface name ([+] for wildcard)\n"
459" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000460" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000461#ifdef IPT_F_GOTO
462" --goto -g chain\n"
463" jump to chain with no return\n"
464#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000465" --match -m match\n"
466" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000467" --numeric -n numeric output of addresses and ports\n"
468" --out-interface -o [!] output name[+]\n"
469" network interface name ([+] for wildcard)\n"
470" --table -t table table to manipulate (default: `filter')\n"
471" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000472" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000473" --exact -x expand numbers (display exact values)\n"
474"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000475" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000476" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000477"[!] --version -V print package version.\n");
478
Rusty Russell363112d2000-08-11 13:49:26 +0000479 /* Print out any special helps. A user might like to be able
480 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000481 results. So we call help for all specified matches & targets */
482 for (t = iptables_targets; t ;t = t->next) {
483 if (t->used) {
484 printf("\n");
485 t->help();
486 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000487 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000488 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000489 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000490 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000491 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000492 exit(0);
493}
494
495static void
496generic_opt_check(int command, int options)
497{
498 int i, j, legal = 0;
499
500 /* Check that commands are valid with options. Complicated by the
501 * fact that if an option is legal with *any* command given, it is
502 * legal overall (ie. -z and -l).
503 */
504 for (i = 0; i < NUMBER_OF_OPT; i++) {
505 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
506
507 for (j = 0; j < NUMBER_OF_CMD; j++) {
508 if (!(command & (1<<j)))
509 continue;
510
511 if (!(options & (1<<i))) {
512 if (commands_v_options[j][i] == '+')
513 exit_error(PARAMETER_PROBLEM,
514 "You need to supply the `-%c' "
515 "option for this command\n",
516 optflags[i]);
517 } else {
518 if (commands_v_options[j][i] != 'x')
519 legal = 1;
520 else if (legal == 0)
521 legal = -1;
522 }
523 }
524 if (legal == -1)
525 exit_error(PARAMETER_PROBLEM,
526 "Illegal option `-%c' with this command\n",
527 optflags[i]);
528 }
529}
530
531static char
532opt2char(int option)
533{
534 const char *ptr;
535 for (ptr = optflags; option > 1; option >>= 1, ptr++);
536
537 return *ptr;
538}
539
540static char
541cmd2char(int option)
542{
543 const char *ptr;
544 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
545
546 return *ptr;
547}
548
549static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000550add_command(unsigned int *cmd, const int newcmd, const int othercmds,
551 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000552{
553 if (invert)
554 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
555 if (*cmd & (~othercmds))
556 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
557 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
558 *cmd |= newcmd;
559}
560
561int
Harald Welteb77f1da2002-03-14 11:35:58 +0000562check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000563{
564 if (option && strcmp(option, "!") == 0) {
565 if (*invert)
566 exit_error(PARAMETER_PROBLEM,
567 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000568 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000569 if (optind) {
570 *optind = *optind+1;
571 if (argc && *optind > argc)
572 exit_error(PARAMETER_PROBLEM,
573 "no argument following `!'");
574 }
575
Marc Bouchere6869a82000-03-20 06:03:29 +0000576 return TRUE;
577 }
578 return FALSE;
579}
580
581static void *
582fw_calloc(size_t count, size_t size)
583{
584 void *p;
585
586 if ((p = calloc(count, size)) == NULL) {
587 perror("iptables: calloc failed");
588 exit(1);
589 }
590 return p;
591}
592
593static void *
594fw_malloc(size_t size)
595{
596 void *p;
597
598 if ((p = malloc(size)) == NULL) {
599 perror("iptables: malloc failed");
600 exit(1);
601 }
602 return p;
603}
604
605static struct in_addr *
606host_to_addr(const char *name, unsigned int *naddr)
607{
608 struct hostent *host;
609 struct in_addr *addr;
610 unsigned int i;
611
612 *naddr = 0;
613 if ((host = gethostbyname(name)) != NULL) {
614 if (host->h_addrtype != AF_INET ||
615 host->h_length != sizeof(struct in_addr))
616 return (struct in_addr *) NULL;
617
618 while (host->h_addr_list[*naddr] != (char *) NULL)
619 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000620 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000621 for (i = 0; i < *naddr; i++)
622 inaddrcpy(&(addr[i]),
623 (struct in_addr *) host->h_addr_list[i]);
624 return addr;
625 }
626
627 return (struct in_addr *) NULL;
628}
629
630static char *
631addr_to_host(const struct in_addr *addr)
632{
633 struct hostent *host;
634
635 if ((host = gethostbyaddr((char *) addr,
636 sizeof(struct in_addr), AF_INET)) != NULL)
637 return (char *) host->h_name;
638
639 return (char *) NULL;
640}
641
642/*
643 * All functions starting with "parse" should succeed, otherwise
644 * the program fails.
645 * Most routines return pointers to static data that may change
646 * between calls to the same or other routines with a few exceptions:
647 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
648 * return global static data.
649*/
650
651static struct in_addr *
652parse_hostnetwork(const char *name, unsigned int *naddrs)
653{
654 struct in_addr *addrp, *addrptmp;
655
656 if ((addrptmp = dotted_to_addr(name)) != NULL ||
657 (addrptmp = network_to_addr(name)) != NULL) {
658 addrp = fw_malloc(sizeof(struct in_addr));
659 inaddrcpy(addrp, addrptmp);
660 *naddrs = 1;
661 return addrp;
662 }
663 if ((addrp = host_to_addr(name, naddrs)) != NULL)
664 return addrp;
665
666 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
667}
668
669static struct in_addr *
670parse_mask(char *mask)
671{
672 static struct in_addr maskaddr;
673 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000674 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000675
676 if (mask == NULL) {
677 /* no mask at all defaults to 32 bits */
678 maskaddr.s_addr = 0xFFFFFFFF;
679 return &maskaddr;
680 }
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000681 if ((addrp = dotted_to_mask(mask)) != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +0000682 /* dotted_to_addr already returns a network byte order addr */
683 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000684 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000685 exit_error(PARAMETER_PROBLEM,
686 "invalid mask `%s' specified", mask);
687 if (bits != 0) {
688 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
689 return &maskaddr;
690 }
691
692 maskaddr.s_addr = 0L;
693 return &maskaddr;
694}
695
Marc Boucherb93c7982001-12-06 14:50:19 +0000696void
Marc Bouchere6869a82000-03-20 06:03:29 +0000697parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
698 struct in_addr *maskp, unsigned int *naddrs)
699{
700 struct in_addr *addrp;
701 char buf[256];
702 char *p;
703 int i, j, k, n;
704
705 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000706 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000707 if ((p = strrchr(buf, '/')) != NULL) {
708 *p = '\0';
709 addrp = parse_mask(p + 1);
710 } else
711 addrp = parse_mask(NULL);
712 inaddrcpy(maskp, addrp);
713
714 /* if a null mask is given, the name is ignored, like in "any/0" */
715 if (maskp->s_addr == 0L)
716 strcpy(buf, "0.0.0.0");
717
718 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
719 n = *naddrs;
720 for (i = 0, j = 0; i < n; i++) {
721 addrp[j++].s_addr &= maskp->s_addr;
722 for (k = 0; k < j - 1; k++) {
723 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
724 (*naddrs)--;
725 j--;
726 break;
727 }
728 }
729 }
730}
731
732struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000733find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000734{
735 struct iptables_match *ptr;
736
737 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000738 if (strcmp(name, ptr->name) == 0) {
739 struct iptables_match *clone;
740
741 /* First match of this type: */
742 if (ptr->m == NULL)
743 break;
744
745 /* Second and subsequent clones */
746 clone = fw_malloc(sizeof(struct iptables_match));
747 memcpy(clone, ptr, sizeof(struct iptables_match));
748 clone->mflags = 0;
749 /* This is a clone: */
750 clone->next = clone;
751
752 ptr = clone;
Marc Bouchere6869a82000-03-20 06:03:29 +0000753 break;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000754 }
755 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000756
Harald Welte3efb6ea2001-08-06 18:50:21 +0000757#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000758 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000759 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000760 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000761 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000762 if (dlopen(path, RTLD_NOW)) {
763 /* Found library. If it didn't register itself,
764 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000765 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000766
Rusty Russell9e1d2142000-04-23 09:11:12 +0000767 if (!ptr)
768 exit_error(PARAMETER_PROBLEM,
769 "Couldn't load match `%s'\n",
770 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000771 } else if (tryload == LOAD_MUST_SUCCEED)
772 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000773 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000774 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000775 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000776#else
777 if (ptr && !ptr->loaded) {
778 if (tryload != DONT_LOAD)
779 ptr->loaded = 1;
780 else
781 ptr = NULL;
782 }
Marc Boucher067477b2002-03-24 15:09:31 +0000783 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
784 exit_error(PARAMETER_PROBLEM,
785 "Couldn't find match `%s'\n", name);
786 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000787#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000788
Martin Josefsson78cafda2004-02-02 20:01:18 +0000789 if (ptr && matches) {
790 struct iptables_rule_match **i;
791 struct iptables_rule_match *newentry;
792
793 newentry = fw_malloc(sizeof(struct iptables_rule_match));
794
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000795 for (i = matches; *i; i = &(*i)->next) {
796 if (strcmp(name, (*i)->match->name) == 0)
797 (*i)->completed = 1;
798 }
Martin Josefsson78cafda2004-02-02 20:01:18 +0000799 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000800 newentry->completed = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000801 newentry->next = NULL;
802 *i = newentry;
803 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000804
Marc Bouchere6869a82000-03-20 06:03:29 +0000805 return ptr;
806}
807
Rusty Russell28381a42000-05-10 00:19:50 +0000808/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
809static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000810find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000811{
Harald Welteed498492001-07-23 01:24:22 +0000812 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000813
Harald Welte0b0013a2002-02-18 16:15:31 +0000814 if (string_to_number(pname, 0, 255, &proto) != -1) {
815 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000816
Harald Welte0b0013a2002-02-18 16:15:31 +0000817 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000818 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000819 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000820 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000821
822 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000823}
824
Marc Boucherb93c7982001-12-06 14:50:19 +0000825u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000826parse_protocol(const char *s)
827{
Harald Welteed498492001-07-23 01:24:22 +0000828 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000829
Harald Welteed498492001-07-23 01:24:22 +0000830 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000831 struct protoent *pent;
832
Harald Weltecbe1ec72006-02-11 09:50:11 +0000833 /* first deal with the special case of 'all' to prevent
834 * people from being able to redefine 'all' in nsswitch
835 * and/or provoke expensive [not working] ldap/nis/...
836 * lookups */
837 if (!strcmp(s, "all"))
838 return 0;
839
Marc Bouchere6869a82000-03-20 06:03:29 +0000840 if ((pent = getprotobyname(s)))
841 proto = pent->p_proto;
842 else {
843 unsigned int i;
844 for (i = 0;
845 i < sizeof(chain_protos)/sizeof(struct pprot);
846 i++) {
847 if (strcmp(s, chain_protos[i].name) == 0) {
848 proto = chain_protos[i].num;
849 break;
850 }
851 }
852 if (i == sizeof(chain_protos)/sizeof(struct pprot))
853 exit_error(PARAMETER_PROBLEM,
854 "unknown protocol `%s' specified",
855 s);
856 }
857 }
858
859 return (u_int16_t)proto;
860}
861
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000862void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000863{
864 int vialen = strlen(arg);
865 unsigned int i;
866
867 memset(mask, 0, IFNAMSIZ);
868 memset(vianame, 0, IFNAMSIZ);
869
870 if (vialen + 1 > IFNAMSIZ)
871 exit_error(PARAMETER_PROBLEM,
872 "interface name `%s' must be shorter than IFNAMSIZ"
873 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000874
Marc Bouchere6869a82000-03-20 06:03:29 +0000875 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000876 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000877 memset(mask, 0, IFNAMSIZ);
878 else if (vianame[vialen - 1] == '+') {
879 memset(mask, 0xFF, vialen - 1);
880 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000881 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000882 } else {
883 /* Include nul-terminator in match */
884 memset(mask, 0xFF, vialen + 1);
885 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000886 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000887 if (vianame[i] == ':' ||
888 vianame[i] == '!' ||
889 vianame[i] == '*') {
Patrick McHardy42bd67b2006-10-11 07:37:26 +0000890 printf("Warning: weird character in interface"
Harald Weltede1578f2001-05-23 23:07:33 +0000891 " `%s' (No aliases, :, ! or *).\n",
892 vianame);
893 break;
894 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000895 }
896 }
897}
898
899/* Can't be zero. */
900static int
901parse_rulenumber(const char *rule)
902{
Harald Welteed498492001-07-23 01:24:22 +0000903 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000904
Harald Welteed498492001-07-23 01:24:22 +0000905 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000906 exit_error(PARAMETER_PROBLEM,
907 "Invalid rule number `%s'", rule);
908
909 return rulenum;
910}
911
912static const char *
913parse_target(const char *targetname)
914{
915 const char *ptr;
916
917 if (strlen(targetname) < 1)
918 exit_error(PARAMETER_PROBLEM,
919 "Invalid target name (too short)");
920
921 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
922 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000923 "Invalid target name `%s' (%u chars max)",
924 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000925
926 for (ptr = targetname; *ptr; ptr++)
927 if (isspace(*ptr))
928 exit_error(PARAMETER_PROBLEM,
929 "Invalid target name `%s'", targetname);
930 return targetname;
931}
932
933static char *
934addr_to_network(const struct in_addr *addr)
935{
936 struct netent *net;
937
938 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
939 return (char *) net->n_name;
940
941 return (char *) NULL;
942}
943
944char *
945addr_to_dotted(const struct in_addr *addrp)
946{
947 static char buf[20];
948 const unsigned char *bytep;
949
950 bytep = (const unsigned char *) &(addrp->s_addr);
951 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
952 return buf;
953}
Marc Boucherb93c7982001-12-06 14:50:19 +0000954
955char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000956addr_to_anyname(const struct in_addr *addr)
957{
958 char *name;
959
960 if ((name = addr_to_host(addr)) != NULL ||
961 (name = addr_to_network(addr)) != NULL)
962 return name;
963
964 return addr_to_dotted(addr);
965}
966
Marc Boucherb93c7982001-12-06 14:50:19 +0000967char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000968mask_to_dotted(const struct in_addr *mask)
969{
970 int i;
971 static char buf[20];
972 u_int32_t maskaddr, bits;
973
974 maskaddr = ntohl(mask->s_addr);
975
976 if (maskaddr == 0xFFFFFFFFL)
977 /* we don't want to see "/32" */
978 return "";
979
980 i = 32;
981 bits = 0xFFFFFFFEL;
982 while (--i >= 0 && maskaddr != bits)
983 bits <<= 1;
984 if (i >= 0)
985 sprintf(buf, "/%d", i);
986 else
987 /* mask was not a decent combination of 1's and 0's */
988 sprintf(buf, "/%s", addr_to_dotted(mask));
989
990 return buf;
991}
992
993int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000994string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
995 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000996{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000997 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000998 char *end;
999
1000 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +00001001 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +00001002 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +00001003 if (*end == '\0' && end != s) {
1004 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +00001005 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +00001006 *ret = number;
1007 return 0;
1008 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001009 }
1010 return -1;
1011}
1012
Martin Josefssonb105bc92004-05-26 15:54:49 +00001013int
1014string_to_number_l(const char *s, unsigned long min, unsigned long max,
1015 unsigned long *ret)
1016{
1017 int result;
1018 unsigned long long number;
1019
1020 result = string_to_number_ll(s, min, max, &number);
1021 *ret = (unsigned long)number;
1022
1023 return result;
1024}
1025
1026int string_to_number(const char *s, unsigned int min, unsigned int max,
1027 unsigned int *ret)
1028{
1029 int result;
1030 unsigned long number;
1031
1032 result = string_to_number_l(s, min, max, &number);
1033 *ret = (unsigned int)number;
1034
1035 return result;
1036}
1037
Marc Bouchere6869a82000-03-20 06:03:29 +00001038static void
1039set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1040 int invert)
1041{
1042 if (*options & option)
1043 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1044 opt2char(option));
1045 *options |= option;
1046
1047 if (invert) {
1048 unsigned int i;
1049 for (i = 0; 1 << i != option; i++);
1050
1051 if (!inverse_for_options[i])
1052 exit_error(PARAMETER_PROBLEM,
1053 "cannot have ! before -%c",
1054 opt2char(option));
1055 *invflg |= inverse_for_options[i];
1056 }
1057}
1058
1059struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +00001060find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +00001061{
1062 struct iptables_target *ptr;
1063
1064 /* Standard target? */
1065 if (strcmp(name, "") == 0
1066 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
1067 || strcmp(name, IPTC_LABEL_DROP) == 0
1068 || strcmp(name, IPTC_LABEL_QUEUE) == 0
1069 || strcmp(name, IPTC_LABEL_RETURN) == 0)
1070 name = "standard";
1071
1072 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
1073 if (strcmp(name, ptr->name) == 0)
1074 break;
1075 }
1076
Harald Welte3efb6ea2001-08-06 18:50:21 +00001077#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001078 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001079 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001080 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001081 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001082 if (dlopen(path, RTLD_NOW)) {
1083 /* Found library. If it didn't register itself,
1084 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001085 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001086 if (!ptr)
1087 exit_error(PARAMETER_PROBLEM,
1088 "Couldn't load target `%s'\n",
1089 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001090 } else if (tryload == LOAD_MUST_SUCCEED)
1091 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001092 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001093 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001094 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001095#else
1096 if (ptr && !ptr->loaded) {
1097 if (tryload != DONT_LOAD)
1098 ptr->loaded = 1;
1099 else
1100 ptr = NULL;
1101 }
Marc Boucher067477b2002-03-24 15:09:31 +00001102 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1103 exit_error(PARAMETER_PROBLEM,
1104 "Couldn't find target `%s'\n", name);
1105 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001106#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001107
Harald Welteae1ff9f2000-12-01 14:26:20 +00001108 if (ptr)
1109 ptr->used = 1;
1110
Marc Bouchere6869a82000-03-20 06:03:29 +00001111 return ptr;
1112}
1113
1114static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001115merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001116 unsigned int *option_offset)
1117{
1118 unsigned int num_old, num_new, i;
1119 struct option *merge;
1120
1121 for (num_old = 0; oldopts[num_old].name; num_old++);
1122 for (num_new = 0; newopts[num_new].name; num_new++);
1123
1124 global_option_offset += OPTION_OFFSET;
1125 *option_offset = global_option_offset;
1126
1127 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1128 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001129 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001130 for (i = 0; i < num_new; i++) {
1131 merge[num_old + i] = newopts[i];
1132 merge[num_old + i].val += *option_offset;
1133 }
1134 memset(merge + num_old + num_new, 0, sizeof(struct option));
1135
1136 return merge;
1137}
1138
Rusty Russell3aef54d2005-01-03 03:48:40 +00001139static int compatible_revision(const char *name, u_int8_t revision, int opt)
1140{
1141 struct ipt_get_revision rev;
1142 socklen_t s = sizeof(rev);
1143 int max_rev, sockfd;
1144
1145 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1146 if (sockfd < 0) {
1147 fprintf(stderr, "Could not open socket to kernel: %s\n",
1148 strerror(errno));
1149 exit(1);
1150 }
1151
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001152 load_iptables_ko(modprobe);
1153
Rusty Russell3aef54d2005-01-03 03:48:40 +00001154 strcpy(rev.name, name);
1155 rev.revision = revision;
1156
1157 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1158 if (max_rev < 0) {
1159 /* Definitely don't support this? */
1160 if (errno == EPROTONOSUPPORT) {
1161 close(sockfd);
1162 return 0;
1163 } else if (errno == ENOPROTOOPT) {
1164 close(sockfd);
1165 /* Assume only revision 0 support (old kernel) */
1166 return (revision == 0);
1167 } else {
1168 fprintf(stderr, "getsockopt failed strangely: %s\n",
1169 strerror(errno));
1170 exit(1);
1171 }
1172 }
1173 close(sockfd);
1174 return 1;
1175}
1176
1177static int compatible_match_revision(const char *name, u_int8_t revision)
1178{
1179 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1180}
1181
1182static int compatible_target_revision(const char *name, u_int8_t revision)
1183{
1184 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1185}
1186
Marc Bouchere6869a82000-03-20 06:03:29 +00001187void
1188register_match(struct iptables_match *me)
1189{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001190 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001191
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 if (strcmp(me->version, program_version) != 0) {
1193 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1194 program_name, me->name, me->version, program_version);
1195 exit(1);
1196 }
1197
Martin Josefsson93911bf2005-01-03 07:46:07 +00001198 /* Revision field stole a char from name. */
1199 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001200 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 program_name, me->name);
1202 exit(1);
1203 }
1204
Jones Desougif5b86e62005-12-22 03:33:50 +00001205 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001206 if (old) {
1207 if (old->revision == me->revision) {
1208 fprintf(stderr,
1209 "%s: match `%s' already registered.\n",
1210 program_name, me->name);
1211 exit(1);
1212 }
1213
1214 /* Now we have two (or more) options, check compatibility. */
1215 if (compatible_match_revision(old->name, old->revision)
1216 && old->revision > me->revision)
1217 return;
1218
1219 /* Replace if compatible. */
1220 if (!compatible_match_revision(me->name, me->revision))
1221 return;
1222
1223 /* Delete old one. */
1224 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1225 *i = old->next;
1226 }
1227
Rusty Russell73f72f52000-07-03 10:17:57 +00001228 if (me->size != IPT_ALIGN(me->size)) {
1229 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001231 exit(1);
1232 }
1233
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001234 /* Append to list. */
1235 for (i = &iptables_matches; *i; i = &(*i)->next);
1236 me->next = NULL;
1237 *i = me;
1238
Marc Bouchere6869a82000-03-20 06:03:29 +00001239 me->m = NULL;
1240 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001241}
1242
1243void
1244register_target(struct iptables_target *me)
1245{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001246 struct iptables_target *old;
1247
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 if (strcmp(me->version, program_version) != 0) {
1249 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1250 program_name, me->name, me->version, program_version);
1251 exit(1);
1252 }
1253
Martin Josefsson93911bf2005-01-03 07:46:07 +00001254 /* Revision field stole a char from name. */
1255 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001256 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001257 program_name, me->name);
1258 exit(1);
1259 }
1260
Jones Desougif5b86e62005-12-22 03:33:50 +00001261 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001262 if (old) {
1263 struct iptables_target **i;
1264
1265 if (old->revision == me->revision) {
1266 fprintf(stderr,
1267 "%s: target `%s' already registered.\n",
1268 program_name, me->name);
1269 exit(1);
1270 }
1271
Rusty Russell3aef54d2005-01-03 03:48:40 +00001272 /* Now we have two (or more) options, check compatibility. */
1273 if (compatible_target_revision(old->name, old->revision)
1274 && old->revision > me->revision)
1275 return;
1276
1277 /* Replace if compatible. */
1278 if (!compatible_target_revision(me->name, me->revision))
1279 return;
1280
1281 /* Delete old one. */
1282 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1283 *i = old->next;
1284 }
1285
Rusty Russell73f72f52000-07-03 10:17:57 +00001286 if (me->size != IPT_ALIGN(me->size)) {
1287 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001288 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001289 exit(1);
1290 }
1291
Marc Bouchere6869a82000-03-20 06:03:29 +00001292 /* Prepend to list. */
1293 me->next = iptables_targets;
1294 iptables_targets = me;
1295 me->t = NULL;
1296 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001297}
1298
1299static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001300print_num(u_int64_t number, unsigned int format)
1301{
1302 if (format & FMT_KILOMEGAGIGA) {
1303 if (number > 99999) {
1304 number = (number + 500) / 1000;
1305 if (number > 9999) {
1306 number = (number + 500) / 1000;
1307 if (number > 9999) {
1308 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001309 if (number > 9999) {
1310 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001311 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001312 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001313 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001314 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001315 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001316 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001317 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001318 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001319 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001320 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001321 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001322}
1323
1324
1325static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001326print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1327{
1328 struct ipt_counters counters;
1329 const char *pol = iptc_get_policy(chain, &counters, handle);
1330 printf("Chain %s", chain);
1331 if (pol) {
1332 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001333 if (!(format & FMT_NOCOUNTS)) {
1334 fputc(' ', stdout);
1335 print_num(counters.pcnt, (format|FMT_NOTABLE));
1336 fputs("packets, ", stdout);
1337 print_num(counters.bcnt, (format|FMT_NOTABLE));
1338 fputs("bytes", stdout);
1339 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001340 printf(")\n");
1341 } else {
1342 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001343 if (!iptc_get_references(&refs, chain, handle))
1344 printf(" (ERROR obtaining refs)\n");
1345 else
1346 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 }
1348
1349 if (format & FMT_LINENUMBERS)
1350 printf(FMT("%-4s ", "%s "), "num");
1351 if (!(format & FMT_NOCOUNTS)) {
1352 if (format & FMT_KILOMEGAGIGA) {
1353 printf(FMT("%5s ","%s "), "pkts");
1354 printf(FMT("%5s ","%s "), "bytes");
1355 } else {
1356 printf(FMT("%8s ","%s "), "pkts");
1357 printf(FMT("%10s ","%s "), "bytes");
1358 }
1359 }
1360 if (!(format & FMT_NOTARGET))
1361 printf(FMT("%-9s ","%s "), "target");
1362 fputs(" prot ", stdout);
1363 if (format & FMT_OPTIONS)
1364 fputs("opt", stdout);
1365 if (format & FMT_VIA) {
1366 printf(FMT(" %-6s ","%s "), "in");
1367 printf(FMT("%-6s ","%s "), "out");
1368 }
1369 printf(FMT(" %-19s ","%s "), "source");
1370 printf(FMT(" %-19s "," %s "), "destination");
1371 printf("\n");
1372}
1373
Marc Bouchere6869a82000-03-20 06:03:29 +00001374
1375static int
1376print_match(const struct ipt_entry_match *m,
1377 const struct ipt_ip *ip,
1378 int numeric)
1379{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001380 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001381
1382 if (match) {
1383 if (match->print)
1384 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001385 else
Rusty Russellb039b022000-09-01 06:04:05 +00001386 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001388 if (m->u.user.name[0])
1389 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001390 }
1391 /* Don't stop iterating. */
1392 return 0;
1393}
1394
1395/* e is called `fw' here for hysterical raisins */
1396static void
1397print_firewall(const struct ipt_entry *fw,
1398 const char *targname,
1399 unsigned int num,
1400 unsigned int format,
1401 const iptc_handle_t handle)
1402{
1403 struct iptables_target *target = NULL;
1404 const struct ipt_entry_target *t;
1405 u_int8_t flags;
1406 char buf[BUFSIZ];
1407
Marc Bouchere6869a82000-03-20 06:03:29 +00001408 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001409 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001410 else
Rusty Russell52a51492000-05-02 16:44:29 +00001411 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001412
1413 t = ipt_get_target((struct ipt_entry *)fw);
1414 flags = fw->ip.flags;
1415
1416 if (format & FMT_LINENUMBERS)
1417 printf(FMT("%-4u ", "%u "), num+1);
1418
1419 if (!(format & FMT_NOCOUNTS)) {
1420 print_num(fw->counters.pcnt, format);
1421 print_num(fw->counters.bcnt, format);
1422 }
1423
1424 if (!(format & FMT_NOTARGET))
1425 printf(FMT("%-9s ", "%s "), targname);
1426
1427 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1428 {
Rusty Russell28381a42000-05-10 00:19:50 +00001429 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 if (pname)
1431 printf(FMT("%-5s", "%s "), pname);
1432 else
1433 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1434 }
1435
1436 if (format & FMT_OPTIONS) {
1437 if (format & FMT_NOTABLE)
1438 fputs("opt ", stdout);
1439 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1440 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1441 fputc(' ', stdout);
1442 }
1443
1444 if (format & FMT_VIA) {
1445 char iface[IFNAMSIZ+2];
1446
1447 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1448 iface[0] = '!';
1449 iface[1] = '\0';
1450 }
1451 else iface[0] = '\0';
1452
1453 if (fw->ip.iniface[0] != '\0') {
1454 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001455 }
1456 else if (format & FMT_NUMERIC) strcat(iface, "*");
1457 else strcat(iface, "any");
1458 printf(FMT(" %-6s ","in %s "), iface);
1459
1460 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1461 iface[0] = '!';
1462 iface[1] = '\0';
1463 }
1464 else iface[0] = '\0';
1465
1466 if (fw->ip.outiface[0] != '\0') {
1467 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001468 }
1469 else if (format & FMT_NUMERIC) strcat(iface, "*");
1470 else strcat(iface, "any");
1471 printf(FMT("%-6s ","out %s "), iface);
1472 }
1473
1474 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1475 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1476 printf(FMT("%-19s ","%s "), "anywhere");
1477 else {
1478 if (format & FMT_NUMERIC)
1479 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1480 else
1481 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1482 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1483 printf(FMT("%-19s ","%s "), buf);
1484 }
1485
1486 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1487 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001488 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001489 else {
1490 if (format & FMT_NUMERIC)
1491 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1492 else
1493 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1494 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001495 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001496 }
1497
1498 if (format & FMT_NOTABLE)
1499 fputs(" ", stdout);
1500
Harald Welte72bd87e2005-11-24 17:04:05 +00001501#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001502 if(fw->ip.flags & IPT_F_GOTO)
1503 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001504#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001505
Marc Bouchere6869a82000-03-20 06:03:29 +00001506 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1507
1508 if (target) {
1509 if (target->print)
1510 /* Print the target information. */
1511 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001512 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001513 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001514 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001515
1516 if (!(format & FMT_NONEWLINE))
1517 fputc('\n', stdout);
1518}
1519
1520static void
1521print_firewall_line(const struct ipt_entry *fw,
1522 const iptc_handle_t h)
1523{
1524 struct ipt_entry_target *t;
1525
1526 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001527 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001528}
1529
1530static int
1531append_entry(const ipt_chainlabel chain,
1532 struct ipt_entry *fw,
1533 unsigned int nsaddrs,
1534 const struct in_addr saddrs[],
1535 unsigned int ndaddrs,
1536 const struct in_addr daddrs[],
1537 int verbose,
1538 iptc_handle_t *handle)
1539{
1540 unsigned int i, j;
1541 int ret = 1;
1542
1543 for (i = 0; i < nsaddrs; i++) {
1544 fw->ip.src.s_addr = saddrs[i].s_addr;
1545 for (j = 0; j < ndaddrs; j++) {
1546 fw->ip.dst.s_addr = daddrs[j].s_addr;
1547 if (verbose)
1548 print_firewall_line(fw, *handle);
1549 ret &= iptc_append_entry(chain, fw, handle);
1550 }
1551 }
1552
1553 return ret;
1554}
1555
1556static int
1557replace_entry(const ipt_chainlabel chain,
1558 struct ipt_entry *fw,
1559 unsigned int rulenum,
1560 const struct in_addr *saddr,
1561 const struct in_addr *daddr,
1562 int verbose,
1563 iptc_handle_t *handle)
1564{
1565 fw->ip.src.s_addr = saddr->s_addr;
1566 fw->ip.dst.s_addr = daddr->s_addr;
1567
1568 if (verbose)
1569 print_firewall_line(fw, *handle);
1570 return iptc_replace_entry(chain, fw, rulenum, handle);
1571}
1572
1573static int
1574insert_entry(const ipt_chainlabel chain,
1575 struct ipt_entry *fw,
1576 unsigned int rulenum,
1577 unsigned int nsaddrs,
1578 const struct in_addr saddrs[],
1579 unsigned int ndaddrs,
1580 const struct in_addr daddrs[],
1581 int verbose,
1582 iptc_handle_t *handle)
1583{
1584 unsigned int i, j;
1585 int ret = 1;
1586
1587 for (i = 0; i < nsaddrs; i++) {
1588 fw->ip.src.s_addr = saddrs[i].s_addr;
1589 for (j = 0; j < ndaddrs; j++) {
1590 fw->ip.dst.s_addr = daddrs[j].s_addr;
1591 if (verbose)
1592 print_firewall_line(fw, *handle);
1593 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1594 }
1595 }
1596
1597 return ret;
1598}
1599
Rusty Russell2e0a3212000-04-19 11:23:18 +00001600static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001601make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001602{
1603 /* Establish mask for comparison */
1604 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001605 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001606 unsigned char *mask, *mptr;
1607
1608 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001609 for (matchp = matches; matchp; matchp = matchp->next)
1610 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001611
Rusty Russell9e1d2142000-04-23 09:11:12 +00001612 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001613 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001614 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001615
Rusty Russell9e1d2142000-04-23 09:11:12 +00001616 memset(mask, 0xFF, sizeof(struct ipt_entry));
1617 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001618
Martin Josefsson78cafda2004-02-02 20:01:18 +00001619 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001620 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001621 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001622 + matchp->match->userspacesize);
1623 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001624 }
1625
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001626 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001627 IPT_ALIGN(sizeof(struct ipt_entry_target))
1628 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001629
1630 return mask;
1631}
1632
Marc Bouchere6869a82000-03-20 06:03:29 +00001633static int
1634delete_entry(const ipt_chainlabel chain,
1635 struct ipt_entry *fw,
1636 unsigned int nsaddrs,
1637 const struct in_addr saddrs[],
1638 unsigned int ndaddrs,
1639 const struct in_addr daddrs[],
1640 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001641 iptc_handle_t *handle,
1642 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001643{
1644 unsigned int i, j;
1645 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001646 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001647
Martin Josefsson78cafda2004-02-02 20:01:18 +00001648 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001649 for (i = 0; i < nsaddrs; i++) {
1650 fw->ip.src.s_addr = saddrs[i].s_addr;
1651 for (j = 0; j < ndaddrs; j++) {
1652 fw->ip.dst.s_addr = daddrs[j].s_addr;
1653 if (verbose)
1654 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001655 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 }
1657 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001658 free(mask);
1659
Marc Bouchere6869a82000-03-20 06:03:29 +00001660 return ret;
1661}
1662
Harald Welteae1ff9f2000-12-01 14:26:20 +00001663int
Marc Bouchere6869a82000-03-20 06:03:29 +00001664for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001665 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001666{
1667 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001668 const char *chain;
1669 char *chains;
1670 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001671
Rusty Russell9e1d2142000-04-23 09:11:12 +00001672 chain = iptc_first_chain(handle);
1673 while (chain) {
1674 chaincount++;
1675 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 }
1677
Rusty Russell9e1d2142000-04-23 09:11:12 +00001678 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1679 i = 0;
1680 chain = iptc_first_chain(handle);
1681 while (chain) {
1682 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1683 i++;
1684 chain = iptc_next_chain(handle);
1685 }
1686
1687 for (i = 0; i < chaincount; i++) {
1688 if (!builtinstoo
1689 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001690 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001691 continue;
1692 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1693 }
1694
1695 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 return ret;
1697}
1698
Harald Welteae1ff9f2000-12-01 14:26:20 +00001699int
Marc Bouchere6869a82000-03-20 06:03:29 +00001700flush_entries(const ipt_chainlabel chain, int verbose,
1701 iptc_handle_t *handle)
1702{
1703 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001704 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001705
1706 if (verbose)
1707 fprintf(stdout, "Flushing chain `%s'\n", chain);
1708 return iptc_flush_entries(chain, handle);
1709}
Marc Bouchere6869a82000-03-20 06:03:29 +00001710
1711static int
1712zero_entries(const ipt_chainlabel chain, int verbose,
1713 iptc_handle_t *handle)
1714{
1715 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001716 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001717
Marc Bouchere6869a82000-03-20 06:03:29 +00001718 if (verbose)
1719 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1720 return iptc_zero_entries(chain, handle);
1721}
1722
Harald Welteae1ff9f2000-12-01 14:26:20 +00001723int
Marc Bouchere6869a82000-03-20 06:03:29 +00001724delete_chain(const ipt_chainlabel chain, int verbose,
1725 iptc_handle_t *handle)
1726{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001727 if (!chain)
1728 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001729
1730 if (verbose)
1731 fprintf(stdout, "Deleting chain `%s'\n", chain);
1732 return iptc_delete_chain(chain, handle);
1733}
1734
1735static int
1736list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1737 int expanded, int linenumbers, iptc_handle_t *handle)
1738{
1739 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001740 unsigned int format;
1741 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001742
1743 format = FMT_OPTIONS;
1744 if (!verbose)
1745 format |= FMT_NOCOUNTS;
1746 else
1747 format |= FMT_VIA;
1748
1749 if (numeric)
1750 format |= FMT_NUMERIC;
1751
1752 if (!expanded)
1753 format |= FMT_KILOMEGAGIGA;
1754
1755 if (linenumbers)
1756 format |= FMT_LINENUMBERS;
1757
Rusty Russell9e1d2142000-04-23 09:11:12 +00001758 for (this = iptc_first_chain(handle);
1759 this;
1760 this = iptc_next_chain(handle)) {
1761 const struct ipt_entry *i;
1762 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001763
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 if (chain && strcmp(chain, this) != 0)
1765 continue;
1766
1767 if (found) printf("\n");
1768
1769 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001770 i = iptc_first_rule(this, handle);
1771
1772 num = 0;
1773 while (i) {
1774 print_firewall(i,
1775 iptc_get_target(i, handle),
1776 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 format,
1778 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001779 i = iptc_next_rule(i, handle);
1780 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 found = 1;
1782 }
1783
1784 errno = ENOENT;
1785 return found;
1786}
1787
Harald Welte82dd2ec2000-12-19 05:18:15 +00001788static char *get_modprobe(void)
1789{
1790 int procfile;
1791 char *ret;
1792
Harald Welte10f7f142004-10-22 08:14:07 +00001793#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001794 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1795 if (procfile < 0)
1796 return NULL;
1797
Harald Welte10f7f142004-10-22 08:14:07 +00001798 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001799 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001800 memset(ret, 0, PROCFILE_BUFSIZ);
1801 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001802 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001803 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001804 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001805 if (ret[strlen(ret)-1]=='\n')
1806 ret[strlen(ret)-1]=0;
1807 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001808 return ret;
1809 }
1810 fail:
1811 free(ret);
1812 close(procfile);
1813 return NULL;
1814}
1815
Harald Welte58918652001-06-16 18:25:25 +00001816int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001817{
1818 char *buf = NULL;
1819 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001820 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001821
1822 /* If they don't explicitly set it, read out of kernel */
1823 if (!modprobe) {
1824 buf = get_modprobe();
1825 if (!buf)
1826 return -1;
1827 modprobe = buf;
1828 }
1829
1830 switch (fork()) {
1831 case 0:
1832 argv[0] = (char *)modprobe;
1833 argv[1] = (char *)modname;
1834 argv[2] = NULL;
1835 execv(argv[0], argv);
1836
1837 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001838 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001839 case -1:
1840 return -1;
1841
1842 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001843 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001844 }
1845
1846 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001847 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1848 return 0;
1849 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001850}
1851
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00001852int load_iptables_ko(const char *modprobe)
1853{
1854 static int loaded = 0;
1855 static int ret = -1;
1856
1857 if (!loaded) {
1858 ret = iptables_insmod("ip_tables", NULL);
1859 loaded = 1;
1860 }
1861
1862 return ret;
1863}
1864
Marc Bouchere6869a82000-03-20 06:03:29 +00001865static struct ipt_entry *
1866generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001867 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001868 struct ipt_entry_target *target)
1869{
1870 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001871 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001872 struct ipt_entry *e;
1873
1874 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001875 for (matchp = matches; matchp; matchp = matchp->next)
1876 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001877
Rusty Russell228e98d2000-04-27 10:28:06 +00001878 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 *e = *fw;
1880 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001881 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001882
1883 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001884 for (matchp = matches; matchp; matchp = matchp->next) {
1885 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1886 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001887 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001888 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001889
1890 return e;
1891}
1892
Martin Josefsson78cafda2004-02-02 20:01:18 +00001893void clear_rule_matches(struct iptables_rule_match **matches)
1894{
1895 struct iptables_rule_match *matchp, *tmp;
1896
1897 for (matchp = *matches; matchp;) {
1898 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001899 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001900 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001901 matchp->match->m = NULL;
1902 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001903 if (matchp->match == matchp->match->next) {
1904 free(matchp->match);
1905 matchp->match = NULL;
1906 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001907 free(matchp);
1908 matchp = tmp;
1909 }
1910
1911 *matches = NULL;
1912}
1913
Rusty Russell3aef54d2005-01-03 03:48:40 +00001914static void set_revision(char *name, u_int8_t revision)
1915{
1916 /* Old kernel sources don't have ".revision" field,
1917 but we stole a byte from name. */
1918 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1919 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1920}
1921
Phil Oester8cf65912005-09-19 15:00:33 +00001922void
1923get_kernel_version(void) {
1924 static struct utsname uts;
1925 int x = 0, y = 0, z = 0;
1926
1927 if (uname(&uts) == -1) {
1928 fprintf(stderr, "Unable to retrieve kernel version.\n");
1929 free_opts(1);
1930 exit(1);
1931 }
1932
1933 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1934 kernel_version = LINUX_VERSION(x, y, z);
1935}
1936
Marc Bouchere6869a82000-03-20 06:03:29 +00001937int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1938{
1939 struct ipt_entry fw, *e = NULL;
1940 int invert = 0;
1941 unsigned int nsaddrs = 0, ndaddrs = 0;
1942 struct in_addr *saddrs = NULL, *daddrs = NULL;
1943
1944 int c, verbose = 0;
1945 const char *chain = NULL;
1946 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1947 const char *policy = NULL, *newname = NULL;
1948 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001949 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001950 int ret = 1;
1951 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001952 struct iptables_rule_match *matches = NULL;
1953 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001954 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001955 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001956 const char *jumpto = "";
1957 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001958 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001959
1960 memset(&fw, 0, sizeof(fw));
1961
Harald Welteae1ff9f2000-12-01 14:26:20 +00001962 /* re-set optind to 0 in case do_command gets called
1963 * a second time */
1964 optind = 0;
1965
1966 /* clear mflags in case do_command gets called a second time
1967 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001968 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001969 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001970
1971 for (t = iptables_targets; t; t = t->next) {
1972 t->tflags = 0;
1973 t->used = 0;
1974 }
1975
Marc Bouchere6869a82000-03-20 06:03:29 +00001976 /* Suppress error messages: we may add new options if we
1977 demand-load a protocol. */
1978 opterr = 0;
1979
1980 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001981 "-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 +00001982 opts, NULL)) != -1) {
1983 switch (c) {
1984 /*
1985 * Command selection
1986 */
1987 case 'A':
1988 add_command(&command, CMD_APPEND, CMD_NONE,
1989 invert);
1990 chain = optarg;
1991 break;
1992
1993 case 'D':
1994 add_command(&command, CMD_DELETE, CMD_NONE,
1995 invert);
1996 chain = optarg;
1997 if (optind < argc && argv[optind][0] != '-'
1998 && argv[optind][0] != '!') {
1999 rulenum = parse_rulenumber(argv[optind++]);
2000 command = CMD_DELETE_NUM;
2001 }
2002 break;
2003
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 case 'R':
2005 add_command(&command, CMD_REPLACE, CMD_NONE,
2006 invert);
2007 chain = optarg;
2008 if (optind < argc && argv[optind][0] != '-'
2009 && argv[optind][0] != '!')
2010 rulenum = parse_rulenumber(argv[optind++]);
2011 else
2012 exit_error(PARAMETER_PROBLEM,
2013 "-%c requires a rule number",
2014 cmd2char(CMD_REPLACE));
2015 break;
2016
2017 case 'I':
2018 add_command(&command, CMD_INSERT, CMD_NONE,
2019 invert);
2020 chain = optarg;
2021 if (optind < argc && argv[optind][0] != '-'
2022 && argv[optind][0] != '!')
2023 rulenum = parse_rulenumber(argv[optind++]);
2024 else rulenum = 1;
2025 break;
2026
2027 case 'L':
2028 add_command(&command, CMD_LIST, CMD_ZERO,
2029 invert);
2030 if (optarg) chain = optarg;
2031 else if (optind < argc && argv[optind][0] != '-'
2032 && argv[optind][0] != '!')
2033 chain = argv[optind++];
2034 break;
2035
2036 case 'F':
2037 add_command(&command, CMD_FLUSH, CMD_NONE,
2038 invert);
2039 if (optarg) chain = optarg;
2040 else if (optind < argc && argv[optind][0] != '-'
2041 && argv[optind][0] != '!')
2042 chain = argv[optind++];
2043 break;
2044
2045 case 'Z':
2046 add_command(&command, CMD_ZERO, CMD_LIST,
2047 invert);
2048 if (optarg) chain = optarg;
2049 else if (optind < argc && argv[optind][0] != '-'
2050 && argv[optind][0] != '!')
2051 chain = argv[optind++];
2052 break;
2053
2054 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00002055 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00002056 exit_error(PARAMETER_PROBLEM,
2057 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00002058 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00002059 if (find_target(optarg, TRY_LOAD))
2060 exit_error(PARAMETER_PROBLEM,
2061 "chain name may not clash "
2062 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
2064 invert);
2065 chain = optarg;
2066 break;
2067
2068 case 'X':
2069 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
2070 invert);
2071 if (optarg) chain = optarg;
2072 else if (optind < argc && argv[optind][0] != '-'
2073 && argv[optind][0] != '!')
2074 chain = argv[optind++];
2075 break;
2076
2077 case 'E':
2078 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2079 invert);
2080 chain = optarg;
2081 if (optind < argc && argv[optind][0] != '-'
2082 && argv[optind][0] != '!')
2083 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002084 else
2085 exit_error(PARAMETER_PROBLEM,
2086 "-%c requires old-chain-name and "
2087 "new-chain-name",
2088 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00002089 break;
2090
2091 case 'P':
2092 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2093 invert);
2094 chain = optarg;
2095 if (optind < argc && argv[optind][0] != '-'
2096 && argv[optind][0] != '!')
2097 policy = argv[optind++];
2098 else
2099 exit_error(PARAMETER_PROBLEM,
2100 "-%c requires a chain and a policy",
2101 cmd2char(CMD_SET_POLICY));
2102 break;
2103
2104 case 'h':
2105 if (!optarg)
2106 optarg = argv[optind];
2107
Rusty Russell2e0a3212000-04-19 11:23:18 +00002108 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002109 if (!matches && protocol)
2110 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002111
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002112 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002113
2114 /*
2115 * Option selection
2116 */
2117 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002118 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002119 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2120 invert);
2121
2122 /* Canonicalize into lower case */
2123 for (protocol = argv[optind-1]; *protocol; protocol++)
2124 *protocol = tolower(*protocol);
2125
2126 protocol = argv[optind-1];
2127 fw.ip.proto = parse_protocol(protocol);
2128
2129 if (fw.ip.proto == 0
2130 && (fw.ip.invflags & IPT_INV_PROTO))
2131 exit_error(PARAMETER_PROBLEM,
2132 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002133 break;
2134
2135 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002136 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002137 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2138 invert);
2139 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002140 break;
2141
2142 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002143 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002144 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2145 invert);
2146 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002147 break;
2148
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002149#ifdef IPT_F_GOTO
2150 case 'g':
2151 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2152 invert);
2153 fw.ip.flags |= IPT_F_GOTO;
2154 jumpto = parse_target(optarg);
2155 break;
2156#endif
2157
Marc Bouchere6869a82000-03-20 06:03:29 +00002158 case 'j':
2159 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2160 invert);
2161 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002162 /* TRY_LOAD (may be chain name) */
2163 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002164
2165 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002166 size_t size;
2167
Rusty Russell73f72f52000-07-03 10:17:57 +00002168 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2169 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002170
Rusty Russell2e0a3212000-04-19 11:23:18 +00002171 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002172 target->t->u.target_size = size;
2173 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002174 set_revision(target->t->u.user.name,
2175 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002176 if (target->init != NULL)
2177 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002178 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002179 }
2180 break;
2181
2182
2183 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002184 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002185 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2186 invert);
2187 parse_interface(argv[optind-1],
2188 fw.ip.iniface,
2189 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002190 break;
2191
2192 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002193 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002194 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2195 invert);
2196 parse_interface(argv[optind-1],
2197 fw.ip.outiface,
2198 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002199 break;
2200
2201 case 'f':
2202 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2203 invert);
2204 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002205 break;
2206
2207 case 'v':
2208 if (!verbose)
2209 set_option(&options, OPT_VERBOSE,
2210 &fw.ip.invflags, invert);
2211 verbose++;
2212 break;
2213
Rusty Russell52a51492000-05-02 16:44:29 +00002214 case 'm': {
2215 size_t size;
2216
Marc Bouchere6869a82000-03-20 06:03:29 +00002217 if (invert)
2218 exit_error(PARAMETER_PROBLEM,
2219 "unexpected ! flag before --match");
2220
Martin Josefsson78cafda2004-02-02 20:01:18 +00002221 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002222 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2223 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002224 m->m = fw_calloc(1, size);
2225 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002226 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002227 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002228 if (m->init != NULL)
2229 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002230 if (m != m->next)
2231 /* Merge options for non-cloned matches */
2232 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002233 }
2234 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002235
2236 case 'n':
2237 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2238 invert);
2239 break;
2240
2241 case 't':
2242 if (invert)
2243 exit_error(PARAMETER_PROBLEM,
2244 "unexpected ! flag before --table");
2245 *table = argv[optind-1];
2246 break;
2247
2248 case 'x':
2249 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2250 invert);
2251 break;
2252
2253 case 'V':
2254 if (invert)
2255 printf("Not %s ;-)\n", program_version);
2256 else
2257 printf("%s v%s\n",
2258 program_name, program_version);
2259 exit(0);
2260
2261 case '0':
2262 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2263 invert);
2264 break;
2265
Harald Welte82dd2ec2000-12-19 05:18:15 +00002266 case 'M':
2267 modprobe = optarg;
2268 break;
2269
Harald Welteccd49e52001-01-23 22:54:34 +00002270 case 'c':
2271
2272 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2273 invert);
2274 pcnt = optarg;
2275 if (optind < argc && argv[optind][0] != '-'
2276 && argv[optind][0] != '!')
2277 bcnt = argv[optind++];
2278 else
2279 exit_error(PARAMETER_PROBLEM,
2280 "-%c requires packet and byte counter",
2281 opt2char(OPT_COUNTERS));
2282
Martin Josefssona28d4952004-05-26 16:04:48 +00002283 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002284 exit_error(PARAMETER_PROBLEM,
2285 "-%c packet counter not numeric",
2286 opt2char(OPT_COUNTERS));
2287
Martin Josefssona28d4952004-05-26 16:04:48 +00002288 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002289 exit_error(PARAMETER_PROBLEM,
2290 "-%c byte counter not numeric",
2291 opt2char(OPT_COUNTERS));
2292
2293 break;
2294
2295
Marc Bouchere6869a82000-03-20 06:03:29 +00002296 case 1: /* non option */
2297 if (optarg[0] == '!' && optarg[1] == '\0') {
2298 if (invert)
2299 exit_error(PARAMETER_PROBLEM,
2300 "multiple consecutive ! not"
2301 " allowed");
2302 invert = TRUE;
2303 optarg[0] = '\0';
2304 continue;
2305 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002306 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002307 exit_tryhelp(2);
2308
2309 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00002310 if (!target
2311 || !(target->parse(c - target->option_offset,
2312 argv, invert,
2313 &target->tflags,
2314 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002315 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002316 if (matchp->completed)
2317 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00002318 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002319 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002320 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002321 &fw,
2322 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002323 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002324 break;
2325 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002326 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002327
2328 /* If you listen carefully, you can
2329 actually hear this code suck. */
2330
2331 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002332 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00002333 * parameter, that has not been parsed yet,
2334 * it's not an option of an explicitly loaded
2335 * match or a target. However, we support
2336 * implicit loading of the protocol match
2337 * extension. '-p tcp' means 'l4 proto 6' and
2338 * at the same time 'load tcp protocol match on
2339 * demand if we specify --dport'.
2340 *
2341 * To make this work, we need to make sure:
2342 * - the parameter has not been parsed by
2343 * a match (m above)
2344 * - a protocol has been specified
2345 * - the protocol extension has not been
2346 * loaded yet, or is loaded and unused
2347 * [think of iptables-restore!]
2348 * - the protocol extension can be successively
2349 * loaded
2350 */
2351 if (m == NULL
2352 && protocol
2353 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002354 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002355 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002356 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002357 && (proto_used == 0))
2358 )
2359 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002360 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002361 /* Try loading protocol */
2362 size_t size;
2363
2364 proto_used = 1;
2365
2366 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2367 + m->size;
2368
2369 m->m = fw_calloc(1, size);
2370 m->m->u.match_size = size;
2371 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002372 set_revision(m->m->u.user.name,
2373 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002374 if (m->init != NULL)
2375 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002376
2377 opts = merge_options(opts,
2378 m->extra_opts, &m->option_offset);
2379
2380 optind--;
2381 continue;
2382 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002383 if (!m)
2384 exit_error(PARAMETER_PROBLEM,
2385 "Unknown arg `%s'",
2386 argv[optind-1]);
2387 }
2388 }
2389 invert = FALSE;
2390 }
2391
Martin Josefsson78cafda2004-02-02 20:01:18 +00002392 for (matchp = matches; matchp; matchp = matchp->next)
2393 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002394
Marc Bouchere6869a82000-03-20 06:03:29 +00002395 if (target)
2396 target->final_check(target->tflags);
2397
2398 /* Fix me: must put inverse options checking here --MN */
2399
2400 if (optind < argc)
2401 exit_error(PARAMETER_PROBLEM,
2402 "unknown arguments found on commandline");
2403 if (!command)
2404 exit_error(PARAMETER_PROBLEM, "no command specified");
2405 if (invert)
2406 exit_error(PARAMETER_PROBLEM,
2407 "nothing appropriate following !");
2408
Harald Welte6336bfd2002-05-07 14:41:43 +00002409 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002410 if (!(options & OPT_DESTINATION))
2411 dhostnetworkmask = "0.0.0.0/0";
2412 if (!(options & OPT_SOURCE))
2413 shostnetworkmask = "0.0.0.0/0";
2414 }
2415
2416 if (shostnetworkmask)
2417 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2418 &(fw.ip.smsk), &nsaddrs);
2419
2420 if (dhostnetworkmask)
2421 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2422 &(fw.ip.dmsk), &ndaddrs);
2423
2424 if ((nsaddrs > 1 || ndaddrs > 1) &&
2425 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2426 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2427 " source or destination IP addresses");
2428
Marc Bouchere6869a82000-03-20 06:03:29 +00002429 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2430 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2431 "specify a unique address");
2432
2433 generic_opt_check(command, options);
2434
2435 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2436 exit_error(PARAMETER_PROBLEM,
2437 "chain name `%s' too long (must be under %i chars)",
2438 chain, IPT_FUNCTION_MAXNAMELEN);
2439
Harald Welteae1ff9f2000-12-01 14:26:20 +00002440 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002441 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002442 *handle = iptc_init(*table);
2443
Rusty Russell8beb0492004-12-22 00:37:10 +00002444 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI740d7272006-11-13 05:09:16 +00002445 if (!*handle && load_iptables_ko(modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002446 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002447
Marc Bouchere6869a82000-03-20 06:03:29 +00002448 if (!*handle)
2449 exit_error(VERSION_PROBLEM,
2450 "can't initialize iptables table `%s': %s",
2451 *table, iptc_strerror(errno));
2452
Harald Welte6336bfd2002-05-07 14:41:43 +00002453 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002454 || command == CMD_DELETE
2455 || command == CMD_INSERT
2456 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002457 if (strcmp(chain, "PREROUTING") == 0
2458 || strcmp(chain, "INPUT") == 0) {
2459 /* -o not valid with incoming packets. */
2460 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002461 exit_error(PARAMETER_PROBLEM,
2462 "Can't use -%c with %s\n",
2463 opt2char(OPT_VIANAMEOUT),
2464 chain);
2465 }
2466
Rusty Russella4860fd2000-06-17 16:13:02 +00002467 if (strcmp(chain, "POSTROUTING") == 0
2468 || strcmp(chain, "OUTPUT") == 0) {
2469 /* -i not valid with outgoing packets */
2470 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002471 exit_error(PARAMETER_PROBLEM,
2472 "Can't use -%c with %s\n",
2473 opt2char(OPT_VIANAMEIN),
2474 chain);
2475 }
2476
2477 if (target && iptc_is_chain(jumpto, *handle)) {
2478 printf("Warning: using chain %s, not extension\n",
2479 jumpto);
2480
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002481 if (target->t)
2482 free(target->t);
2483
Marc Bouchere6869a82000-03-20 06:03:29 +00002484 target = NULL;
2485 }
2486
2487 /* If they didn't specify a target, or it's a chain
2488 name, use standard. */
2489 if (!target
2490 && (strlen(jumpto) == 0
2491 || iptc_is_chain(jumpto, *handle))) {
2492 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002493
Rusty Russell52a51492000-05-02 16:44:29 +00002494 target = find_target(IPT_STANDARD_TARGET,
2495 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002496
2497 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002498 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002499 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002500 target->t->u.target_size = size;
2501 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002502 if (!iptc_is_chain(jumpto, *handle))
2503 set_revision(target->t->u.user.name,
2504 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002505 if (target->init != NULL)
2506 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002507 }
2508
Rusty Russell7e53bf92000-03-20 07:03:28 +00002509 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002510 /* it is no chain, and we can't load a plugin.
2511 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002512 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002513 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002514#ifdef IPT_F_GOTO
2515 if (fw.ip.flags & IPT_F_GOTO)
2516 exit_error(PARAMETER_PROBLEM,
2517 "goto '%s' is not a chain\n", jumpto);
2518#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002519 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002520 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002521 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002522 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002523 }
2524 }
2525
2526 switch (command) {
2527 case CMD_APPEND:
2528 ret = append_entry(chain, e,
2529 nsaddrs, saddrs, ndaddrs, daddrs,
2530 options&OPT_VERBOSE,
2531 handle);
2532 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002533 case CMD_DELETE:
2534 ret = delete_entry(chain, e,
2535 nsaddrs, saddrs, ndaddrs, daddrs,
2536 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002537 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002538 break;
2539 case CMD_DELETE_NUM:
2540 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2541 break;
2542 case CMD_REPLACE:
2543 ret = replace_entry(chain, e, rulenum - 1,
2544 saddrs, daddrs, options&OPT_VERBOSE,
2545 handle);
2546 break;
2547 case CMD_INSERT:
2548 ret = insert_entry(chain, e, rulenum - 1,
2549 nsaddrs, saddrs, ndaddrs, daddrs,
2550 options&OPT_VERBOSE,
2551 handle);
2552 break;
2553 case CMD_LIST:
2554 ret = list_entries(chain,
2555 options&OPT_VERBOSE,
2556 options&OPT_NUMERIC,
2557 options&OPT_EXPANDED,
2558 options&OPT_LINENUMBERS,
2559 handle);
2560 break;
2561 case CMD_FLUSH:
2562 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2563 break;
2564 case CMD_ZERO:
2565 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2566 break;
2567 case CMD_LIST|CMD_ZERO:
2568 ret = list_entries(chain,
2569 options&OPT_VERBOSE,
2570 options&OPT_NUMERIC,
2571 options&OPT_EXPANDED,
2572 options&OPT_LINENUMBERS,
2573 handle);
2574 if (ret)
2575 ret = zero_entries(chain,
2576 options&OPT_VERBOSE, handle);
2577 break;
2578 case CMD_NEW_CHAIN:
2579 ret = iptc_create_chain(chain, handle);
2580 break;
2581 case CMD_DELETE_CHAIN:
2582 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2583 break;
2584 case CMD_RENAME_CHAIN:
2585 ret = iptc_rename_chain(chain, newname, handle);
2586 break;
2587 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002588 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002589 break;
2590 default:
2591 /* We should never reach this... */
2592 exit_tryhelp(2);
2593 }
2594
2595 if (verbose > 1)
2596 dump_entries(*handle);
2597
Martin Josefsson78cafda2004-02-02 20:01:18 +00002598 clear_rule_matches(&matches);
2599
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002600 if (e != NULL) {
2601 free(e);
2602 e = NULL;
2603 }
2604
keso6997cdf2004-07-04 15:20:53 +00002605 free(saddrs);
2606 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002607 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002608
Marc Bouchere6869a82000-03-20 06:03:29 +00002609 return ret;
2610}