blob: b66e5e1b6f577a7a26b5a606cf1630d1b0f6c358 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
41#include <sys/wait.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000043
44#ifndef TRUE
45#define TRUE 1
46#endif
47#ifndef FALSE
48#define FALSE 0
49#endif
50
Harald Welte82dd2ec2000-12-19 05:18:15 +000051#ifndef PROC_SYS_MODPROBE
52#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
82#define CMD_CHECK 0x0800U
83#define CMD_RENAME_CHAIN 0x1000U
84#define NUMBER_OF_CMD 13
85static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000086 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000087
88#define OPTION_OFFSET 256
89
90#define OPT_NONE 0x00000U
91#define OPT_NUMERIC 0x00001U
92#define OPT_SOURCE 0x00002U
93#define OPT_DESTINATION 0x00004U
94#define OPT_PROTOCOL 0x00008U
95#define OPT_JUMP 0x00010U
96#define OPT_VERBOSE 0x00020U
97#define OPT_EXPANDED 0x00040U
98#define OPT_VIANAMEIN 0x00080U
99#define OPT_VIANAMEOUT 0x00100U
100#define OPT_FRAGMENT 0x00200U
101#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000102#define OPT_COUNTERS 0x00800U
103#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000123 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "fragments", 0, 0, 'f' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000136 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000137 { "set-counters", 1, 0, 'c' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000138 { "goto", 1, 0, 'g' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000139 { 0 }
140};
141
Illes Marci63e90632003-03-03 08:08:37 +0000142/* we need this for iptables-restore. iptables-restore.c sets line to the
143 * current line of the input file, in order to give a more precise error
144 * message. iptables itself doesn't need this, so it is initialized to the
145 * magic number of -1 */
146int line = -1;
147
Marc Bouchere6869a82000-03-20 06:03:29 +0000148static struct option *opts = original_opts;
149static unsigned int global_option_offset = 0;
150
151/* Table of legal combinations of commands and options. If any of the
152 * given commands make an option legal, that option is legal (applies to
153 * CMD_LIST and CMD_ZERO only).
154 * Key:
155 * + compulsory
156 * x illegal
157 * optional
158 */
159
160static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
161/* Well, it's better than "Re: Linux vs FreeBSD" */
162{
163 /* -n -s -d -p -j -v -x -i -o -f --line */
164/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
165/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
167/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
168/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
169/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
170/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000175/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000176/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
177};
178
179static int inverse_for_options[NUMBER_OF_OPT] =
180{
181/* -n */ 0,
182/* -s */ IPT_INV_SRCIP,
183/* -d */ IPT_INV_DSTIP,
184/* -p */ IPT_INV_PROTO,
185/* -j */ 0,
186/* -v */ 0,
187/* -x */ 0,
188/* -i */ IPT_INV_VIA_IN,
189/* -o */ IPT_INV_VIA_OUT,
190/* -f */ IPT_INV_FRAG,
191/*--line*/ 0
192};
193
194const char *program_version;
195const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000196char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000197
Phil Oester8cf65912005-09-19 15:00:33 +0000198int kernel_version;
199
Rusty Russell2e0a3212000-04-19 11:23:18 +0000200/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000201struct iptables_match *iptables_matches = NULL;
202struct iptables_target *iptables_targets = NULL;
203
204/* Extra debugging from libiptc */
205extern void dump_entries(const iptc_handle_t handle);
206
207/* A few hardcoded protocols for 'all' and in case the user has no
208 /etc/protocols */
209struct pprot {
210 char *name;
211 u_int8_t num;
212};
213
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000215/* defined in netinet/in.h */
216#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000217#ifndef IPPROTO_ESP
218#define IPPROTO_ESP 50
219#endif
220#ifndef IPPROTO_AH
221#define IPPROTO_AH 51
222#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000223#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000224
Marc Bouchere6869a82000-03-20 06:03:29 +0000225static const struct pprot chain_protos[] = {
226 { "tcp", IPPROTO_TCP },
227 { "udp", IPPROTO_UDP },
228 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000231 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000232 { "all", 0 },
233};
234
235static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000236proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000237{
238 unsigned int i;
239
Rusty Russell28381a42000-05-10 00:19:50 +0000240 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
253struct in_addr *
254dotted_to_addr(const char *dotted)
255{
256 static struct in_addr addr;
257 unsigned char *addrp;
258 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000259 unsigned int onebyte;
260 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000261 char buf[20];
262
263 /* copy dotted string, because we need to modify it */
264 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000265 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000266 addrp = (unsigned char *) &(addr.s_addr);
267
268 p = buf;
269 for (i = 0; i < 3; i++) {
270 if ((q = strchr(p, '.')) == NULL)
271 return (struct in_addr *) NULL;
272
273 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000274 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000275 return (struct in_addr *) NULL;
276
277 addrp[i] = (unsigned char) onebyte;
278 p = q + 1;
279 }
280
281 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000282 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000283 return (struct in_addr *) NULL;
284
285 addrp[3] = (unsigned char) onebyte;
286
287 return &addr;
288}
289
290static struct in_addr *
291network_to_addr(const char *name)
292{
293 struct netent *net;
294 static struct in_addr addr;
295
296 if ((net = getnetbyname(name)) != NULL) {
297 if (net->n_addrtype != AF_INET)
298 return (struct in_addr *) NULL;
299 addr.s_addr = htonl((unsigned long) net->n_net);
300 return &addr;
301 }
302
303 return (struct in_addr *) NULL;
304}
305
306static void
307inaddrcpy(struct in_addr *dst, struct in_addr *src)
308{
309 /* memcpy(dst, src, sizeof(struct in_addr)); */
310 dst->s_addr = src->s_addr;
311}
312
Pablo Neiradfdcd642005-05-29 19:05:23 +0000313static void free_opts(int reset_offset)
314{
315 if (opts != original_opts) {
316 free(opts);
317 opts = original_opts;
318 if (reset_offset)
319 global_option_offset = 0;
320 }
321}
322
Marc Bouchere6869a82000-03-20 06:03:29 +0000323void
324exit_error(enum exittype status, char *msg, ...)
325{
326 va_list args;
327
328 va_start(args, msg);
329 fprintf(stderr, "%s v%s: ", program_name, program_version);
330 vfprintf(stderr, msg, args);
331 va_end(args);
332 fprintf(stderr, "\n");
333 if (status == PARAMETER_PROBLEM)
334 exit_tryhelp(status);
335 if (status == VERSION_PROBLEM)
336 fprintf(stderr,
337 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000338 /* On error paths, make sure that we don't leak memory */
339 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 exit(status);
341}
342
343void
344exit_tryhelp(int status)
345{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000346 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000347 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000348 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
349 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000350 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000351 exit(status);
352}
353
354void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000355exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000356{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000357 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000358 struct iptables_target *t = NULL;
359
Marc Bouchere6869a82000-03-20 06:03:29 +0000360 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000361"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000362" %s -[RI] chain rulenum rule-specification [options]\n"
363" %s -D chain rulenum [options]\n"
364" %s -[LFZ] [chain] [options]\n"
365" %s -[NX] chain\n"
366" %s -E old-chain-name new-chain-name\n"
367" %s -P chain target [options]\n"
368" %s -h (print this help information)\n\n",
369 program_name, program_version, program_name, program_name,
370 program_name, program_name, program_name, program_name,
371 program_name, program_name);
372
373 printf(
374"Commands:\n"
375"Either long or short options are allowed.\n"
376" --append -A chain Append to chain\n"
377" --delete -D chain Delete matching rule from chain\n"
378" --delete -D chain rulenum\n"
379" Delete rule rulenum (1 = first) from chain\n"
380" --insert -I chain [rulenum]\n"
381" Insert in chain as rulenum (default 1=first)\n"
382" --replace -R chain rulenum\n"
383" Replace rule rulenum (1 = first) in chain\n"
384" --list -L [chain] List the rules in a chain or all chains\n"
385" --flush -F [chain] Delete all rules in chain or all chains\n"
386" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000387" --new -N chain Create a new user-defined chain\n"
388" --delete-chain\n"
389" -X [chain] Delete a user-defined chain\n"
390" --policy -P chain target\n"
391" Change policy on chain to target\n"
392" --rename-chain\n"
393" -E old-chain new-chain\n"
394" Change chain name, (moving any references)\n"
395
396"Options:\n"
397" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
398" --source -s [!] address[/mask]\n"
399" source specification\n"
400" --destination -d [!] address[/mask]\n"
401" destination specification\n"
402" --in-interface -i [!] input name[+]\n"
403" network interface name ([+] for wildcard)\n"
404" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000405" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000406#ifdef IPT_F_GOTO
407" --goto -g chain\n"
408" jump to chain with no return\n"
409#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000410" --match -m match\n"
411" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000412" --numeric -n numeric output of addresses and ports\n"
413" --out-interface -o [!] output name[+]\n"
414" network interface name ([+] for wildcard)\n"
415" --table -t table table to manipulate (default: `filter')\n"
416" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000417" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000418" --exact -x expand numbers (display exact values)\n"
419"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000420" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000421" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000422"[!] --version -V print package version.\n");
423
Rusty Russell363112d2000-08-11 13:49:26 +0000424 /* Print out any special helps. A user might like to be able
425 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000426 results. So we call help for all specified matches & targets */
427 for (t = iptables_targets; t ;t = t->next) {
428 if (t->used) {
429 printf("\n");
430 t->help();
431 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000432 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000433 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000434 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000435 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000436 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000437 exit(0);
438}
439
440static void
441generic_opt_check(int command, int options)
442{
443 int i, j, legal = 0;
444
445 /* Check that commands are valid with options. Complicated by the
446 * fact that if an option is legal with *any* command given, it is
447 * legal overall (ie. -z and -l).
448 */
449 for (i = 0; i < NUMBER_OF_OPT; i++) {
450 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
451
452 for (j = 0; j < NUMBER_OF_CMD; j++) {
453 if (!(command & (1<<j)))
454 continue;
455
456 if (!(options & (1<<i))) {
457 if (commands_v_options[j][i] == '+')
458 exit_error(PARAMETER_PROBLEM,
459 "You need to supply the `-%c' "
460 "option for this command\n",
461 optflags[i]);
462 } else {
463 if (commands_v_options[j][i] != 'x')
464 legal = 1;
465 else if (legal == 0)
466 legal = -1;
467 }
468 }
469 if (legal == -1)
470 exit_error(PARAMETER_PROBLEM,
471 "Illegal option `-%c' with this command\n",
472 optflags[i]);
473 }
474}
475
476static char
477opt2char(int option)
478{
479 const char *ptr;
480 for (ptr = optflags; option > 1; option >>= 1, ptr++);
481
482 return *ptr;
483}
484
485static char
486cmd2char(int option)
487{
488 const char *ptr;
489 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
490
491 return *ptr;
492}
493
494static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000495add_command(unsigned int *cmd, const int newcmd, const int othercmds,
496 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000497{
498 if (invert)
499 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
500 if (*cmd & (~othercmds))
501 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
502 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
503 *cmd |= newcmd;
504}
505
506int
Harald Welteb77f1da2002-03-14 11:35:58 +0000507check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000508{
509 if (option && strcmp(option, "!") == 0) {
510 if (*invert)
511 exit_error(PARAMETER_PROBLEM,
512 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000513 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000514 if (optind) {
515 *optind = *optind+1;
516 if (argc && *optind > argc)
517 exit_error(PARAMETER_PROBLEM,
518 "no argument following `!'");
519 }
520
Marc Bouchere6869a82000-03-20 06:03:29 +0000521 return TRUE;
522 }
523 return FALSE;
524}
525
526static void *
527fw_calloc(size_t count, size_t size)
528{
529 void *p;
530
531 if ((p = calloc(count, size)) == NULL) {
532 perror("iptables: calloc failed");
533 exit(1);
534 }
535 return p;
536}
537
538static void *
539fw_malloc(size_t size)
540{
541 void *p;
542
543 if ((p = malloc(size)) == NULL) {
544 perror("iptables: malloc failed");
545 exit(1);
546 }
547 return p;
548}
549
550static struct in_addr *
551host_to_addr(const char *name, unsigned int *naddr)
552{
553 struct hostent *host;
554 struct in_addr *addr;
555 unsigned int i;
556
557 *naddr = 0;
558 if ((host = gethostbyname(name)) != NULL) {
559 if (host->h_addrtype != AF_INET ||
560 host->h_length != sizeof(struct in_addr))
561 return (struct in_addr *) NULL;
562
563 while (host->h_addr_list[*naddr] != (char *) NULL)
564 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000565 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000566 for (i = 0; i < *naddr; i++)
567 inaddrcpy(&(addr[i]),
568 (struct in_addr *) host->h_addr_list[i]);
569 return addr;
570 }
571
572 return (struct in_addr *) NULL;
573}
574
575static char *
576addr_to_host(const struct in_addr *addr)
577{
578 struct hostent *host;
579
580 if ((host = gethostbyaddr((char *) addr,
581 sizeof(struct in_addr), AF_INET)) != NULL)
582 return (char *) host->h_name;
583
584 return (char *) NULL;
585}
586
587/*
588 * All functions starting with "parse" should succeed, otherwise
589 * the program fails.
590 * Most routines return pointers to static data that may change
591 * between calls to the same or other routines with a few exceptions:
592 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
593 * return global static data.
594*/
595
596static struct in_addr *
597parse_hostnetwork(const char *name, unsigned int *naddrs)
598{
599 struct in_addr *addrp, *addrptmp;
600
601 if ((addrptmp = dotted_to_addr(name)) != NULL ||
602 (addrptmp = network_to_addr(name)) != NULL) {
603 addrp = fw_malloc(sizeof(struct in_addr));
604 inaddrcpy(addrp, addrptmp);
605 *naddrs = 1;
606 return addrp;
607 }
608 if ((addrp = host_to_addr(name, naddrs)) != NULL)
609 return addrp;
610
611 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
612}
613
614static struct in_addr *
615parse_mask(char *mask)
616{
617 static struct in_addr maskaddr;
618 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000619 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000620
621 if (mask == NULL) {
622 /* no mask at all defaults to 32 bits */
623 maskaddr.s_addr = 0xFFFFFFFF;
624 return &maskaddr;
625 }
626 if ((addrp = dotted_to_addr(mask)) != NULL)
627 /* dotted_to_addr already returns a network byte order addr */
628 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000629 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000630 exit_error(PARAMETER_PROBLEM,
631 "invalid mask `%s' specified", mask);
632 if (bits != 0) {
633 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
634 return &maskaddr;
635 }
636
637 maskaddr.s_addr = 0L;
638 return &maskaddr;
639}
640
Marc Boucherb93c7982001-12-06 14:50:19 +0000641void
Marc Bouchere6869a82000-03-20 06:03:29 +0000642parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
643 struct in_addr *maskp, unsigned int *naddrs)
644{
645 struct in_addr *addrp;
646 char buf[256];
647 char *p;
648 int i, j, k, n;
649
650 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000651 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000652 if ((p = strrchr(buf, '/')) != NULL) {
653 *p = '\0';
654 addrp = parse_mask(p + 1);
655 } else
656 addrp = parse_mask(NULL);
657 inaddrcpy(maskp, addrp);
658
659 /* if a null mask is given, the name is ignored, like in "any/0" */
660 if (maskp->s_addr == 0L)
661 strcpy(buf, "0.0.0.0");
662
663 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
664 n = *naddrs;
665 for (i = 0, j = 0; i < n; i++) {
666 addrp[j++].s_addr &= maskp->s_addr;
667 for (k = 0; k < j - 1; k++) {
668 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
669 (*naddrs)--;
670 j--;
671 break;
672 }
673 }
674 }
675}
676
677struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000678find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000679{
680 struct iptables_match *ptr;
681
682 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
683 if (strcmp(name, ptr->name) == 0)
684 break;
685 }
686
Harald Welte3efb6ea2001-08-06 18:50:21 +0000687#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000688 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000689 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000690 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000691 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000692 if (dlopen(path, RTLD_NOW)) {
693 /* Found library. If it didn't register itself,
694 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000695 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000696
Rusty Russell9e1d2142000-04-23 09:11:12 +0000697 if (!ptr)
698 exit_error(PARAMETER_PROBLEM,
699 "Couldn't load match `%s'\n",
700 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000701 } else if (tryload == LOAD_MUST_SUCCEED)
702 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000703 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000704 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000705 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000706#else
707 if (ptr && !ptr->loaded) {
708 if (tryload != DONT_LOAD)
709 ptr->loaded = 1;
710 else
711 ptr = NULL;
712 }
Marc Boucher067477b2002-03-24 15:09:31 +0000713 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
714 exit_error(PARAMETER_PROBLEM,
715 "Couldn't find match `%s'\n", name);
716 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000717#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000718
Martin Josefsson78cafda2004-02-02 20:01:18 +0000719 if (ptr && matches) {
720 struct iptables_rule_match **i;
721 struct iptables_rule_match *newentry;
722
723 newentry = fw_malloc(sizeof(struct iptables_rule_match));
724
725 for (i = matches; *i; i = &(*i)->next);
726 newentry->match = ptr;
727 newentry->next = NULL;
728 *i = newentry;
729 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000730
Marc Bouchere6869a82000-03-20 06:03:29 +0000731 return ptr;
732}
733
Rusty Russell28381a42000-05-10 00:19:50 +0000734/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
735static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000736find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000737{
Harald Welteed498492001-07-23 01:24:22 +0000738 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000739
Harald Welte0b0013a2002-02-18 16:15:31 +0000740 if (string_to_number(pname, 0, 255, &proto) != -1) {
741 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000742
Harald Welte0b0013a2002-02-18 16:15:31 +0000743 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000744 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000745 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000746 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000747
748 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000749}
750
Marc Boucherb93c7982001-12-06 14:50:19 +0000751u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000752parse_protocol(const char *s)
753{
Harald Welteed498492001-07-23 01:24:22 +0000754 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000755
Harald Welteed498492001-07-23 01:24:22 +0000756 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000757 struct protoent *pent;
758
759 if ((pent = getprotobyname(s)))
760 proto = pent->p_proto;
761 else {
762 unsigned int i;
763 for (i = 0;
764 i < sizeof(chain_protos)/sizeof(struct pprot);
765 i++) {
766 if (strcmp(s, chain_protos[i].name) == 0) {
767 proto = chain_protos[i].num;
768 break;
769 }
770 }
771 if (i == sizeof(chain_protos)/sizeof(struct pprot))
772 exit_error(PARAMETER_PROBLEM,
773 "unknown protocol `%s' specified",
774 s);
775 }
776 }
777
778 return (u_int16_t)proto;
779}
780
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000781void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000782{
783 int vialen = strlen(arg);
784 unsigned int i;
785
786 memset(mask, 0, IFNAMSIZ);
787 memset(vianame, 0, IFNAMSIZ);
788
789 if (vialen + 1 > IFNAMSIZ)
790 exit_error(PARAMETER_PROBLEM,
791 "interface name `%s' must be shorter than IFNAMSIZ"
792 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000793
Marc Bouchere6869a82000-03-20 06:03:29 +0000794 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000795 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 memset(mask, 0, IFNAMSIZ);
797 else if (vianame[vialen - 1] == '+') {
798 memset(mask, 0xFF, vialen - 1);
799 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000800 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000801 } else {
802 /* Include nul-terminator in match */
803 memset(mask, 0xFF, vialen + 1);
804 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000805 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000806 if (!isalnum(vianame[i])
807 && vianame[i] != '_'
808 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000809 printf("Warning: wierd character in interface"
810 " `%s' (No aliases, :, ! or *).\n",
811 vianame);
812 break;
813 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000814 }
815 }
816}
817
818/* Can't be zero. */
819static int
820parse_rulenumber(const char *rule)
821{
Harald Welteed498492001-07-23 01:24:22 +0000822 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000823
Harald Welteed498492001-07-23 01:24:22 +0000824 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000825 exit_error(PARAMETER_PROBLEM,
826 "Invalid rule number `%s'", rule);
827
828 return rulenum;
829}
830
831static const char *
832parse_target(const char *targetname)
833{
834 const char *ptr;
835
836 if (strlen(targetname) < 1)
837 exit_error(PARAMETER_PROBLEM,
838 "Invalid target name (too short)");
839
840 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
841 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000842 "Invalid target name `%s' (%u chars max)",
843 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000844
845 for (ptr = targetname; *ptr; ptr++)
846 if (isspace(*ptr))
847 exit_error(PARAMETER_PROBLEM,
848 "Invalid target name `%s'", targetname);
849 return targetname;
850}
851
852static char *
853addr_to_network(const struct in_addr *addr)
854{
855 struct netent *net;
856
857 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
858 return (char *) net->n_name;
859
860 return (char *) NULL;
861}
862
863char *
864addr_to_dotted(const struct in_addr *addrp)
865{
866 static char buf[20];
867 const unsigned char *bytep;
868
869 bytep = (const unsigned char *) &(addrp->s_addr);
870 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
871 return buf;
872}
Marc Boucherb93c7982001-12-06 14:50:19 +0000873
874char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000875addr_to_anyname(const struct in_addr *addr)
876{
877 char *name;
878
879 if ((name = addr_to_host(addr)) != NULL ||
880 (name = addr_to_network(addr)) != NULL)
881 return name;
882
883 return addr_to_dotted(addr);
884}
885
Marc Boucherb93c7982001-12-06 14:50:19 +0000886char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000887mask_to_dotted(const struct in_addr *mask)
888{
889 int i;
890 static char buf[20];
891 u_int32_t maskaddr, bits;
892
893 maskaddr = ntohl(mask->s_addr);
894
895 if (maskaddr == 0xFFFFFFFFL)
896 /* we don't want to see "/32" */
897 return "";
898
899 i = 32;
900 bits = 0xFFFFFFFEL;
901 while (--i >= 0 && maskaddr != bits)
902 bits <<= 1;
903 if (i >= 0)
904 sprintf(buf, "/%d", i);
905 else
906 /* mask was not a decent combination of 1's and 0's */
907 sprintf(buf, "/%s", addr_to_dotted(mask));
908
909 return buf;
910}
911
912int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000913string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
914 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000915{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000917 char *end;
918
919 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000920 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000921 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000922 if (*end == '\0' && end != s) {
923 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000924 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000925 *ret = number;
926 return 0;
927 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 }
929 return -1;
930}
931
Martin Josefssonb105bc92004-05-26 15:54:49 +0000932int
933string_to_number_l(const char *s, unsigned long min, unsigned long max,
934 unsigned long *ret)
935{
936 int result;
937 unsigned long long number;
938
939 result = string_to_number_ll(s, min, max, &number);
940 *ret = (unsigned long)number;
941
942 return result;
943}
944
945int string_to_number(const char *s, unsigned int min, unsigned int max,
946 unsigned int *ret)
947{
948 int result;
949 unsigned long number;
950
951 result = string_to_number_l(s, min, max, &number);
952 *ret = (unsigned int)number;
953
954 return result;
955}
956
Marc Bouchere6869a82000-03-20 06:03:29 +0000957static void
958set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
959 int invert)
960{
961 if (*options & option)
962 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
963 opt2char(option));
964 *options |= option;
965
966 if (invert) {
967 unsigned int i;
968 for (i = 0; 1 << i != option; i++);
969
970 if (!inverse_for_options[i])
971 exit_error(PARAMETER_PROBLEM,
972 "cannot have ! before -%c",
973 opt2char(option));
974 *invflg |= inverse_for_options[i];
975 }
976}
977
978struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000979find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000980{
981 struct iptables_target *ptr;
982
983 /* Standard target? */
984 if (strcmp(name, "") == 0
985 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
986 || strcmp(name, IPTC_LABEL_DROP) == 0
987 || strcmp(name, IPTC_LABEL_QUEUE) == 0
988 || strcmp(name, IPTC_LABEL_RETURN) == 0)
989 name = "standard";
990
991 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
992 if (strcmp(name, ptr->name) == 0)
993 break;
994 }
995
Harald Welte3efb6ea2001-08-06 18:50:21 +0000996#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000997 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000998 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000999 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001000 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001001 if (dlopen(path, RTLD_NOW)) {
1002 /* Found library. If it didn't register itself,
1003 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001004 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001005 if (!ptr)
1006 exit_error(PARAMETER_PROBLEM,
1007 "Couldn't load target `%s'\n",
1008 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001009 } else if (tryload == LOAD_MUST_SUCCEED)
1010 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001011 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001012 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001013 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001014#else
1015 if (ptr && !ptr->loaded) {
1016 if (tryload != DONT_LOAD)
1017 ptr->loaded = 1;
1018 else
1019 ptr = NULL;
1020 }
Marc Boucher067477b2002-03-24 15:09:31 +00001021 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1022 exit_error(PARAMETER_PROBLEM,
1023 "Couldn't find target `%s'\n", name);
1024 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001025#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001026
Harald Welteae1ff9f2000-12-01 14:26:20 +00001027 if (ptr)
1028 ptr->used = 1;
1029
Marc Bouchere6869a82000-03-20 06:03:29 +00001030 return ptr;
1031}
1032
1033static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001034merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001035 unsigned int *option_offset)
1036{
1037 unsigned int num_old, num_new, i;
1038 struct option *merge;
1039
1040 for (num_old = 0; oldopts[num_old].name; num_old++);
1041 for (num_new = 0; newopts[num_new].name; num_new++);
1042
1043 global_option_offset += OPTION_OFFSET;
1044 *option_offset = global_option_offset;
1045
1046 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1047 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001048 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001049 for (i = 0; i < num_new; i++) {
1050 merge[num_old + i] = newopts[i];
1051 merge[num_old + i].val += *option_offset;
1052 }
1053 memset(merge + num_old + num_new, 0, sizeof(struct option));
1054
1055 return merge;
1056}
1057
Rusty Russell3aef54d2005-01-03 03:48:40 +00001058static int compatible_revision(const char *name, u_int8_t revision, int opt)
1059{
1060 struct ipt_get_revision rev;
1061 socklen_t s = sizeof(rev);
1062 int max_rev, sockfd;
1063
1064 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1065 if (sockfd < 0) {
1066 fprintf(stderr, "Could not open socket to kernel: %s\n",
1067 strerror(errno));
1068 exit(1);
1069 }
1070
1071 strcpy(rev.name, name);
1072 rev.revision = revision;
1073
1074 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1075 if (max_rev < 0) {
1076 /* Definitely don't support this? */
1077 if (errno == EPROTONOSUPPORT) {
1078 close(sockfd);
1079 return 0;
1080 } else if (errno == ENOPROTOOPT) {
1081 close(sockfd);
1082 /* Assume only revision 0 support (old kernel) */
1083 return (revision == 0);
1084 } else {
1085 fprintf(stderr, "getsockopt failed strangely: %s\n",
1086 strerror(errno));
1087 exit(1);
1088 }
1089 }
1090 close(sockfd);
1091 return 1;
1092}
1093
1094static int compatible_match_revision(const char *name, u_int8_t revision)
1095{
1096 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1097}
1098
1099static int compatible_target_revision(const char *name, u_int8_t revision)
1100{
1101 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1102}
1103
Marc Bouchere6869a82000-03-20 06:03:29 +00001104void
1105register_match(struct iptables_match *me)
1106{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001107 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001108
Marc Bouchere6869a82000-03-20 06:03:29 +00001109 if (strcmp(me->version, program_version) != 0) {
1110 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1111 program_name, me->name, me->version, program_version);
1112 exit(1);
1113 }
1114
Martin Josefsson93911bf2005-01-03 07:46:07 +00001115 /* Revision field stole a char from name. */
1116 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001117 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001118 program_name, me->name);
1119 exit(1);
1120 }
1121
Jones Desougif5b86e62005-12-22 03:33:50 +00001122 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001123 if (old) {
1124 if (old->revision == me->revision) {
1125 fprintf(stderr,
1126 "%s: match `%s' already registered.\n",
1127 program_name, me->name);
1128 exit(1);
1129 }
1130
1131 /* Now we have two (or more) options, check compatibility. */
1132 if (compatible_match_revision(old->name, old->revision)
1133 && old->revision > me->revision)
1134 return;
1135
1136 /* Replace if compatible. */
1137 if (!compatible_match_revision(me->name, me->revision))
1138 return;
1139
1140 /* Delete old one. */
1141 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1142 *i = old->next;
1143 }
1144
Rusty Russell73f72f52000-07-03 10:17:57 +00001145 if (me->size != IPT_ALIGN(me->size)) {
1146 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001147 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001148 exit(1);
1149 }
1150
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001151 /* Append to list. */
1152 for (i = &iptables_matches; *i; i = &(*i)->next);
1153 me->next = NULL;
1154 *i = me;
1155
Marc Bouchere6869a82000-03-20 06:03:29 +00001156 me->m = NULL;
1157 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001158}
1159
1160void
1161register_target(struct iptables_target *me)
1162{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001163 struct iptables_target *old;
1164
Marc Bouchere6869a82000-03-20 06:03:29 +00001165 if (strcmp(me->version, program_version) != 0) {
1166 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1167 program_name, me->name, me->version, program_version);
1168 exit(1);
1169 }
1170
Martin Josefsson93911bf2005-01-03 07:46:07 +00001171 /* Revision field stole a char from name. */
1172 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001173 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001174 program_name, me->name);
1175 exit(1);
1176 }
1177
Jones Desougif5b86e62005-12-22 03:33:50 +00001178 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001179 if (old) {
1180 struct iptables_target **i;
1181
1182 if (old->revision == me->revision) {
1183 fprintf(stderr,
1184 "%s: target `%s' already registered.\n",
1185 program_name, me->name);
1186 exit(1);
1187 }
1188
Rusty Russell3aef54d2005-01-03 03:48:40 +00001189 /* Now we have two (or more) options, check compatibility. */
1190 if (compatible_target_revision(old->name, old->revision)
1191 && old->revision > me->revision)
1192 return;
1193
1194 /* Replace if compatible. */
1195 if (!compatible_target_revision(me->name, me->revision))
1196 return;
1197
1198 /* Delete old one. */
1199 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1200 *i = old->next;
1201 }
1202
Rusty Russell73f72f52000-07-03 10:17:57 +00001203 if (me->size != IPT_ALIGN(me->size)) {
1204 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001205 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001206 exit(1);
1207 }
1208
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 /* Prepend to list. */
1210 me->next = iptables_targets;
1211 iptables_targets = me;
1212 me->t = NULL;
1213 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001214}
1215
1216static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001217print_num(u_int64_t number, unsigned int format)
1218{
1219 if (format & FMT_KILOMEGAGIGA) {
1220 if (number > 99999) {
1221 number = (number + 500) / 1000;
1222 if (number > 9999) {
1223 number = (number + 500) / 1000;
1224 if (number > 9999) {
1225 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001226 if (number > 9999) {
1227 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001228 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001229 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001231 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001232 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001233 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001234 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001235 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001236 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001237 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001238 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001239}
1240
1241
1242static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001243print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1244{
1245 struct ipt_counters counters;
1246 const char *pol = iptc_get_policy(chain, &counters, handle);
1247 printf("Chain %s", chain);
1248 if (pol) {
1249 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001250 if (!(format & FMT_NOCOUNTS)) {
1251 fputc(' ', stdout);
1252 print_num(counters.pcnt, (format|FMT_NOTABLE));
1253 fputs("packets, ", stdout);
1254 print_num(counters.bcnt, (format|FMT_NOTABLE));
1255 fputs("bytes", stdout);
1256 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001257 printf(")\n");
1258 } else {
1259 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001260 if (!iptc_get_references(&refs, chain, handle))
1261 printf(" (ERROR obtaining refs)\n");
1262 else
1263 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001264 }
1265
1266 if (format & FMT_LINENUMBERS)
1267 printf(FMT("%-4s ", "%s "), "num");
1268 if (!(format & FMT_NOCOUNTS)) {
1269 if (format & FMT_KILOMEGAGIGA) {
1270 printf(FMT("%5s ","%s "), "pkts");
1271 printf(FMT("%5s ","%s "), "bytes");
1272 } else {
1273 printf(FMT("%8s ","%s "), "pkts");
1274 printf(FMT("%10s ","%s "), "bytes");
1275 }
1276 }
1277 if (!(format & FMT_NOTARGET))
1278 printf(FMT("%-9s ","%s "), "target");
1279 fputs(" prot ", stdout);
1280 if (format & FMT_OPTIONS)
1281 fputs("opt", stdout);
1282 if (format & FMT_VIA) {
1283 printf(FMT(" %-6s ","%s "), "in");
1284 printf(FMT("%-6s ","%s "), "out");
1285 }
1286 printf(FMT(" %-19s ","%s "), "source");
1287 printf(FMT(" %-19s "," %s "), "destination");
1288 printf("\n");
1289}
1290
Marc Bouchere6869a82000-03-20 06:03:29 +00001291
1292static int
1293print_match(const struct ipt_entry_match *m,
1294 const struct ipt_ip *ip,
1295 int numeric)
1296{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001297 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001298
1299 if (match) {
1300 if (match->print)
1301 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001302 else
Rusty Russellb039b022000-09-01 06:04:05 +00001303 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001304 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001305 if (m->u.user.name[0])
1306 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001307 }
1308 /* Don't stop iterating. */
1309 return 0;
1310}
1311
1312/* e is called `fw' here for hysterical raisins */
1313static void
1314print_firewall(const struct ipt_entry *fw,
1315 const char *targname,
1316 unsigned int num,
1317 unsigned int format,
1318 const iptc_handle_t handle)
1319{
1320 struct iptables_target *target = NULL;
1321 const struct ipt_entry_target *t;
1322 u_int8_t flags;
1323 char buf[BUFSIZ];
1324
Marc Bouchere6869a82000-03-20 06:03:29 +00001325 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001326 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 else
Rusty Russell52a51492000-05-02 16:44:29 +00001328 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001329
1330 t = ipt_get_target((struct ipt_entry *)fw);
1331 flags = fw->ip.flags;
1332
1333 if (format & FMT_LINENUMBERS)
1334 printf(FMT("%-4u ", "%u "), num+1);
1335
1336 if (!(format & FMT_NOCOUNTS)) {
1337 print_num(fw->counters.pcnt, format);
1338 print_num(fw->counters.bcnt, format);
1339 }
1340
1341 if (!(format & FMT_NOTARGET))
1342 printf(FMT("%-9s ", "%s "), targname);
1343
1344 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1345 {
Rusty Russell28381a42000-05-10 00:19:50 +00001346 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 if (pname)
1348 printf(FMT("%-5s", "%s "), pname);
1349 else
1350 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1351 }
1352
1353 if (format & FMT_OPTIONS) {
1354 if (format & FMT_NOTABLE)
1355 fputs("opt ", stdout);
1356 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1357 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1358 fputc(' ', stdout);
1359 }
1360
1361 if (format & FMT_VIA) {
1362 char iface[IFNAMSIZ+2];
1363
1364 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1365 iface[0] = '!';
1366 iface[1] = '\0';
1367 }
1368 else iface[0] = '\0';
1369
1370 if (fw->ip.iniface[0] != '\0') {
1371 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001372 }
1373 else if (format & FMT_NUMERIC) strcat(iface, "*");
1374 else strcat(iface, "any");
1375 printf(FMT(" %-6s ","in %s "), iface);
1376
1377 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1378 iface[0] = '!';
1379 iface[1] = '\0';
1380 }
1381 else iface[0] = '\0';
1382
1383 if (fw->ip.outiface[0] != '\0') {
1384 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001385 }
1386 else if (format & FMT_NUMERIC) strcat(iface, "*");
1387 else strcat(iface, "any");
1388 printf(FMT("%-6s ","out %s "), iface);
1389 }
1390
1391 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1392 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1393 printf(FMT("%-19s ","%s "), "anywhere");
1394 else {
1395 if (format & FMT_NUMERIC)
1396 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1397 else
1398 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1399 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1400 printf(FMT("%-19s ","%s "), buf);
1401 }
1402
1403 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1404 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001405 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001406 else {
1407 if (format & FMT_NUMERIC)
1408 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1409 else
1410 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1411 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001412 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001413 }
1414
1415 if (format & FMT_NOTABLE)
1416 fputs(" ", stdout);
1417
Harald Welte72bd87e2005-11-24 17:04:05 +00001418#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001419 if(fw->ip.flags & IPT_F_GOTO)
1420 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001421#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001422
Marc Bouchere6869a82000-03-20 06:03:29 +00001423 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1424
1425 if (target) {
1426 if (target->print)
1427 /* Print the target information. */
1428 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001429 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001431 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001432
1433 if (!(format & FMT_NONEWLINE))
1434 fputc('\n', stdout);
1435}
1436
1437static void
1438print_firewall_line(const struct ipt_entry *fw,
1439 const iptc_handle_t h)
1440{
1441 struct ipt_entry_target *t;
1442
1443 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001444 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001445}
1446
1447static int
1448append_entry(const ipt_chainlabel chain,
1449 struct ipt_entry *fw,
1450 unsigned int nsaddrs,
1451 const struct in_addr saddrs[],
1452 unsigned int ndaddrs,
1453 const struct in_addr daddrs[],
1454 int verbose,
1455 iptc_handle_t *handle)
1456{
1457 unsigned int i, j;
1458 int ret = 1;
1459
1460 for (i = 0; i < nsaddrs; i++) {
1461 fw->ip.src.s_addr = saddrs[i].s_addr;
1462 for (j = 0; j < ndaddrs; j++) {
1463 fw->ip.dst.s_addr = daddrs[j].s_addr;
1464 if (verbose)
1465 print_firewall_line(fw, *handle);
1466 ret &= iptc_append_entry(chain, fw, handle);
1467 }
1468 }
1469
1470 return ret;
1471}
1472
1473static int
1474replace_entry(const ipt_chainlabel chain,
1475 struct ipt_entry *fw,
1476 unsigned int rulenum,
1477 const struct in_addr *saddr,
1478 const struct in_addr *daddr,
1479 int verbose,
1480 iptc_handle_t *handle)
1481{
1482 fw->ip.src.s_addr = saddr->s_addr;
1483 fw->ip.dst.s_addr = daddr->s_addr;
1484
1485 if (verbose)
1486 print_firewall_line(fw, *handle);
1487 return iptc_replace_entry(chain, fw, rulenum, handle);
1488}
1489
1490static int
1491insert_entry(const ipt_chainlabel chain,
1492 struct ipt_entry *fw,
1493 unsigned int rulenum,
1494 unsigned int nsaddrs,
1495 const struct in_addr saddrs[],
1496 unsigned int ndaddrs,
1497 const struct in_addr daddrs[],
1498 int verbose,
1499 iptc_handle_t *handle)
1500{
1501 unsigned int i, j;
1502 int ret = 1;
1503
1504 for (i = 0; i < nsaddrs; i++) {
1505 fw->ip.src.s_addr = saddrs[i].s_addr;
1506 for (j = 0; j < ndaddrs; j++) {
1507 fw->ip.dst.s_addr = daddrs[j].s_addr;
1508 if (verbose)
1509 print_firewall_line(fw, *handle);
1510 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1511 }
1512 }
1513
1514 return ret;
1515}
1516
Rusty Russell2e0a3212000-04-19 11:23:18 +00001517static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001518make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001519{
1520 /* Establish mask for comparison */
1521 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001522 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001523 unsigned char *mask, *mptr;
1524
1525 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001526 for (matchp = matches; matchp; matchp = matchp->next)
1527 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001528
Rusty Russell9e1d2142000-04-23 09:11:12 +00001529 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001530 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001531 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001532
Rusty Russell9e1d2142000-04-23 09:11:12 +00001533 memset(mask, 0xFF, sizeof(struct ipt_entry));
1534 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001535
Martin Josefsson78cafda2004-02-02 20:01:18 +00001536 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001537 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001538 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001539 + matchp->match->userspacesize);
1540 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001541 }
1542
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001543 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001544 IPT_ALIGN(sizeof(struct ipt_entry_target))
1545 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001546
1547 return mask;
1548}
1549
Marc Bouchere6869a82000-03-20 06:03:29 +00001550static int
1551delete_entry(const ipt_chainlabel chain,
1552 struct ipt_entry *fw,
1553 unsigned int nsaddrs,
1554 const struct in_addr saddrs[],
1555 unsigned int ndaddrs,
1556 const struct in_addr daddrs[],
1557 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001558 iptc_handle_t *handle,
1559 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001560{
1561 unsigned int i, j;
1562 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001563 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001564
Martin Josefsson78cafda2004-02-02 20:01:18 +00001565 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001566 for (i = 0; i < nsaddrs; i++) {
1567 fw->ip.src.s_addr = saddrs[i].s_addr;
1568 for (j = 0; j < ndaddrs; j++) {
1569 fw->ip.dst.s_addr = daddrs[j].s_addr;
1570 if (verbose)
1571 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001572 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001573 }
1574 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001575 free(mask);
1576
Marc Bouchere6869a82000-03-20 06:03:29 +00001577 return ret;
1578}
1579
Harald Welteae1ff9f2000-12-01 14:26:20 +00001580int
Marc Bouchere6869a82000-03-20 06:03:29 +00001581for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001582 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001583{
1584 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001585 const char *chain;
1586 char *chains;
1587 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001588
Rusty Russell9e1d2142000-04-23 09:11:12 +00001589 chain = iptc_first_chain(handle);
1590 while (chain) {
1591 chaincount++;
1592 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001593 }
1594
Rusty Russell9e1d2142000-04-23 09:11:12 +00001595 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1596 i = 0;
1597 chain = iptc_first_chain(handle);
1598 while (chain) {
1599 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1600 i++;
1601 chain = iptc_next_chain(handle);
1602 }
1603
1604 for (i = 0; i < chaincount; i++) {
1605 if (!builtinstoo
1606 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001607 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001608 continue;
1609 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1610 }
1611
1612 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001613 return ret;
1614}
1615
Harald Welteae1ff9f2000-12-01 14:26:20 +00001616int
Marc Bouchere6869a82000-03-20 06:03:29 +00001617flush_entries(const ipt_chainlabel chain, int verbose,
1618 iptc_handle_t *handle)
1619{
1620 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001621 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001622
1623 if (verbose)
1624 fprintf(stdout, "Flushing chain `%s'\n", chain);
1625 return iptc_flush_entries(chain, handle);
1626}
Marc Bouchere6869a82000-03-20 06:03:29 +00001627
1628static int
1629zero_entries(const ipt_chainlabel chain, int verbose,
1630 iptc_handle_t *handle)
1631{
1632 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001633 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001634
Marc Bouchere6869a82000-03-20 06:03:29 +00001635 if (verbose)
1636 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1637 return iptc_zero_entries(chain, handle);
1638}
1639
Harald Welteae1ff9f2000-12-01 14:26:20 +00001640int
Marc Bouchere6869a82000-03-20 06:03:29 +00001641delete_chain(const ipt_chainlabel chain, int verbose,
1642 iptc_handle_t *handle)
1643{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001644 if (!chain)
1645 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001646
1647 if (verbose)
1648 fprintf(stdout, "Deleting chain `%s'\n", chain);
1649 return iptc_delete_chain(chain, handle);
1650}
1651
1652static int
1653list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1654 int expanded, int linenumbers, iptc_handle_t *handle)
1655{
1656 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001657 unsigned int format;
1658 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001659
1660 format = FMT_OPTIONS;
1661 if (!verbose)
1662 format |= FMT_NOCOUNTS;
1663 else
1664 format |= FMT_VIA;
1665
1666 if (numeric)
1667 format |= FMT_NUMERIC;
1668
1669 if (!expanded)
1670 format |= FMT_KILOMEGAGIGA;
1671
1672 if (linenumbers)
1673 format |= FMT_LINENUMBERS;
1674
Rusty Russell9e1d2142000-04-23 09:11:12 +00001675 for (this = iptc_first_chain(handle);
1676 this;
1677 this = iptc_next_chain(handle)) {
1678 const struct ipt_entry *i;
1679 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001680
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 if (chain && strcmp(chain, this) != 0)
1682 continue;
1683
1684 if (found) printf("\n");
1685
1686 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001687 i = iptc_first_rule(this, handle);
1688
1689 num = 0;
1690 while (i) {
1691 print_firewall(i,
1692 iptc_get_target(i, handle),
1693 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001694 format,
1695 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001696 i = iptc_next_rule(i, handle);
1697 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 found = 1;
1699 }
1700
1701 errno = ENOENT;
1702 return found;
1703}
1704
Harald Welte82dd2ec2000-12-19 05:18:15 +00001705static char *get_modprobe(void)
1706{
1707 int procfile;
1708 char *ret;
1709
Harald Welte10f7f142004-10-22 08:14:07 +00001710#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001711 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1712 if (procfile < 0)
1713 return NULL;
1714
Harald Welte10f7f142004-10-22 08:14:07 +00001715 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001716 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001717 memset(ret, 0, PROCFILE_BUFSIZ);
1718 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001719 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001720 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001721 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001722 if (ret[strlen(ret)-1]=='\n')
1723 ret[strlen(ret)-1]=0;
1724 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001725 return ret;
1726 }
1727 fail:
1728 free(ret);
1729 close(procfile);
1730 return NULL;
1731}
1732
Harald Welte58918652001-06-16 18:25:25 +00001733int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001734{
1735 char *buf = NULL;
1736 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001737 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001738
1739 /* If they don't explicitly set it, read out of kernel */
1740 if (!modprobe) {
1741 buf = get_modprobe();
1742 if (!buf)
1743 return -1;
1744 modprobe = buf;
1745 }
1746
1747 switch (fork()) {
1748 case 0:
1749 argv[0] = (char *)modprobe;
1750 argv[1] = (char *)modname;
1751 argv[2] = NULL;
1752 execv(argv[0], argv);
1753
1754 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001755 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001756 case -1:
1757 return -1;
1758
1759 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001760 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001761 }
1762
1763 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001764 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1765 return 0;
1766 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001767}
1768
Marc Bouchere6869a82000-03-20 06:03:29 +00001769static struct ipt_entry *
1770generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001771 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001772 struct ipt_entry_target *target)
1773{
1774 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001775 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001776 struct ipt_entry *e;
1777
1778 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001779 for (matchp = matches; matchp; matchp = matchp->next)
1780 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001781
Rusty Russell228e98d2000-04-27 10:28:06 +00001782 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001783 *e = *fw;
1784 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001785 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001786
1787 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001788 for (matchp = matches; matchp; matchp = matchp->next) {
1789 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1790 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001791 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001792 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001793
1794 return e;
1795}
1796
Martin Josefsson78cafda2004-02-02 20:01:18 +00001797void clear_rule_matches(struct iptables_rule_match **matches)
1798{
1799 struct iptables_rule_match *matchp, *tmp;
1800
1801 for (matchp = *matches; matchp;) {
1802 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001803 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001804 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001805 matchp->match->m = NULL;
1806 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001807 free(matchp);
1808 matchp = tmp;
1809 }
1810
1811 *matches = NULL;
1812}
1813
Rusty Russell3aef54d2005-01-03 03:48:40 +00001814static void set_revision(char *name, u_int8_t revision)
1815{
1816 /* Old kernel sources don't have ".revision" field,
1817 but we stole a byte from name. */
1818 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1819 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1820}
1821
Phil Oester8cf65912005-09-19 15:00:33 +00001822void
1823get_kernel_version(void) {
1824 static struct utsname uts;
1825 int x = 0, y = 0, z = 0;
1826
1827 if (uname(&uts) == -1) {
1828 fprintf(stderr, "Unable to retrieve kernel version.\n");
1829 free_opts(1);
1830 exit(1);
1831 }
1832
1833 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1834 kernel_version = LINUX_VERSION(x, y, z);
1835}
1836
Marc Bouchere6869a82000-03-20 06:03:29 +00001837int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1838{
1839 struct ipt_entry fw, *e = NULL;
1840 int invert = 0;
1841 unsigned int nsaddrs = 0, ndaddrs = 0;
1842 struct in_addr *saddrs = NULL, *daddrs = NULL;
1843
1844 int c, verbose = 0;
1845 const char *chain = NULL;
1846 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1847 const char *policy = NULL, *newname = NULL;
1848 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001849 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 int ret = 1;
1851 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001852 struct iptables_rule_match *matches = NULL;
1853 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001854 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001855 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 const char *jumpto = "";
1857 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001858 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001859 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001860
1861 memset(&fw, 0, sizeof(fw));
1862
Harald Welteae1ff9f2000-12-01 14:26:20 +00001863 /* re-set optind to 0 in case do_command gets called
1864 * a second time */
1865 optind = 0;
1866
1867 /* clear mflags in case do_command gets called a second time
1868 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001869 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001870 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001871
1872 for (t = iptables_targets; t; t = t->next) {
1873 t->tflags = 0;
1874 t->used = 0;
1875 }
1876
Marc Bouchere6869a82000-03-20 06:03:29 +00001877 /* Suppress error messages: we may add new options if we
1878 demand-load a protocol. */
1879 opterr = 0;
1880
1881 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001882 "-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 +00001883 opts, NULL)) != -1) {
1884 switch (c) {
1885 /*
1886 * Command selection
1887 */
1888 case 'A':
1889 add_command(&command, CMD_APPEND, CMD_NONE,
1890 invert);
1891 chain = optarg;
1892 break;
1893
1894 case 'D':
1895 add_command(&command, CMD_DELETE, CMD_NONE,
1896 invert);
1897 chain = optarg;
1898 if (optind < argc && argv[optind][0] != '-'
1899 && argv[optind][0] != '!') {
1900 rulenum = parse_rulenumber(argv[optind++]);
1901 command = CMD_DELETE_NUM;
1902 }
1903 break;
1904
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 case 'R':
1906 add_command(&command, CMD_REPLACE, CMD_NONE,
1907 invert);
1908 chain = optarg;
1909 if (optind < argc && argv[optind][0] != '-'
1910 && argv[optind][0] != '!')
1911 rulenum = parse_rulenumber(argv[optind++]);
1912 else
1913 exit_error(PARAMETER_PROBLEM,
1914 "-%c requires a rule number",
1915 cmd2char(CMD_REPLACE));
1916 break;
1917
1918 case 'I':
1919 add_command(&command, CMD_INSERT, CMD_NONE,
1920 invert);
1921 chain = optarg;
1922 if (optind < argc && argv[optind][0] != '-'
1923 && argv[optind][0] != '!')
1924 rulenum = parse_rulenumber(argv[optind++]);
1925 else rulenum = 1;
1926 break;
1927
1928 case 'L':
1929 add_command(&command, CMD_LIST, CMD_ZERO,
1930 invert);
1931 if (optarg) chain = optarg;
1932 else if (optind < argc && argv[optind][0] != '-'
1933 && argv[optind][0] != '!')
1934 chain = argv[optind++];
1935 break;
1936
1937 case 'F':
1938 add_command(&command, CMD_FLUSH, CMD_NONE,
1939 invert);
1940 if (optarg) chain = optarg;
1941 else if (optind < argc && argv[optind][0] != '-'
1942 && argv[optind][0] != '!')
1943 chain = argv[optind++];
1944 break;
1945
1946 case 'Z':
1947 add_command(&command, CMD_ZERO, CMD_LIST,
1948 invert);
1949 if (optarg) chain = optarg;
1950 else if (optind < argc && argv[optind][0] != '-'
1951 && argv[optind][0] != '!')
1952 chain = argv[optind++];
1953 break;
1954
1955 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001956 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001957 exit_error(PARAMETER_PROBLEM,
1958 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001959 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001960 if (find_target(optarg, TRY_LOAD))
1961 exit_error(PARAMETER_PROBLEM,
1962 "chain name may not clash "
1963 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001964 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1965 invert);
1966 chain = optarg;
1967 break;
1968
1969 case 'X':
1970 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1971 invert);
1972 if (optarg) chain = optarg;
1973 else if (optind < argc && argv[optind][0] != '-'
1974 && argv[optind][0] != '!')
1975 chain = argv[optind++];
1976 break;
1977
1978 case 'E':
1979 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1980 invert);
1981 chain = optarg;
1982 if (optind < argc && argv[optind][0] != '-'
1983 && argv[optind][0] != '!')
1984 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001985 else
1986 exit_error(PARAMETER_PROBLEM,
1987 "-%c requires old-chain-name and "
1988 "new-chain-name",
1989 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001990 break;
1991
1992 case 'P':
1993 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1994 invert);
1995 chain = optarg;
1996 if (optind < argc && argv[optind][0] != '-'
1997 && argv[optind][0] != '!')
1998 policy = argv[optind++];
1999 else
2000 exit_error(PARAMETER_PROBLEM,
2001 "-%c requires a chain and a policy",
2002 cmd2char(CMD_SET_POLICY));
2003 break;
2004
2005 case 'h':
2006 if (!optarg)
2007 optarg = argv[optind];
2008
Rusty Russell2e0a3212000-04-19 11:23:18 +00002009 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002010 if (!matches && protocol)
2011 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002012
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002013 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002014
2015 /*
2016 * Option selection
2017 */
2018 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002019 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2021 invert);
2022
2023 /* Canonicalize into lower case */
2024 for (protocol = argv[optind-1]; *protocol; protocol++)
2025 *protocol = tolower(*protocol);
2026
2027 protocol = argv[optind-1];
2028 fw.ip.proto = parse_protocol(protocol);
2029
2030 if (fw.ip.proto == 0
2031 && (fw.ip.invflags & IPT_INV_PROTO))
2032 exit_error(PARAMETER_PROBLEM,
2033 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002034 break;
2035
2036 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002037 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2039 invert);
2040 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 break;
2042
2043 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002044 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002045 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2046 invert);
2047 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002048 break;
2049
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002050#ifdef IPT_F_GOTO
2051 case 'g':
2052 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2053 invert);
2054 fw.ip.flags |= IPT_F_GOTO;
2055 jumpto = parse_target(optarg);
2056 break;
2057#endif
2058
Marc Bouchere6869a82000-03-20 06:03:29 +00002059 case 'j':
2060 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2061 invert);
2062 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002063 /* TRY_LOAD (may be chain name) */
2064 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002065
2066 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002067 size_t size;
2068
Rusty Russell73f72f52000-07-03 10:17:57 +00002069 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2070 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002071
Rusty Russell2e0a3212000-04-19 11:23:18 +00002072 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002073 target->t->u.target_size = size;
2074 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002075 set_revision(target->t->u.user.name,
2076 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002077 if (target->init != NULL)
2078 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002079 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002080 }
2081 break;
2082
2083
2084 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002085 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002086 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2087 invert);
2088 parse_interface(argv[optind-1],
2089 fw.ip.iniface,
2090 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002091 break;
2092
2093 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002094 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002095 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2096 invert);
2097 parse_interface(argv[optind-1],
2098 fw.ip.outiface,
2099 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002100 break;
2101
2102 case 'f':
2103 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2104 invert);
2105 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002106 break;
2107
2108 case 'v':
2109 if (!verbose)
2110 set_option(&options, OPT_VERBOSE,
2111 &fw.ip.invflags, invert);
2112 verbose++;
2113 break;
2114
Rusty Russell52a51492000-05-02 16:44:29 +00002115 case 'm': {
2116 size_t size;
2117
Marc Bouchere6869a82000-03-20 06:03:29 +00002118 if (invert)
2119 exit_error(PARAMETER_PROBLEM,
2120 "unexpected ! flag before --match");
2121
Martin Josefsson78cafda2004-02-02 20:01:18 +00002122 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002123 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2124 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002125 m->m = fw_calloc(1, size);
2126 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002127 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002128 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002129 if (m->init != NULL)
2130 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002131 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002132 }
2133 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002134
2135 case 'n':
2136 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2137 invert);
2138 break;
2139
2140 case 't':
2141 if (invert)
2142 exit_error(PARAMETER_PROBLEM,
2143 "unexpected ! flag before --table");
2144 *table = argv[optind-1];
2145 break;
2146
2147 case 'x':
2148 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2149 invert);
2150 break;
2151
2152 case 'V':
2153 if (invert)
2154 printf("Not %s ;-)\n", program_version);
2155 else
2156 printf("%s v%s\n",
2157 program_name, program_version);
2158 exit(0);
2159
2160 case '0':
2161 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2162 invert);
2163 break;
2164
Harald Welte82dd2ec2000-12-19 05:18:15 +00002165 case 'M':
2166 modprobe = optarg;
2167 break;
2168
Harald Welteccd49e52001-01-23 22:54:34 +00002169 case 'c':
2170
2171 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2172 invert);
2173 pcnt = optarg;
2174 if (optind < argc && argv[optind][0] != '-'
2175 && argv[optind][0] != '!')
2176 bcnt = argv[optind++];
2177 else
2178 exit_error(PARAMETER_PROBLEM,
2179 "-%c requires packet and byte counter",
2180 opt2char(OPT_COUNTERS));
2181
Martin Josefssona28d4952004-05-26 16:04:48 +00002182 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002183 exit_error(PARAMETER_PROBLEM,
2184 "-%c packet counter not numeric",
2185 opt2char(OPT_COUNTERS));
2186
Martin Josefssona28d4952004-05-26 16:04:48 +00002187 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002188 exit_error(PARAMETER_PROBLEM,
2189 "-%c byte counter not numeric",
2190 opt2char(OPT_COUNTERS));
2191
2192 break;
2193
2194
Marc Bouchere6869a82000-03-20 06:03:29 +00002195 case 1: /* non option */
2196 if (optarg[0] == '!' && optarg[1] == '\0') {
2197 if (invert)
2198 exit_error(PARAMETER_PROBLEM,
2199 "multiple consecutive ! not"
2200 " allowed");
2201 invert = TRUE;
2202 optarg[0] = '\0';
2203 continue;
2204 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002205 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002206 exit_tryhelp(2);
2207
2208 default:
2209 /* FIXME: This scheme doesn't allow two of the same
2210 matches --RR */
2211 if (!target
2212 || !(target->parse(c - target->option_offset,
2213 argv, invert,
2214 &target->tflags,
2215 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002216 for (matchp = matches; matchp; matchp = matchp->next) {
2217 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002218 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002219 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002220 &fw,
2221 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002222 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002223 break;
2224 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002225 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002226
2227 /* If you listen carefully, you can
2228 actually hear this code suck. */
2229
2230 /* some explanations (after four different bugs
2231 * in 3 different releases): If we encountere a
2232 * parameter, that has not been parsed yet,
2233 * it's not an option of an explicitly loaded
2234 * match or a target. However, we support
2235 * implicit loading of the protocol match
2236 * extension. '-p tcp' means 'l4 proto 6' and
2237 * at the same time 'load tcp protocol match on
2238 * demand if we specify --dport'.
2239 *
2240 * To make this work, we need to make sure:
2241 * - the parameter has not been parsed by
2242 * a match (m above)
2243 * - a protocol has been specified
2244 * - the protocol extension has not been
2245 * loaded yet, or is loaded and unused
2246 * [think of iptables-restore!]
2247 * - the protocol extension can be successively
2248 * loaded
2249 */
2250 if (m == NULL
2251 && protocol
2252 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002253 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002254 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002255 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002256 && (proto_used == 0))
2257 )
2258 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002259 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002260 /* Try loading protocol */
2261 size_t size;
2262
2263 proto_used = 1;
2264
2265 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2266 + m->size;
2267
2268 m->m = fw_calloc(1, size);
2269 m->m->u.match_size = size;
2270 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002271 set_revision(m->m->u.user.name,
2272 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002273 if (m->init != NULL)
2274 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002275
2276 opts = merge_options(opts,
2277 m->extra_opts, &m->option_offset);
2278
2279 optind--;
2280 continue;
2281 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002282 if (!m)
2283 exit_error(PARAMETER_PROBLEM,
2284 "Unknown arg `%s'",
2285 argv[optind-1]);
2286 }
2287 }
2288 invert = FALSE;
2289 }
2290
Martin Josefsson78cafda2004-02-02 20:01:18 +00002291 for (matchp = matches; matchp; matchp = matchp->next)
2292 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002293
Marc Bouchere6869a82000-03-20 06:03:29 +00002294 if (target)
2295 target->final_check(target->tflags);
2296
2297 /* Fix me: must put inverse options checking here --MN */
2298
2299 if (optind < argc)
2300 exit_error(PARAMETER_PROBLEM,
2301 "unknown arguments found on commandline");
2302 if (!command)
2303 exit_error(PARAMETER_PROBLEM, "no command specified");
2304 if (invert)
2305 exit_error(PARAMETER_PROBLEM,
2306 "nothing appropriate following !");
2307
Harald Welte6336bfd2002-05-07 14:41:43 +00002308 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002309 if (!(options & OPT_DESTINATION))
2310 dhostnetworkmask = "0.0.0.0/0";
2311 if (!(options & OPT_SOURCE))
2312 shostnetworkmask = "0.0.0.0/0";
2313 }
2314
2315 if (shostnetworkmask)
2316 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2317 &(fw.ip.smsk), &nsaddrs);
2318
2319 if (dhostnetworkmask)
2320 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2321 &(fw.ip.dmsk), &ndaddrs);
2322
2323 if ((nsaddrs > 1 || ndaddrs > 1) &&
2324 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2325 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2326 " source or destination IP addresses");
2327
Marc Bouchere6869a82000-03-20 06:03:29 +00002328 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2329 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2330 "specify a unique address");
2331
2332 generic_opt_check(command, options);
2333
2334 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2335 exit_error(PARAMETER_PROBLEM,
2336 "chain name `%s' too long (must be under %i chars)",
2337 chain, IPT_FUNCTION_MAXNAMELEN);
2338
Harald Welteae1ff9f2000-12-01 14:26:20 +00002339 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002340 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002341 *handle = iptc_init(*table);
2342
Rusty Russell8beb0492004-12-22 00:37:10 +00002343 /* try to insmod the module if iptc_init failed */
2344 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002345 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002346
Marc Bouchere6869a82000-03-20 06:03:29 +00002347 if (!*handle)
2348 exit_error(VERSION_PROBLEM,
2349 "can't initialize iptables table `%s': %s",
2350 *table, iptc_strerror(errno));
2351
Harald Welte6336bfd2002-05-07 14:41:43 +00002352 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002353 || command == CMD_DELETE
2354 || command == CMD_INSERT
2355 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002356 if (strcmp(chain, "PREROUTING") == 0
2357 || strcmp(chain, "INPUT") == 0) {
2358 /* -o not valid with incoming packets. */
2359 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002360 exit_error(PARAMETER_PROBLEM,
2361 "Can't use -%c with %s\n",
2362 opt2char(OPT_VIANAMEOUT),
2363 chain);
2364 }
2365
Rusty Russella4860fd2000-06-17 16:13:02 +00002366 if (strcmp(chain, "POSTROUTING") == 0
2367 || strcmp(chain, "OUTPUT") == 0) {
2368 /* -i not valid with outgoing packets */
2369 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002370 exit_error(PARAMETER_PROBLEM,
2371 "Can't use -%c with %s\n",
2372 opt2char(OPT_VIANAMEIN),
2373 chain);
2374 }
2375
2376 if (target && iptc_is_chain(jumpto, *handle)) {
2377 printf("Warning: using chain %s, not extension\n",
2378 jumpto);
2379
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002380 if (target->t)
2381 free(target->t);
2382
Marc Bouchere6869a82000-03-20 06:03:29 +00002383 target = NULL;
2384 }
2385
2386 /* If they didn't specify a target, or it's a chain
2387 name, use standard. */
2388 if (!target
2389 && (strlen(jumpto) == 0
2390 || iptc_is_chain(jumpto, *handle))) {
2391 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002392
Rusty Russell52a51492000-05-02 16:44:29 +00002393 target = find_target(IPT_STANDARD_TARGET,
2394 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002395
2396 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002397 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002398 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002399 target->t->u.target_size = size;
2400 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002401 if (!iptc_is_chain(jumpto, *handle))
2402 set_revision(target->t->u.user.name,
2403 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002404 if (target->init != NULL)
2405 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002406 }
2407
Rusty Russell7e53bf92000-03-20 07:03:28 +00002408 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002409 /* it is no chain, and we can't load a plugin.
2410 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002411 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002412 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002413#ifdef IPT_F_GOTO
2414 if (fw.ip.flags & IPT_F_GOTO)
2415 exit_error(PARAMETER_PROBLEM,
2416 "goto '%s' is not a chain\n", jumpto);
2417#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002418 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002419 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002420 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002421 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002422 }
2423 }
2424
2425 switch (command) {
2426 case CMD_APPEND:
2427 ret = append_entry(chain, e,
2428 nsaddrs, saddrs, ndaddrs, daddrs,
2429 options&OPT_VERBOSE,
2430 handle);
2431 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002432 case CMD_DELETE:
2433 ret = delete_entry(chain, e,
2434 nsaddrs, saddrs, ndaddrs, daddrs,
2435 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002436 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002437 break;
2438 case CMD_DELETE_NUM:
2439 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2440 break;
2441 case CMD_REPLACE:
2442 ret = replace_entry(chain, e, rulenum - 1,
2443 saddrs, daddrs, options&OPT_VERBOSE,
2444 handle);
2445 break;
2446 case CMD_INSERT:
2447 ret = insert_entry(chain, e, rulenum - 1,
2448 nsaddrs, saddrs, ndaddrs, daddrs,
2449 options&OPT_VERBOSE,
2450 handle);
2451 break;
2452 case CMD_LIST:
2453 ret = list_entries(chain,
2454 options&OPT_VERBOSE,
2455 options&OPT_NUMERIC,
2456 options&OPT_EXPANDED,
2457 options&OPT_LINENUMBERS,
2458 handle);
2459 break;
2460 case CMD_FLUSH:
2461 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2462 break;
2463 case CMD_ZERO:
2464 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2465 break;
2466 case CMD_LIST|CMD_ZERO:
2467 ret = list_entries(chain,
2468 options&OPT_VERBOSE,
2469 options&OPT_NUMERIC,
2470 options&OPT_EXPANDED,
2471 options&OPT_LINENUMBERS,
2472 handle);
2473 if (ret)
2474 ret = zero_entries(chain,
2475 options&OPT_VERBOSE, handle);
2476 break;
2477 case CMD_NEW_CHAIN:
2478 ret = iptc_create_chain(chain, handle);
2479 break;
2480 case CMD_DELETE_CHAIN:
2481 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2482 break;
2483 case CMD_RENAME_CHAIN:
2484 ret = iptc_rename_chain(chain, newname, handle);
2485 break;
2486 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002487 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002488 break;
2489 default:
2490 /* We should never reach this... */
2491 exit_tryhelp(2);
2492 }
2493
2494 if (verbose > 1)
2495 dump_entries(*handle);
2496
Martin Josefsson78cafda2004-02-02 20:01:18 +00002497 clear_rule_matches(&matches);
2498
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002499 if (e != NULL) {
2500 free(e);
2501 e = NULL;
2502 }
2503
keso6997cdf2004-07-04 15:20:53 +00002504 free(saddrs);
2505 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002506 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002507
Marc Bouchere6869a82000-03-20 06:03:29 +00002508 return ret;
2509}