blob: 47de8648809772d05540b23516f94d383bee8e70 [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>
Marc Bouchere6869a82000-03-20 06:03:29 +000034#include <ctype.h>
35#include <stdarg.h>
36#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000037#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000038#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000041#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050#define FMT_NUMERIC 0x0001
51#define FMT_NOCOUNTS 0x0002
52#define FMT_KILOMEGAGIGA 0x0004
53#define FMT_OPTIONS 0x0008
54#define FMT_NOTABLE 0x0010
55#define FMT_NOTARGET 0x0020
56#define FMT_VIA 0x0040
57#define FMT_NONEWLINE 0x0080
58#define FMT_LINENUMBERS 0x0100
59
60#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
61 | FMT_NUMERIC | FMT_NOTABLE)
62#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
63
64
65#define CMD_NONE 0x0000U
66#define CMD_INSERT 0x0001U
67#define CMD_DELETE 0x0002U
68#define CMD_DELETE_NUM 0x0004U
69#define CMD_REPLACE 0x0008U
70#define CMD_APPEND 0x0010U
71#define CMD_LIST 0x0020U
72#define CMD_FLUSH 0x0040U
73#define CMD_ZERO 0x0080U
74#define CMD_NEW_CHAIN 0x0100U
75#define CMD_DELETE_CHAIN 0x0200U
76#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000077#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000078#define NUMBER_OF_CMD 13
79static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000080 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000081
82#define OPTION_OFFSET 256
83
84#define OPT_NONE 0x00000U
85#define OPT_NUMERIC 0x00001U
86#define OPT_SOURCE 0x00002U
87#define OPT_DESTINATION 0x00004U
88#define OPT_PROTOCOL 0x00008U
89#define OPT_JUMP 0x00010U
90#define OPT_VERBOSE 0x00020U
91#define OPT_EXPANDED 0x00040U
92#define OPT_VIANAMEIN 0x00080U
93#define OPT_VIANAMEOUT 0x00100U
94#define OPT_FRAGMENT 0x00200U
95#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000096#define OPT_COUNTERS 0x00800U
97#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +000098static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000099= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000100
101static struct option original_opts[] = {
102 { "append", 1, 0, 'A' },
103 { "delete", 1, 0, 'D' },
104 { "insert", 1, 0, 'I' },
105 { "replace", 1, 0, 'R' },
106 { "list", 2, 0, 'L' },
107 { "flush", 2, 0, 'F' },
108 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000109 { "new-chain", 1, 0, 'N' },
110 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000111 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000112 { "policy", 1, 0, 'P' },
113 { "source", 1, 0, 's' },
114 { "destination", 1, 0, 'd' },
115 { "src", 1, 0, 's' }, /* synonym */
116 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000117 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "in-interface", 1, 0, 'i' },
119 { "jump", 1, 0, 'j' },
120 { "table", 1, 0, 't' },
121 { "match", 1, 0, 'm' },
122 { "numeric", 0, 0, 'n' },
123 { "out-interface", 1, 0, 'o' },
124 { "verbose", 0, 0, 'v' },
125 { "exact", 0, 0, 'x' },
126 { "fragments", 0, 0, 'f' },
127 { "version", 0, 0, 'V' },
128 { "help", 2, 0, 'h' },
129 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000130 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000131 { "set-counters", 1, 0, 'c' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000132 { "goto", 1, 0, 'g' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 { 0 }
134};
135
Illes Marci63e90632003-03-03 08:08:37 +0000136/* we need this for iptables-restore. iptables-restore.c sets line to the
137 * current line of the input file, in order to give a more precise error
138 * message. iptables itself doesn't need this, so it is initialized to the
139 * magic number of -1 */
140int line = -1;
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142static struct option *opts = original_opts;
143static unsigned int global_option_offset = 0;
144
145/* Table of legal combinations of commands and options. If any of the
146 * given commands make an option legal, that option is legal (applies to
147 * CMD_LIST and CMD_ZERO only).
148 * Key:
149 * + compulsory
150 * x illegal
151 * optional
152 */
153
154static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
155/* Well, it's better than "Re: Linux vs FreeBSD" */
156{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157 /* -n -s -d -p -j -v -x -i -o -f --line -c */
158/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
159/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
160/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
161/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
162/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
163/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
164/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
165/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000170};
171
172static int inverse_for_options[NUMBER_OF_OPT] =
173{
174/* -n */ 0,
175/* -s */ IPT_INV_SRCIP,
176/* -d */ IPT_INV_DSTIP,
177/* -p */ IPT_INV_PROTO,
178/* -j */ 0,
179/* -v */ 0,
180/* -x */ 0,
181/* -i */ IPT_INV_VIA_IN,
182/* -o */ IPT_INV_VIA_OUT,
183/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000184/*--line*/ 0,
185/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000186};
187
188const char *program_version;
189const char *program_name;
190
Phil Oester8cf65912005-09-19 15:00:33 +0000191int kernel_version;
192
Marc Bouchere6869a82000-03-20 06:03:29 +0000193extern void dump_entries(const iptc_handle_t handle);
194
195/* A few hardcoded protocols for 'all' and in case the user has no
196 /etc/protocols */
197struct pprot {
198 char *name;
199 u_int8_t num;
200};
201
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000202struct afinfo afinfo = {
203 .family = AF_INET,
204 .libprefix = "libipt_",
205 .ipproto = IPPROTO_IP,
206 .kmod = "ip_tables",
207 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
208 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
209};
210
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000211/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000212/* defined in netinet/in.h */
213#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214#ifndef IPPROTO_ESP
215#define IPPROTO_ESP 50
216#endif
217#ifndef IPPROTO_AH
218#define IPPROTO_AH 51
219#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000220#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000221
Marc Bouchere6869a82000-03-20 06:03:29 +0000222static const struct pprot chain_protos[] = {
223 { "tcp", IPPROTO_TCP },
224 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000225 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000227 { "esp", IPPROTO_ESP },
228 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000229 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000230 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231};
232
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000233static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000234proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000235{
236 unsigned int i;
237
Rusty Russell28381a42000-05-10 00:19:50 +0000238 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
Phil Oester58179b12006-07-20 17:00:19 +0000251int
252service_to_port(const char *name, const char *proto)
253{
254 struct servent *service;
255
256 if ((service = getservbyname(name, proto)) != NULL)
257 return ntohs((unsigned short) service->s_port);
258
259 return -1;
260}
261
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000262u_int16_t
263parse_port(const char *port, const char *proto)
264{
265 unsigned int portnum;
266
267 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
268 (portnum = service_to_port(port, proto)) != -1)
269 return (u_int16_t)portnum;
270
271 exit_error(PARAMETER_PROBLEM,
272 "invalid port/service `%s' specified", port);
273}
274
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000275enum {
276 IPT_DOTTED_ADDR = 0,
277 IPT_DOTTED_MASK
278};
279
280static struct in_addr *
281__dotted_to_addr(const char *dotted, int type)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282{
283 static struct in_addr addr;
284 unsigned char *addrp;
285 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000286 unsigned int onebyte;
287 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000288 char buf[20];
289
290 /* copy dotted string, because we need to modify it */
291 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000292 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000293 addrp = (unsigned char *) &(addr.s_addr);
294
295 p = buf;
296 for (i = 0; i < 3; i++) {
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000297 if ((q = strchr(p, '.')) == NULL) {
298 if (type == IPT_DOTTED_ADDR) {
299 /* autocomplete, this is a network address */
300 if (string_to_number(p, 0, 255, &onebyte) == -1)
301 return (struct in_addr *) NULL;
302
303 addrp[i] = (unsigned char) onebyte;
304 while (i < 3)
305 addrp[++i] = 0;
306
307 return &addr;
308 } else
309 return (struct in_addr *) NULL;
310 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000311
312 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000313 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000314 return (struct in_addr *) NULL;
315
316 addrp[i] = (unsigned char) onebyte;
317 p = q + 1;
318 }
319
320 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000321 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000322 return (struct in_addr *) NULL;
323
324 addrp[3] = (unsigned char) onebyte;
325
326 return &addr;
327}
328
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000329struct in_addr *
330dotted_to_addr(const char *dotted)
331{
332 return __dotted_to_addr(dotted, IPT_DOTTED_ADDR);
333}
334
335struct in_addr *
336dotted_to_mask(const char *dotted)
337{
338 return __dotted_to_addr(dotted, IPT_DOTTED_MASK);
339}
340
Marc Bouchere6869a82000-03-20 06:03:29 +0000341static struct in_addr *
342network_to_addr(const char *name)
343{
344 struct netent *net;
345 static struct in_addr addr;
346
347 if ((net = getnetbyname(name)) != NULL) {
348 if (net->n_addrtype != AF_INET)
349 return (struct in_addr *) NULL;
350 addr.s_addr = htonl((unsigned long) net->n_net);
351 return &addr;
352 }
353
354 return (struct in_addr *) NULL;
355}
356
357static void
358inaddrcpy(struct in_addr *dst, struct in_addr *src)
359{
360 /* memcpy(dst, src, sizeof(struct in_addr)); */
361 dst->s_addr = src->s_addr;
362}
363
Pablo Neiradfdcd642005-05-29 19:05:23 +0000364static void free_opts(int reset_offset)
365{
366 if (opts != original_opts) {
367 free(opts);
368 opts = original_opts;
369 if (reset_offset)
370 global_option_offset = 0;
371 }
372}
373
Marc Bouchere6869a82000-03-20 06:03:29 +0000374void
375exit_error(enum exittype status, char *msg, ...)
376{
377 va_list args;
378
379 va_start(args, msg);
380 fprintf(stderr, "%s v%s: ", program_name, program_version);
381 vfprintf(stderr, msg, args);
382 va_end(args);
383 fprintf(stderr, "\n");
384 if (status == PARAMETER_PROBLEM)
385 exit_tryhelp(status);
386 if (status == VERSION_PROBLEM)
387 fprintf(stderr,
388 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000389 /* On error paths, make sure that we don't leak memory */
390 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000391 exit(status);
392}
393
394void
395exit_tryhelp(int status)
396{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000397 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000398 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000399 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
400 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000401 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 exit(status);
403}
404
405void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000406exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000407{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000408 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000409 struct iptables_target *t = NULL;
410
Marc Bouchere6869a82000-03-20 06:03:29 +0000411 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000412"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000413" %s -[RI] chain rulenum rule-specification [options]\n"
414" %s -D chain rulenum [options]\n"
415" %s -[LFZ] [chain] [options]\n"
416" %s -[NX] chain\n"
417" %s -E old-chain-name new-chain-name\n"
418" %s -P chain target [options]\n"
419" %s -h (print this help information)\n\n",
420 program_name, program_version, program_name, program_name,
421 program_name, program_name, program_name, program_name,
422 program_name, program_name);
423
424 printf(
425"Commands:\n"
426"Either long or short options are allowed.\n"
427" --append -A chain Append to chain\n"
428" --delete -D chain Delete matching rule from chain\n"
429" --delete -D chain rulenum\n"
430" Delete rule rulenum (1 = first) from chain\n"
431" --insert -I chain [rulenum]\n"
432" Insert in chain as rulenum (default 1=first)\n"
433" --replace -R chain rulenum\n"
434" Replace rule rulenum (1 = first) in chain\n"
435" --list -L [chain] List the rules in a chain or all chains\n"
436" --flush -F [chain] Delete all rules in chain or all chains\n"
437" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000438" --new -N chain Create a new user-defined chain\n"
439" --delete-chain\n"
440" -X [chain] Delete a user-defined chain\n"
441" --policy -P chain target\n"
442" Change policy on chain to target\n"
443" --rename-chain\n"
444" -E old-chain new-chain\n"
445" Change chain name, (moving any references)\n"
446
447"Options:\n"
448" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
449" --source -s [!] address[/mask]\n"
450" source specification\n"
451" --destination -d [!] address[/mask]\n"
452" destination specification\n"
453" --in-interface -i [!] input name[+]\n"
454" network interface name ([+] for wildcard)\n"
455" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000456" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000457#ifdef IPT_F_GOTO
458" --goto -g chain\n"
459" jump to chain with no return\n"
460#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000461" --match -m match\n"
462" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000463" --numeric -n numeric output of addresses and ports\n"
464" --out-interface -o [!] output name[+]\n"
465" network interface name ([+] for wildcard)\n"
466" --table -t table table to manipulate (default: `filter')\n"
467" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000468" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000469" --exact -x expand numbers (display exact values)\n"
470"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000471" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000472" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000473"[!] --version -V print package version.\n");
474
Rusty Russell363112d2000-08-11 13:49:26 +0000475 /* Print out any special helps. A user might like to be able
476 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000477 results. So we call help for all specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000478 for (t = xtables_targets; t ;t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000479 if (t->used) {
480 printf("\n");
481 t->help();
482 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000483 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000484 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000485 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000486 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000487 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000488 exit(0);
489}
490
491static void
492generic_opt_check(int command, int options)
493{
494 int i, j, legal = 0;
495
496 /* Check that commands are valid with options. Complicated by the
497 * fact that if an option is legal with *any* command given, it is
498 * legal overall (ie. -z and -l).
499 */
500 for (i = 0; i < NUMBER_OF_OPT; i++) {
501 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
502
503 for (j = 0; j < NUMBER_OF_CMD; j++) {
504 if (!(command & (1<<j)))
505 continue;
506
507 if (!(options & (1<<i))) {
508 if (commands_v_options[j][i] == '+')
509 exit_error(PARAMETER_PROBLEM,
510 "You need to supply the `-%c' "
511 "option for this command\n",
512 optflags[i]);
513 } else {
514 if (commands_v_options[j][i] != 'x')
515 legal = 1;
516 else if (legal == 0)
517 legal = -1;
518 }
519 }
520 if (legal == -1)
521 exit_error(PARAMETER_PROBLEM,
522 "Illegal option `-%c' with this command\n",
523 optflags[i]);
524 }
525}
526
527static char
528opt2char(int option)
529{
530 const char *ptr;
531 for (ptr = optflags; option > 1; option >>= 1, ptr++);
532
533 return *ptr;
534}
535
536static char
537cmd2char(int option)
538{
539 const char *ptr;
540 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
541
542 return *ptr;
543}
544
545static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000546add_command(unsigned int *cmd, const int newcmd, const int othercmds,
547 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000548{
549 if (invert)
550 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
551 if (*cmd & (~othercmds))
552 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
553 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
554 *cmd |= newcmd;
555}
556
557int
Harald Welteb77f1da2002-03-14 11:35:58 +0000558check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000559{
560 if (option && strcmp(option, "!") == 0) {
561 if (*invert)
562 exit_error(PARAMETER_PROBLEM,
563 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000564 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000565 if (optind) {
566 *optind = *optind+1;
567 if (argc && *optind > argc)
568 exit_error(PARAMETER_PROBLEM,
569 "no argument following `!'");
570 }
571
Marc Bouchere6869a82000-03-20 06:03:29 +0000572 return TRUE;
573 }
574 return FALSE;
575}
576
Marc Bouchere6869a82000-03-20 06:03:29 +0000577static struct in_addr *
578host_to_addr(const char *name, unsigned int *naddr)
579{
580 struct hostent *host;
581 struct in_addr *addr;
582 unsigned int i;
583
584 *naddr = 0;
585 if ((host = gethostbyname(name)) != NULL) {
586 if (host->h_addrtype != AF_INET ||
587 host->h_length != sizeof(struct in_addr))
588 return (struct in_addr *) NULL;
589
590 while (host->h_addr_list[*naddr] != (char *) NULL)
591 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000592 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000593 for (i = 0; i < *naddr; i++)
594 inaddrcpy(&(addr[i]),
595 (struct in_addr *) host->h_addr_list[i]);
596 return addr;
597 }
598
599 return (struct in_addr *) NULL;
600}
601
602static char *
603addr_to_host(const struct in_addr *addr)
604{
605 struct hostent *host;
606
607 if ((host = gethostbyaddr((char *) addr,
608 sizeof(struct in_addr), AF_INET)) != NULL)
609 return (char *) host->h_name;
610
611 return (char *) NULL;
612}
613
614/*
615 * All functions starting with "parse" should succeed, otherwise
616 * the program fails.
617 * Most routines return pointers to static data that may change
618 * between calls to the same or other routines with a few exceptions:
619 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
620 * return global static data.
621*/
622
623static struct in_addr *
624parse_hostnetwork(const char *name, unsigned int *naddrs)
625{
626 struct in_addr *addrp, *addrptmp;
627
628 if ((addrptmp = dotted_to_addr(name)) != NULL ||
629 (addrptmp = network_to_addr(name)) != NULL) {
630 addrp = fw_malloc(sizeof(struct in_addr));
631 inaddrcpy(addrp, addrptmp);
632 *naddrs = 1;
633 return addrp;
634 }
635 if ((addrp = host_to_addr(name, naddrs)) != NULL)
636 return addrp;
637
638 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
639}
640
641static struct in_addr *
642parse_mask(char *mask)
643{
644 static struct in_addr maskaddr;
645 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000646 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000647
648 if (mask == NULL) {
649 /* no mask at all defaults to 32 bits */
650 maskaddr.s_addr = 0xFFFFFFFF;
651 return &maskaddr;
652 }
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000653 if ((addrp = dotted_to_mask(mask)) != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +0000654 /* dotted_to_addr already returns a network byte order addr */
655 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000656 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000657 exit_error(PARAMETER_PROBLEM,
658 "invalid mask `%s' specified", mask);
659 if (bits != 0) {
660 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
661 return &maskaddr;
662 }
663
664 maskaddr.s_addr = 0L;
665 return &maskaddr;
666}
667
Marc Boucherb93c7982001-12-06 14:50:19 +0000668void
Marc Bouchere6869a82000-03-20 06:03:29 +0000669parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
670 struct in_addr *maskp, unsigned int *naddrs)
671{
672 struct in_addr *addrp;
673 char buf[256];
674 char *p;
675 int i, j, k, n;
676
677 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000678 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000679 if ((p = strrchr(buf, '/')) != NULL) {
680 *p = '\0';
681 addrp = parse_mask(p + 1);
682 } else
683 addrp = parse_mask(NULL);
684 inaddrcpy(maskp, addrp);
685
686 /* if a null mask is given, the name is ignored, like in "any/0" */
687 if (maskp->s_addr == 0L)
688 strcpy(buf, "0.0.0.0");
689
690 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
691 n = *naddrs;
692 for (i = 0, j = 0; i < n; i++) {
693 addrp[j++].s_addr &= maskp->s_addr;
694 for (k = 0; k < j - 1; k++) {
695 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
696 (*naddrs)--;
697 j--;
698 break;
699 }
700 }
701 }
702}
703
Rusty Russell28381a42000-05-10 00:19:50 +0000704/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
705static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000706find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000707{
Harald Welteed498492001-07-23 01:24:22 +0000708 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000709
Harald Welte0b0013a2002-02-18 16:15:31 +0000710 if (string_to_number(pname, 0, 255, &proto) != -1) {
711 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000712
Harald Welte0b0013a2002-02-18 16:15:31 +0000713 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000714 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000715 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000716 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000717
718 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000719}
720
Marc Boucherb93c7982001-12-06 14:50:19 +0000721u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000722parse_protocol(const char *s)
723{
Harald Welteed498492001-07-23 01:24:22 +0000724 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000725
Harald Welteed498492001-07-23 01:24:22 +0000726 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000727 struct protoent *pent;
728
Harald Weltecbe1ec72006-02-11 09:50:11 +0000729 /* first deal with the special case of 'all' to prevent
730 * people from being able to redefine 'all' in nsswitch
731 * and/or provoke expensive [not working] ldap/nis/...
732 * lookups */
733 if (!strcmp(s, "all"))
734 return 0;
735
Marc Bouchere6869a82000-03-20 06:03:29 +0000736 if ((pent = getprotobyname(s)))
737 proto = pent->p_proto;
738 else {
739 unsigned int i;
740 for (i = 0;
741 i < sizeof(chain_protos)/sizeof(struct pprot);
742 i++) {
743 if (strcmp(s, chain_protos[i].name) == 0) {
744 proto = chain_protos[i].num;
745 break;
746 }
747 }
748 if (i == sizeof(chain_protos)/sizeof(struct pprot))
749 exit_error(PARAMETER_PROBLEM,
750 "unknown protocol `%s' specified",
751 s);
752 }
753 }
754
755 return (u_int16_t)proto;
756}
757
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000758void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000759{
760 int vialen = strlen(arg);
761 unsigned int i;
762
763 memset(mask, 0, IFNAMSIZ);
764 memset(vianame, 0, IFNAMSIZ);
765
766 if (vialen + 1 > IFNAMSIZ)
767 exit_error(PARAMETER_PROBLEM,
768 "interface name `%s' must be shorter than IFNAMSIZ"
769 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000770
Marc Bouchere6869a82000-03-20 06:03:29 +0000771 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000772 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 memset(mask, 0, IFNAMSIZ);
774 else if (vianame[vialen - 1] == '+') {
775 memset(mask, 0xFF, vialen - 1);
776 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000777 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000778 } else {
779 /* Include nul-terminator in match */
780 memset(mask, 0xFF, vialen + 1);
781 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000782 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000783 if (vianame[i] == ':' ||
784 vianame[i] == '!' ||
785 vianame[i] == '*') {
Patrick McHardy42bd67b2006-10-11 07:37:26 +0000786 printf("Warning: weird character in interface"
Harald Weltede1578f2001-05-23 23:07:33 +0000787 " `%s' (No aliases, :, ! or *).\n",
788 vianame);
789 break;
790 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000791 }
792 }
793}
794
795/* Can't be zero. */
796static int
797parse_rulenumber(const char *rule)
798{
Harald Welteed498492001-07-23 01:24:22 +0000799 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000800
Harald Welteed498492001-07-23 01:24:22 +0000801 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 exit_error(PARAMETER_PROBLEM,
803 "Invalid rule number `%s'", rule);
804
805 return rulenum;
806}
807
808static const char *
809parse_target(const char *targetname)
810{
811 const char *ptr;
812
813 if (strlen(targetname) < 1)
814 exit_error(PARAMETER_PROBLEM,
815 "Invalid target name (too short)");
816
817 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
818 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000819 "Invalid target name `%s' (%u chars max)",
820 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000821
822 for (ptr = targetname; *ptr; ptr++)
823 if (isspace(*ptr))
824 exit_error(PARAMETER_PROBLEM,
825 "Invalid target name `%s'", targetname);
826 return targetname;
827}
828
829static char *
830addr_to_network(const struct in_addr *addr)
831{
832 struct netent *net;
833
834 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
835 return (char *) net->n_name;
836
837 return (char *) NULL;
838}
839
840char *
841addr_to_dotted(const struct in_addr *addrp)
842{
843 static char buf[20];
844 const unsigned char *bytep;
845
846 bytep = (const unsigned char *) &(addrp->s_addr);
847 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
848 return buf;
849}
Marc Boucherb93c7982001-12-06 14:50:19 +0000850
851char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000852addr_to_anyname(const struct in_addr *addr)
853{
854 char *name;
855
856 if ((name = addr_to_host(addr)) != NULL ||
857 (name = addr_to_network(addr)) != NULL)
858 return name;
859
860 return addr_to_dotted(addr);
861}
862
Marc Boucherb93c7982001-12-06 14:50:19 +0000863char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000864mask_to_dotted(const struct in_addr *mask)
865{
866 int i;
867 static char buf[20];
868 u_int32_t maskaddr, bits;
869
870 maskaddr = ntohl(mask->s_addr);
871
872 if (maskaddr == 0xFFFFFFFFL)
873 /* we don't want to see "/32" */
874 return "";
875
876 i = 32;
877 bits = 0xFFFFFFFEL;
878 while (--i >= 0 && maskaddr != bits)
879 bits <<= 1;
880 if (i >= 0)
881 sprintf(buf, "/%d", i);
882 else
883 /* mask was not a decent combination of 1's and 0's */
884 sprintf(buf, "/%s", addr_to_dotted(mask));
885
886 return buf;
887}
888
889int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000890string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
891 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000892{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000893 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 char *end;
895
896 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000897 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000898 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000899 if (*end == '\0' && end != s) {
900 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000901 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000902 *ret = number;
903 return 0;
904 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000905 }
906 return -1;
907}
908
Martin Josefssonb105bc92004-05-26 15:54:49 +0000909int
910string_to_number_l(const char *s, unsigned long min, unsigned long max,
911 unsigned long *ret)
912{
913 int result;
914 unsigned long long number;
915
916 result = string_to_number_ll(s, min, max, &number);
917 *ret = (unsigned long)number;
918
919 return result;
920}
921
922int string_to_number(const char *s, unsigned int min, unsigned int max,
923 unsigned int *ret)
924{
925 int result;
926 unsigned long number;
927
928 result = string_to_number_l(s, min, max, &number);
929 *ret = (unsigned int)number;
930
931 return result;
932}
933
Marc Bouchere6869a82000-03-20 06:03:29 +0000934static void
935set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
936 int invert)
937{
938 if (*options & option)
939 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
940 opt2char(option));
941 *options |= option;
942
943 if (invert) {
944 unsigned int i;
945 for (i = 0; 1 << i != option; i++);
946
947 if (!inverse_for_options[i])
948 exit_error(PARAMETER_PROBLEM,
949 "cannot have ! before -%c",
950 opt2char(option));
951 *invflg |= inverse_for_options[i];
952 }
953}
954
Marc Bouchere6869a82000-03-20 06:03:29 +0000955static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000956merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000957 unsigned int *option_offset)
958{
959 unsigned int num_old, num_new, i;
960 struct option *merge;
961
962 for (num_old = 0; oldopts[num_old].name; num_old++);
963 for (num_new = 0; newopts[num_new].name; num_new++);
964
965 global_option_offset += OPTION_OFFSET;
966 *option_offset = global_option_offset;
967
968 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
969 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000970 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000971 for (i = 0; i < num_new; i++) {
972 merge[num_old + i] = newopts[i];
973 merge[num_old + i].val += *option_offset;
974 }
975 memset(merge + num_old + num_new, 0, sizeof(struct option));
976
977 return merge;
978}
979
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000980void register_match(struct iptables_match *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000981{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000982 me->family = PF_INET;
983 xtables_register_match(me);
Rusty Russell3aef54d2005-01-03 03:48:40 +0000984}
985
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000986void register_target(struct iptables_target *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000987{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000988 me->family = PF_INET;
989 xtables_register_target(me);
Marc Bouchere6869a82000-03-20 06:03:29 +0000990}
991
992static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000993print_num(u_int64_t number, unsigned int format)
994{
995 if (format & FMT_KILOMEGAGIGA) {
996 if (number > 99999) {
997 number = (number + 500) / 1000;
998 if (number > 9999) {
999 number = (number + 500) / 1000;
1000 if (number > 9999) {
1001 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001002 if (number > 9999) {
1003 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001004 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001005 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001006 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001007 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001008 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001009 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001010 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001011 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001012 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001013 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001014 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001015}
1016
1017
1018static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001019print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1020{
1021 struct ipt_counters counters;
1022 const char *pol = iptc_get_policy(chain, &counters, handle);
1023 printf("Chain %s", chain);
1024 if (pol) {
1025 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001026 if (!(format & FMT_NOCOUNTS)) {
1027 fputc(' ', stdout);
1028 print_num(counters.pcnt, (format|FMT_NOTABLE));
1029 fputs("packets, ", stdout);
1030 print_num(counters.bcnt, (format|FMT_NOTABLE));
1031 fputs("bytes", stdout);
1032 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001033 printf(")\n");
1034 } else {
1035 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001036 if (!iptc_get_references(&refs, chain, handle))
1037 printf(" (ERROR obtaining refs)\n");
1038 else
1039 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001040 }
1041
1042 if (format & FMT_LINENUMBERS)
1043 printf(FMT("%-4s ", "%s "), "num");
1044 if (!(format & FMT_NOCOUNTS)) {
1045 if (format & FMT_KILOMEGAGIGA) {
1046 printf(FMT("%5s ","%s "), "pkts");
1047 printf(FMT("%5s ","%s "), "bytes");
1048 } else {
1049 printf(FMT("%8s ","%s "), "pkts");
1050 printf(FMT("%10s ","%s "), "bytes");
1051 }
1052 }
1053 if (!(format & FMT_NOTARGET))
1054 printf(FMT("%-9s ","%s "), "target");
1055 fputs(" prot ", stdout);
1056 if (format & FMT_OPTIONS)
1057 fputs("opt", stdout);
1058 if (format & FMT_VIA) {
1059 printf(FMT(" %-6s ","%s "), "in");
1060 printf(FMT("%-6s ","%s "), "out");
1061 }
1062 printf(FMT(" %-19s ","%s "), "source");
1063 printf(FMT(" %-19s "," %s "), "destination");
1064 printf("\n");
1065}
1066
Marc Bouchere6869a82000-03-20 06:03:29 +00001067
1068static int
1069print_match(const struct ipt_entry_match *m,
1070 const struct ipt_ip *ip,
1071 int numeric)
1072{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001073 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001074
1075 if (match) {
1076 if (match->print)
1077 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001078 else
Rusty Russellb039b022000-09-01 06:04:05 +00001079 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001080 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001081 if (m->u.user.name[0])
1082 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001083 }
1084 /* Don't stop iterating. */
1085 return 0;
1086}
1087
1088/* e is called `fw' here for hysterical raisins */
1089static void
1090print_firewall(const struct ipt_entry *fw,
1091 const char *targname,
1092 unsigned int num,
1093 unsigned int format,
1094 const iptc_handle_t handle)
1095{
1096 struct iptables_target *target = NULL;
1097 const struct ipt_entry_target *t;
1098 u_int8_t flags;
1099 char buf[BUFSIZ];
1100
Marc Bouchere6869a82000-03-20 06:03:29 +00001101 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001102 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001103 else
Rusty Russell52a51492000-05-02 16:44:29 +00001104 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001105
1106 t = ipt_get_target((struct ipt_entry *)fw);
1107 flags = fw->ip.flags;
1108
1109 if (format & FMT_LINENUMBERS)
1110 printf(FMT("%-4u ", "%u "), num+1);
1111
1112 if (!(format & FMT_NOCOUNTS)) {
1113 print_num(fw->counters.pcnt, format);
1114 print_num(fw->counters.bcnt, format);
1115 }
1116
1117 if (!(format & FMT_NOTARGET))
1118 printf(FMT("%-9s ", "%s "), targname);
1119
1120 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1121 {
Rusty Russell28381a42000-05-10 00:19:50 +00001122 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001123 if (pname)
1124 printf(FMT("%-5s", "%s "), pname);
1125 else
1126 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1127 }
1128
1129 if (format & FMT_OPTIONS) {
1130 if (format & FMT_NOTABLE)
1131 fputs("opt ", stdout);
1132 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1133 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1134 fputc(' ', stdout);
1135 }
1136
1137 if (format & FMT_VIA) {
1138 char iface[IFNAMSIZ+2];
1139
1140 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1141 iface[0] = '!';
1142 iface[1] = '\0';
1143 }
1144 else iface[0] = '\0';
1145
1146 if (fw->ip.iniface[0] != '\0') {
1147 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001148 }
1149 else if (format & FMT_NUMERIC) strcat(iface, "*");
1150 else strcat(iface, "any");
1151 printf(FMT(" %-6s ","in %s "), iface);
1152
1153 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1154 iface[0] = '!';
1155 iface[1] = '\0';
1156 }
1157 else iface[0] = '\0';
1158
1159 if (fw->ip.outiface[0] != '\0') {
1160 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001161 }
1162 else if (format & FMT_NUMERIC) strcat(iface, "*");
1163 else strcat(iface, "any");
1164 printf(FMT("%-6s ","out %s "), iface);
1165 }
1166
1167 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1168 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1169 printf(FMT("%-19s ","%s "), "anywhere");
1170 else {
1171 if (format & FMT_NUMERIC)
1172 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1173 else
1174 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1175 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1176 printf(FMT("%-19s ","%s "), buf);
1177 }
1178
1179 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1180 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001181 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 else {
1183 if (format & FMT_NUMERIC)
1184 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1185 else
1186 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1187 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001188 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001189 }
1190
1191 if (format & FMT_NOTABLE)
1192 fputs(" ", stdout);
1193
Harald Welte72bd87e2005-11-24 17:04:05 +00001194#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001195 if(fw->ip.flags & IPT_F_GOTO)
1196 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001197#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001198
Marc Bouchere6869a82000-03-20 06:03:29 +00001199 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1200
1201 if (target) {
1202 if (target->print)
1203 /* Print the target information. */
1204 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001205 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001206 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001207 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001208
1209 if (!(format & FMT_NONEWLINE))
1210 fputc('\n', stdout);
1211}
1212
1213static void
1214print_firewall_line(const struct ipt_entry *fw,
1215 const iptc_handle_t h)
1216{
1217 struct ipt_entry_target *t;
1218
1219 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001220 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001221}
1222
1223static int
1224append_entry(const ipt_chainlabel chain,
1225 struct ipt_entry *fw,
1226 unsigned int nsaddrs,
1227 const struct in_addr saddrs[],
1228 unsigned int ndaddrs,
1229 const struct in_addr daddrs[],
1230 int verbose,
1231 iptc_handle_t *handle)
1232{
1233 unsigned int i, j;
1234 int ret = 1;
1235
1236 for (i = 0; i < nsaddrs; i++) {
1237 fw->ip.src.s_addr = saddrs[i].s_addr;
1238 for (j = 0; j < ndaddrs; j++) {
1239 fw->ip.dst.s_addr = daddrs[j].s_addr;
1240 if (verbose)
1241 print_firewall_line(fw, *handle);
1242 ret &= iptc_append_entry(chain, fw, handle);
1243 }
1244 }
1245
1246 return ret;
1247}
1248
1249static int
1250replace_entry(const ipt_chainlabel chain,
1251 struct ipt_entry *fw,
1252 unsigned int rulenum,
1253 const struct in_addr *saddr,
1254 const struct in_addr *daddr,
1255 int verbose,
1256 iptc_handle_t *handle)
1257{
1258 fw->ip.src.s_addr = saddr->s_addr;
1259 fw->ip.dst.s_addr = daddr->s_addr;
1260
1261 if (verbose)
1262 print_firewall_line(fw, *handle);
1263 return iptc_replace_entry(chain, fw, rulenum, handle);
1264}
1265
1266static int
1267insert_entry(const ipt_chainlabel chain,
1268 struct ipt_entry *fw,
1269 unsigned int rulenum,
1270 unsigned int nsaddrs,
1271 const struct in_addr saddrs[],
1272 unsigned int ndaddrs,
1273 const struct in_addr daddrs[],
1274 int verbose,
1275 iptc_handle_t *handle)
1276{
1277 unsigned int i, j;
1278 int ret = 1;
1279
1280 for (i = 0; i < nsaddrs; i++) {
1281 fw->ip.src.s_addr = saddrs[i].s_addr;
1282 for (j = 0; j < ndaddrs; j++) {
1283 fw->ip.dst.s_addr = daddrs[j].s_addr;
1284 if (verbose)
1285 print_firewall_line(fw, *handle);
1286 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1287 }
1288 }
1289
1290 return ret;
1291}
1292
Rusty Russell2e0a3212000-04-19 11:23:18 +00001293static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001294make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001295{
1296 /* Establish mask for comparison */
1297 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001298 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001299 unsigned char *mask, *mptr;
1300
1301 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001302 for (matchp = matches; matchp; matchp = matchp->next)
1303 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001304
Rusty Russell9e1d2142000-04-23 09:11:12 +00001305 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001306 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001307 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001308
Rusty Russell9e1d2142000-04-23 09:11:12 +00001309 memset(mask, 0xFF, sizeof(struct ipt_entry));
1310 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001311
Martin Josefsson78cafda2004-02-02 20:01:18 +00001312 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001313 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001314 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001315 + matchp->match->userspacesize);
1316 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001317 }
1318
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001319 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001320 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001321 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001322
1323 return mask;
1324}
1325
Marc Bouchere6869a82000-03-20 06:03:29 +00001326static int
1327delete_entry(const ipt_chainlabel chain,
1328 struct ipt_entry *fw,
1329 unsigned int nsaddrs,
1330 const struct in_addr saddrs[],
1331 unsigned int ndaddrs,
1332 const struct in_addr daddrs[],
1333 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001334 iptc_handle_t *handle,
1335 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001336{
1337 unsigned int i, j;
1338 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001339 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001340
Martin Josefsson78cafda2004-02-02 20:01:18 +00001341 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001342 for (i = 0; i < nsaddrs; i++) {
1343 fw->ip.src.s_addr = saddrs[i].s_addr;
1344 for (j = 0; j < ndaddrs; j++) {
1345 fw->ip.dst.s_addr = daddrs[j].s_addr;
1346 if (verbose)
1347 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001348 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 }
1350 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001351 free(mask);
1352
Marc Bouchere6869a82000-03-20 06:03:29 +00001353 return ret;
1354}
1355
Harald Welteae1ff9f2000-12-01 14:26:20 +00001356int
Marc Bouchere6869a82000-03-20 06:03:29 +00001357for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001358 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001359{
1360 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001361 const char *chain;
1362 char *chains;
1363 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001364
Rusty Russell9e1d2142000-04-23 09:11:12 +00001365 chain = iptc_first_chain(handle);
1366 while (chain) {
1367 chaincount++;
1368 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001369 }
1370
Rusty Russell9e1d2142000-04-23 09:11:12 +00001371 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1372 i = 0;
1373 chain = iptc_first_chain(handle);
1374 while (chain) {
1375 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1376 i++;
1377 chain = iptc_next_chain(handle);
1378 }
1379
1380 for (i = 0; i < chaincount; i++) {
1381 if (!builtinstoo
1382 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001383 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001384 continue;
1385 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1386 }
1387
1388 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001389 return ret;
1390}
1391
Harald Welteae1ff9f2000-12-01 14:26:20 +00001392int
Marc Bouchere6869a82000-03-20 06:03:29 +00001393flush_entries(const ipt_chainlabel chain, int verbose,
1394 iptc_handle_t *handle)
1395{
1396 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001397 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001398
1399 if (verbose)
1400 fprintf(stdout, "Flushing chain `%s'\n", chain);
1401 return iptc_flush_entries(chain, handle);
1402}
Marc Bouchere6869a82000-03-20 06:03:29 +00001403
1404static int
1405zero_entries(const ipt_chainlabel chain, int verbose,
1406 iptc_handle_t *handle)
1407{
1408 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001409 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001410
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 if (verbose)
1412 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1413 return iptc_zero_entries(chain, handle);
1414}
1415
Harald Welteae1ff9f2000-12-01 14:26:20 +00001416int
Marc Bouchere6869a82000-03-20 06:03:29 +00001417delete_chain(const ipt_chainlabel chain, int verbose,
1418 iptc_handle_t *handle)
1419{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001420 if (!chain)
1421 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
1423 if (verbose)
1424 fprintf(stdout, "Deleting chain `%s'\n", chain);
1425 return iptc_delete_chain(chain, handle);
1426}
1427
1428static int
1429list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1430 int expanded, int linenumbers, iptc_handle_t *handle)
1431{
1432 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001433 unsigned int format;
1434 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001435
1436 format = FMT_OPTIONS;
1437 if (!verbose)
1438 format |= FMT_NOCOUNTS;
1439 else
1440 format |= FMT_VIA;
1441
1442 if (numeric)
1443 format |= FMT_NUMERIC;
1444
1445 if (!expanded)
1446 format |= FMT_KILOMEGAGIGA;
1447
1448 if (linenumbers)
1449 format |= FMT_LINENUMBERS;
1450
Rusty Russell9e1d2142000-04-23 09:11:12 +00001451 for (this = iptc_first_chain(handle);
1452 this;
1453 this = iptc_next_chain(handle)) {
1454 const struct ipt_entry *i;
1455 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001456
Marc Bouchere6869a82000-03-20 06:03:29 +00001457 if (chain && strcmp(chain, this) != 0)
1458 continue;
1459
1460 if (found) printf("\n");
1461
1462 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001463 i = iptc_first_rule(this, handle);
1464
1465 num = 0;
1466 while (i) {
1467 print_firewall(i,
1468 iptc_get_target(i, handle),
1469 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001470 format,
1471 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001472 i = iptc_next_rule(i, handle);
1473 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001474 found = 1;
1475 }
1476
1477 errno = ENOENT;
1478 return found;
1479}
1480
1481static struct ipt_entry *
1482generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001483 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 struct ipt_entry_target *target)
1485{
1486 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001487 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001488 struct ipt_entry *e;
1489
1490 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001491 for (matchp = matches; matchp; matchp = matchp->next)
1492 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001493
Rusty Russell228e98d2000-04-27 10:28:06 +00001494 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001495 *e = *fw;
1496 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001497 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001498
1499 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001500 for (matchp = matches; matchp; matchp = matchp->next) {
1501 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1502 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001503 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001504 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001505
1506 return e;
1507}
1508
Martin Josefsson78cafda2004-02-02 20:01:18 +00001509void clear_rule_matches(struct iptables_rule_match **matches)
1510{
1511 struct iptables_rule_match *matchp, *tmp;
1512
1513 for (matchp = *matches; matchp;) {
1514 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001515 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001516 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001517 matchp->match->m = NULL;
1518 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001519 if (matchp->match == matchp->match->next) {
1520 free(matchp->match);
1521 matchp->match = NULL;
1522 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001523 free(matchp);
1524 matchp = tmp;
1525 }
1526
1527 *matches = NULL;
1528}
1529
Rusty Russell3aef54d2005-01-03 03:48:40 +00001530static void set_revision(char *name, u_int8_t revision)
1531{
1532 /* Old kernel sources don't have ".revision" field,
1533 but we stole a byte from name. */
1534 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1535 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1536}
1537
Phil Oester8cf65912005-09-19 15:00:33 +00001538void
1539get_kernel_version(void) {
1540 static struct utsname uts;
1541 int x = 0, y = 0, z = 0;
1542
1543 if (uname(&uts) == -1) {
1544 fprintf(stderr, "Unable to retrieve kernel version.\n");
1545 free_opts(1);
1546 exit(1);
1547 }
1548
1549 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1550 kernel_version = LINUX_VERSION(x, y, z);
1551}
1552
Marc Bouchere6869a82000-03-20 06:03:29 +00001553int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1554{
1555 struct ipt_entry fw, *e = NULL;
1556 int invert = 0;
1557 unsigned int nsaddrs = 0, ndaddrs = 0;
1558 struct in_addr *saddrs = NULL, *daddrs = NULL;
1559
1560 int c, verbose = 0;
1561 const char *chain = NULL;
1562 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1563 const char *policy = NULL, *newname = NULL;
1564 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001565 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001566 int ret = 1;
1567 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001568 struct iptables_rule_match *matches = NULL;
1569 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001570 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001571 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001572 const char *jumpto = "";
1573 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001574 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
1576 memset(&fw, 0, sizeof(fw));
1577
Harald Welteae1ff9f2000-12-01 14:26:20 +00001578 /* re-set optind to 0 in case do_command gets called
1579 * a second time */
1580 optind = 0;
1581
1582 /* clear mflags in case do_command gets called a second time
1583 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001584 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001585 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001586
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001587 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001588 t->tflags = 0;
1589 t->used = 0;
1590 }
1591
Marc Bouchere6869a82000-03-20 06:03:29 +00001592 /* Suppress error messages: we may add new options if we
1593 demand-load a protocol. */
1594 opterr = 0;
1595
1596 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001597 "-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 +00001598 opts, NULL)) != -1) {
1599 switch (c) {
1600 /*
1601 * Command selection
1602 */
1603 case 'A':
1604 add_command(&command, CMD_APPEND, CMD_NONE,
1605 invert);
1606 chain = optarg;
1607 break;
1608
1609 case 'D':
1610 add_command(&command, CMD_DELETE, CMD_NONE,
1611 invert);
1612 chain = optarg;
1613 if (optind < argc && argv[optind][0] != '-'
1614 && argv[optind][0] != '!') {
1615 rulenum = parse_rulenumber(argv[optind++]);
1616 command = CMD_DELETE_NUM;
1617 }
1618 break;
1619
Marc Bouchere6869a82000-03-20 06:03:29 +00001620 case 'R':
1621 add_command(&command, CMD_REPLACE, CMD_NONE,
1622 invert);
1623 chain = optarg;
1624 if (optind < argc && argv[optind][0] != '-'
1625 && argv[optind][0] != '!')
1626 rulenum = parse_rulenumber(argv[optind++]);
1627 else
1628 exit_error(PARAMETER_PROBLEM,
1629 "-%c requires a rule number",
1630 cmd2char(CMD_REPLACE));
1631 break;
1632
1633 case 'I':
1634 add_command(&command, CMD_INSERT, CMD_NONE,
1635 invert);
1636 chain = optarg;
1637 if (optind < argc && argv[optind][0] != '-'
1638 && argv[optind][0] != '!')
1639 rulenum = parse_rulenumber(argv[optind++]);
1640 else rulenum = 1;
1641 break;
1642
1643 case 'L':
1644 add_command(&command, CMD_LIST, CMD_ZERO,
1645 invert);
1646 if (optarg) chain = optarg;
1647 else if (optind < argc && argv[optind][0] != '-'
1648 && argv[optind][0] != '!')
1649 chain = argv[optind++];
1650 break;
1651
1652 case 'F':
1653 add_command(&command, CMD_FLUSH, CMD_NONE,
1654 invert);
1655 if (optarg) chain = optarg;
1656 else if (optind < argc && argv[optind][0] != '-'
1657 && argv[optind][0] != '!')
1658 chain = argv[optind++];
1659 break;
1660
1661 case 'Z':
1662 add_command(&command, CMD_ZERO, CMD_LIST,
1663 invert);
1664 if (optarg) chain = optarg;
1665 else if (optind < argc && argv[optind][0] != '-'
1666 && argv[optind][0] != '!')
1667 chain = argv[optind++];
1668 break;
1669
1670 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001671 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001672 exit_error(PARAMETER_PROBLEM,
1673 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001674 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001675 if (find_target(optarg, TRY_LOAD))
1676 exit_error(PARAMETER_PROBLEM,
1677 "chain name may not clash "
1678 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001679 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1680 invert);
1681 chain = optarg;
1682 break;
1683
1684 case 'X':
1685 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1686 invert);
1687 if (optarg) chain = optarg;
1688 else if (optind < argc && argv[optind][0] != '-'
1689 && argv[optind][0] != '!')
1690 chain = argv[optind++];
1691 break;
1692
1693 case 'E':
1694 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1695 invert);
1696 chain = optarg;
1697 if (optind < argc && argv[optind][0] != '-'
1698 && argv[optind][0] != '!')
1699 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001700 else
1701 exit_error(PARAMETER_PROBLEM,
1702 "-%c requires old-chain-name and "
1703 "new-chain-name",
1704 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 break;
1706
1707 case 'P':
1708 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1709 invert);
1710 chain = optarg;
1711 if (optind < argc && argv[optind][0] != '-'
1712 && argv[optind][0] != '!')
1713 policy = argv[optind++];
1714 else
1715 exit_error(PARAMETER_PROBLEM,
1716 "-%c requires a chain and a policy",
1717 cmd2char(CMD_SET_POLICY));
1718 break;
1719
1720 case 'h':
1721 if (!optarg)
1722 optarg = argv[optind];
1723
Rusty Russell2e0a3212000-04-19 11:23:18 +00001724 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001725 if (!matches && protocol)
1726 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001727
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001728 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001729
1730 /*
1731 * Option selection
1732 */
1733 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001734 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001735 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1736 invert);
1737
1738 /* Canonicalize into lower case */
1739 for (protocol = argv[optind-1]; *protocol; protocol++)
1740 *protocol = tolower(*protocol);
1741
1742 protocol = argv[optind-1];
1743 fw.ip.proto = parse_protocol(protocol);
1744
1745 if (fw.ip.proto == 0
1746 && (fw.ip.invflags & IPT_INV_PROTO))
1747 exit_error(PARAMETER_PROBLEM,
1748 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001749 break;
1750
1751 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001752 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001753 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1754 invert);
1755 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001756 break;
1757
1758 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001759 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1761 invert);
1762 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001763 break;
1764
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001765#ifdef IPT_F_GOTO
1766 case 'g':
1767 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1768 invert);
1769 fw.ip.flags |= IPT_F_GOTO;
1770 jumpto = parse_target(optarg);
1771 break;
1772#endif
1773
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 case 'j':
1775 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1776 invert);
1777 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001778 /* TRY_LOAD (may be chain name) */
1779 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001780
1781 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001782 size_t size;
1783
Rusty Russell73f72f52000-07-03 10:17:57 +00001784 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1785 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001786
Rusty Russell2e0a3212000-04-19 11:23:18 +00001787 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001788 target->t->u.target_size = size;
1789 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001790 set_revision(target->t->u.user.name,
1791 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001792 if (target->init != NULL)
1793 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001794 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001795 }
1796 break;
1797
1798
1799 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001800 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001801 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1802 invert);
1803 parse_interface(argv[optind-1],
1804 fw.ip.iniface,
1805 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 break;
1807
1808 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001809 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001810 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1811 invert);
1812 parse_interface(argv[optind-1],
1813 fw.ip.outiface,
1814 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001815 break;
1816
1817 case 'f':
1818 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1819 invert);
1820 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001821 break;
1822
1823 case 'v':
1824 if (!verbose)
1825 set_option(&options, OPT_VERBOSE,
1826 &fw.ip.invflags, invert);
1827 verbose++;
1828 break;
1829
Rusty Russell52a51492000-05-02 16:44:29 +00001830 case 'm': {
1831 size_t size;
1832
Marc Bouchere6869a82000-03-20 06:03:29 +00001833 if (invert)
1834 exit_error(PARAMETER_PROBLEM,
1835 "unexpected ! flag before --match");
1836
Martin Josefsson78cafda2004-02-02 20:01:18 +00001837 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001838 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1839 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001840 m->m = fw_calloc(1, size);
1841 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001842 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001843 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001844 if (m->init != NULL)
1845 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001846 if (m != m->next)
1847 /* Merge options for non-cloned matches */
1848 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001849 }
1850 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001851
1852 case 'n':
1853 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1854 invert);
1855 break;
1856
1857 case 't':
1858 if (invert)
1859 exit_error(PARAMETER_PROBLEM,
1860 "unexpected ! flag before --table");
1861 *table = argv[optind-1];
1862 break;
1863
1864 case 'x':
1865 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1866 invert);
1867 break;
1868
1869 case 'V':
1870 if (invert)
1871 printf("Not %s ;-)\n", program_version);
1872 else
1873 printf("%s v%s\n",
1874 program_name, program_version);
1875 exit(0);
1876
1877 case '0':
1878 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1879 invert);
1880 break;
1881
Harald Welte82dd2ec2000-12-19 05:18:15 +00001882 case 'M':
1883 modprobe = optarg;
1884 break;
1885
Harald Welteccd49e52001-01-23 22:54:34 +00001886 case 'c':
1887
1888 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1889 invert);
1890 pcnt = optarg;
1891 if (optind < argc && argv[optind][0] != '-'
1892 && argv[optind][0] != '!')
1893 bcnt = argv[optind++];
1894 else
1895 exit_error(PARAMETER_PROBLEM,
1896 "-%c requires packet and byte counter",
1897 opt2char(OPT_COUNTERS));
1898
Martin Josefssona28d4952004-05-26 16:04:48 +00001899 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001900 exit_error(PARAMETER_PROBLEM,
1901 "-%c packet counter not numeric",
1902 opt2char(OPT_COUNTERS));
1903
Martin Josefssona28d4952004-05-26 16:04:48 +00001904 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001905 exit_error(PARAMETER_PROBLEM,
1906 "-%c byte counter not numeric",
1907 opt2char(OPT_COUNTERS));
1908
1909 break;
1910
1911
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 case 1: /* non option */
1913 if (optarg[0] == '!' && optarg[1] == '\0') {
1914 if (invert)
1915 exit_error(PARAMETER_PROBLEM,
1916 "multiple consecutive ! not"
1917 " allowed");
1918 invert = TRUE;
1919 optarg[0] = '\0';
1920 continue;
1921 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001922 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001923 exit_tryhelp(2);
1924
1925 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001926 if (!target
1927 || !(target->parse(c - target->option_offset,
1928 argv, invert,
1929 &target->tflags,
1930 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001931 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001932 if (matchp->completed)
1933 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001934 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001935 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001936 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001937 &fw,
1938 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001939 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001940 break;
1941 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001942 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001943
1944 /* If you listen carefully, you can
1945 actually hear this code suck. */
1946
1947 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001948 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001949 * parameter, that has not been parsed yet,
1950 * it's not an option of an explicitly loaded
1951 * match or a target. However, we support
1952 * implicit loading of the protocol match
1953 * extension. '-p tcp' means 'l4 proto 6' and
1954 * at the same time 'load tcp protocol match on
1955 * demand if we specify --dport'.
1956 *
1957 * To make this work, we need to make sure:
1958 * - the parameter has not been parsed by
1959 * a match (m above)
1960 * - a protocol has been specified
1961 * - the protocol extension has not been
1962 * loaded yet, or is loaded and unused
1963 * [think of iptables-restore!]
1964 * - the protocol extension can be successively
1965 * loaded
1966 */
1967 if (m == NULL
1968 && protocol
1969 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001970 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001971 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001972 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001973 && (proto_used == 0))
1974 )
1975 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001976 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001977 /* Try loading protocol */
1978 size_t size;
1979
1980 proto_used = 1;
1981
1982 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1983 + m->size;
1984
1985 m->m = fw_calloc(1, size);
1986 m->m->u.match_size = size;
1987 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001988 set_revision(m->m->u.user.name,
1989 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001990 if (m->init != NULL)
1991 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00001992
1993 opts = merge_options(opts,
1994 m->extra_opts, &m->option_offset);
1995
1996 optind--;
1997 continue;
1998 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001999 if (!m)
2000 exit_error(PARAMETER_PROBLEM,
2001 "Unknown arg `%s'",
2002 argv[optind-1]);
2003 }
2004 }
2005 invert = FALSE;
2006 }
2007
Martin Josefsson78cafda2004-02-02 20:01:18 +00002008 for (matchp = matches; matchp; matchp = matchp->next)
2009 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002010
Marc Bouchere6869a82000-03-20 06:03:29 +00002011 if (target)
2012 target->final_check(target->tflags);
2013
2014 /* Fix me: must put inverse options checking here --MN */
2015
2016 if (optind < argc)
2017 exit_error(PARAMETER_PROBLEM,
2018 "unknown arguments found on commandline");
2019 if (!command)
2020 exit_error(PARAMETER_PROBLEM, "no command specified");
2021 if (invert)
2022 exit_error(PARAMETER_PROBLEM,
2023 "nothing appropriate following !");
2024
Harald Welte6336bfd2002-05-07 14:41:43 +00002025 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 if (!(options & OPT_DESTINATION))
2027 dhostnetworkmask = "0.0.0.0/0";
2028 if (!(options & OPT_SOURCE))
2029 shostnetworkmask = "0.0.0.0/0";
2030 }
2031
2032 if (shostnetworkmask)
2033 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2034 &(fw.ip.smsk), &nsaddrs);
2035
2036 if (dhostnetworkmask)
2037 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2038 &(fw.ip.dmsk), &ndaddrs);
2039
2040 if ((nsaddrs > 1 || ndaddrs > 1) &&
2041 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2042 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2043 " source or destination IP addresses");
2044
Marc Bouchere6869a82000-03-20 06:03:29 +00002045 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2046 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2047 "specify a unique address");
2048
2049 generic_opt_check(command, options);
2050
2051 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2052 exit_error(PARAMETER_PROBLEM,
2053 "chain name `%s' too long (must be under %i chars)",
2054 chain, IPT_FUNCTION_MAXNAMELEN);
2055
Harald Welteae1ff9f2000-12-01 14:26:20 +00002056 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002057 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002058 *handle = iptc_init(*table);
2059
Rusty Russell8beb0492004-12-22 00:37:10 +00002060 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00002061 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002062 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002063
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 if (!*handle)
2065 exit_error(VERSION_PROBLEM,
2066 "can't initialize iptables table `%s': %s",
2067 *table, iptc_strerror(errno));
2068
Harald Welte6336bfd2002-05-07 14:41:43 +00002069 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 || command == CMD_DELETE
2071 || command == CMD_INSERT
2072 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002073 if (strcmp(chain, "PREROUTING") == 0
2074 || strcmp(chain, "INPUT") == 0) {
2075 /* -o not valid with incoming packets. */
2076 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 exit_error(PARAMETER_PROBLEM,
2078 "Can't use -%c with %s\n",
2079 opt2char(OPT_VIANAMEOUT),
2080 chain);
2081 }
2082
Rusty Russella4860fd2000-06-17 16:13:02 +00002083 if (strcmp(chain, "POSTROUTING") == 0
2084 || strcmp(chain, "OUTPUT") == 0) {
2085 /* -i not valid with outgoing packets */
2086 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002087 exit_error(PARAMETER_PROBLEM,
2088 "Can't use -%c with %s\n",
2089 opt2char(OPT_VIANAMEIN),
2090 chain);
2091 }
2092
2093 if (target && iptc_is_chain(jumpto, *handle)) {
2094 printf("Warning: using chain %s, not extension\n",
2095 jumpto);
2096
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002097 if (target->t)
2098 free(target->t);
2099
Marc Bouchere6869a82000-03-20 06:03:29 +00002100 target = NULL;
2101 }
2102
2103 /* If they didn't specify a target, or it's a chain
2104 name, use standard. */
2105 if (!target
2106 && (strlen(jumpto) == 0
2107 || iptc_is_chain(jumpto, *handle))) {
2108 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002109
Rusty Russell52a51492000-05-02 16:44:29 +00002110 target = find_target(IPT_STANDARD_TARGET,
2111 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002112
2113 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002114 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002115 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002116 target->t->u.target_size = size;
2117 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002118 if (!iptc_is_chain(jumpto, *handle))
2119 set_revision(target->t->u.user.name,
2120 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002121 if (target->init != NULL)
2122 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002123 }
2124
Rusty Russell7e53bf92000-03-20 07:03:28 +00002125 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002126 /* it is no chain, and we can't load a plugin.
2127 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002128 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002129 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002130#ifdef IPT_F_GOTO
2131 if (fw.ip.flags & IPT_F_GOTO)
2132 exit_error(PARAMETER_PROBLEM,
2133 "goto '%s' is not a chain\n", jumpto);
2134#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002135 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002136 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002137 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002138 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002139 }
2140 }
2141
2142 switch (command) {
2143 case CMD_APPEND:
2144 ret = append_entry(chain, e,
2145 nsaddrs, saddrs, ndaddrs, daddrs,
2146 options&OPT_VERBOSE,
2147 handle);
2148 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 case CMD_DELETE:
2150 ret = delete_entry(chain, e,
2151 nsaddrs, saddrs, ndaddrs, daddrs,
2152 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002153 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002154 break;
2155 case CMD_DELETE_NUM:
2156 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2157 break;
2158 case CMD_REPLACE:
2159 ret = replace_entry(chain, e, rulenum - 1,
2160 saddrs, daddrs, options&OPT_VERBOSE,
2161 handle);
2162 break;
2163 case CMD_INSERT:
2164 ret = insert_entry(chain, e, rulenum - 1,
2165 nsaddrs, saddrs, ndaddrs, daddrs,
2166 options&OPT_VERBOSE,
2167 handle);
2168 break;
2169 case CMD_LIST:
2170 ret = list_entries(chain,
2171 options&OPT_VERBOSE,
2172 options&OPT_NUMERIC,
2173 options&OPT_EXPANDED,
2174 options&OPT_LINENUMBERS,
2175 handle);
2176 break;
2177 case CMD_FLUSH:
2178 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2179 break;
2180 case CMD_ZERO:
2181 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2182 break;
2183 case CMD_LIST|CMD_ZERO:
2184 ret = list_entries(chain,
2185 options&OPT_VERBOSE,
2186 options&OPT_NUMERIC,
2187 options&OPT_EXPANDED,
2188 options&OPT_LINENUMBERS,
2189 handle);
2190 if (ret)
2191 ret = zero_entries(chain,
2192 options&OPT_VERBOSE, handle);
2193 break;
2194 case CMD_NEW_CHAIN:
2195 ret = iptc_create_chain(chain, handle);
2196 break;
2197 case CMD_DELETE_CHAIN:
2198 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2199 break;
2200 case CMD_RENAME_CHAIN:
2201 ret = iptc_rename_chain(chain, newname, handle);
2202 break;
2203 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002204 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002205 break;
2206 default:
2207 /* We should never reach this... */
2208 exit_tryhelp(2);
2209 }
2210
2211 if (verbose > 1)
2212 dump_entries(*handle);
2213
Martin Josefsson78cafda2004-02-02 20:01:18 +00002214 clear_rule_matches(&matches);
2215
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002216 if (e != NULL) {
2217 free(e);
2218 e = NULL;
2219 }
2220
keso6997cdf2004-07-04 15:20:53 +00002221 free(saddrs);
2222 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002223 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002224
Marc Bouchere6869a82000-03-20 06:03:29 +00002225 return ret;
2226}