blob: cc47e9808f4138c277be2ad5c6f033d7a455214e [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
Rusty Russell52a51492000-05-02 16:44:29 +0000688 if (!ptr && tryload != DONT_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
Rusty Russell52a51492000-05-02 16:44:29 +0000997 if (!ptr && tryload != DONT_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
Rusty Russell3aef54d2005-01-03 03:48:40 +00001122 old = find_match(me->name, DONT_LOAD, NULL);
1123 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
Rusty Russell3aef54d2005-01-03 03:48:40 +00001178 old = find_target(me->name, DONT_LOAD);
1179 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
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001418 if(fw->ip.flags & IPT_F_GOTO)
1419 printf("[goto] ");
1420
Marc Bouchere6869a82000-03-20 06:03:29 +00001421 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1422
1423 if (target) {
1424 if (target->print)
1425 /* Print the target information. */
1426 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001427 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001428 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001429 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001430
1431 if (!(format & FMT_NONEWLINE))
1432 fputc('\n', stdout);
1433}
1434
1435static void
1436print_firewall_line(const struct ipt_entry *fw,
1437 const iptc_handle_t h)
1438{
1439 struct ipt_entry_target *t;
1440
1441 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001442 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001443}
1444
1445static int
1446append_entry(const ipt_chainlabel chain,
1447 struct ipt_entry *fw,
1448 unsigned int nsaddrs,
1449 const struct in_addr saddrs[],
1450 unsigned int ndaddrs,
1451 const struct in_addr daddrs[],
1452 int verbose,
1453 iptc_handle_t *handle)
1454{
1455 unsigned int i, j;
1456 int ret = 1;
1457
1458 for (i = 0; i < nsaddrs; i++) {
1459 fw->ip.src.s_addr = saddrs[i].s_addr;
1460 for (j = 0; j < ndaddrs; j++) {
1461 fw->ip.dst.s_addr = daddrs[j].s_addr;
1462 if (verbose)
1463 print_firewall_line(fw, *handle);
1464 ret &= iptc_append_entry(chain, fw, handle);
1465 }
1466 }
1467
1468 return ret;
1469}
1470
1471static int
1472replace_entry(const ipt_chainlabel chain,
1473 struct ipt_entry *fw,
1474 unsigned int rulenum,
1475 const struct in_addr *saddr,
1476 const struct in_addr *daddr,
1477 int verbose,
1478 iptc_handle_t *handle)
1479{
1480 fw->ip.src.s_addr = saddr->s_addr;
1481 fw->ip.dst.s_addr = daddr->s_addr;
1482
1483 if (verbose)
1484 print_firewall_line(fw, *handle);
1485 return iptc_replace_entry(chain, fw, rulenum, handle);
1486}
1487
1488static int
1489insert_entry(const ipt_chainlabel chain,
1490 struct ipt_entry *fw,
1491 unsigned int rulenum,
1492 unsigned int nsaddrs,
1493 const struct in_addr saddrs[],
1494 unsigned int ndaddrs,
1495 const struct in_addr daddrs[],
1496 int verbose,
1497 iptc_handle_t *handle)
1498{
1499 unsigned int i, j;
1500 int ret = 1;
1501
1502 for (i = 0; i < nsaddrs; i++) {
1503 fw->ip.src.s_addr = saddrs[i].s_addr;
1504 for (j = 0; j < ndaddrs; j++) {
1505 fw->ip.dst.s_addr = daddrs[j].s_addr;
1506 if (verbose)
1507 print_firewall_line(fw, *handle);
1508 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1509 }
1510 }
1511
1512 return ret;
1513}
1514
Rusty Russell2e0a3212000-04-19 11:23:18 +00001515static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001516make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001517{
1518 /* Establish mask for comparison */
1519 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001520 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001521 unsigned char *mask, *mptr;
1522
1523 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001524 for (matchp = matches; matchp; matchp = matchp->next)
1525 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001526
Rusty Russell9e1d2142000-04-23 09:11:12 +00001527 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001528 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001529 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001530
Rusty Russell9e1d2142000-04-23 09:11:12 +00001531 memset(mask, 0xFF, sizeof(struct ipt_entry));
1532 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001533
Martin Josefsson78cafda2004-02-02 20:01:18 +00001534 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001535 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001536 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001537 + matchp->match->userspacesize);
1538 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001539 }
1540
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001541 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001542 IPT_ALIGN(sizeof(struct ipt_entry_target))
1543 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001544
1545 return mask;
1546}
1547
Marc Bouchere6869a82000-03-20 06:03:29 +00001548static int
1549delete_entry(const ipt_chainlabel chain,
1550 struct ipt_entry *fw,
1551 unsigned int nsaddrs,
1552 const struct in_addr saddrs[],
1553 unsigned int ndaddrs,
1554 const struct in_addr daddrs[],
1555 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001556 iptc_handle_t *handle,
1557 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001558{
1559 unsigned int i, j;
1560 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001561 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001562
Martin Josefsson78cafda2004-02-02 20:01:18 +00001563 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001564 for (i = 0; i < nsaddrs; i++) {
1565 fw->ip.src.s_addr = saddrs[i].s_addr;
1566 for (j = 0; j < ndaddrs; j++) {
1567 fw->ip.dst.s_addr = daddrs[j].s_addr;
1568 if (verbose)
1569 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001570 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001571 }
1572 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001573 free(mask);
1574
Marc Bouchere6869a82000-03-20 06:03:29 +00001575 return ret;
1576}
1577
Harald Welteae1ff9f2000-12-01 14:26:20 +00001578int
Marc Bouchere6869a82000-03-20 06:03:29 +00001579for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001580 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001581{
1582 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001583 const char *chain;
1584 char *chains;
1585 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001586
Rusty Russell9e1d2142000-04-23 09:11:12 +00001587 chain = iptc_first_chain(handle);
1588 while (chain) {
1589 chaincount++;
1590 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001591 }
1592
Rusty Russell9e1d2142000-04-23 09:11:12 +00001593 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1594 i = 0;
1595 chain = iptc_first_chain(handle);
1596 while (chain) {
1597 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1598 i++;
1599 chain = iptc_next_chain(handle);
1600 }
1601
1602 for (i = 0; i < chaincount; i++) {
1603 if (!builtinstoo
1604 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001605 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001606 continue;
1607 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1608 }
1609
1610 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001611 return ret;
1612}
1613
Harald Welteae1ff9f2000-12-01 14:26:20 +00001614int
Marc Bouchere6869a82000-03-20 06:03:29 +00001615flush_entries(const ipt_chainlabel chain, int verbose,
1616 iptc_handle_t *handle)
1617{
1618 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001619 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001620
1621 if (verbose)
1622 fprintf(stdout, "Flushing chain `%s'\n", chain);
1623 return iptc_flush_entries(chain, handle);
1624}
Marc Bouchere6869a82000-03-20 06:03:29 +00001625
1626static int
1627zero_entries(const ipt_chainlabel chain, int verbose,
1628 iptc_handle_t *handle)
1629{
1630 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001631 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001632
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 if (verbose)
1634 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1635 return iptc_zero_entries(chain, handle);
1636}
1637
Harald Welteae1ff9f2000-12-01 14:26:20 +00001638int
Marc Bouchere6869a82000-03-20 06:03:29 +00001639delete_chain(const ipt_chainlabel chain, int verbose,
1640 iptc_handle_t *handle)
1641{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001642 if (!chain)
1643 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001644
1645 if (verbose)
1646 fprintf(stdout, "Deleting chain `%s'\n", chain);
1647 return iptc_delete_chain(chain, handle);
1648}
1649
1650static int
1651list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1652 int expanded, int linenumbers, iptc_handle_t *handle)
1653{
1654 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001655 unsigned int format;
1656 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001657
1658 format = FMT_OPTIONS;
1659 if (!verbose)
1660 format |= FMT_NOCOUNTS;
1661 else
1662 format |= FMT_VIA;
1663
1664 if (numeric)
1665 format |= FMT_NUMERIC;
1666
1667 if (!expanded)
1668 format |= FMT_KILOMEGAGIGA;
1669
1670 if (linenumbers)
1671 format |= FMT_LINENUMBERS;
1672
Rusty Russell9e1d2142000-04-23 09:11:12 +00001673 for (this = iptc_first_chain(handle);
1674 this;
1675 this = iptc_next_chain(handle)) {
1676 const struct ipt_entry *i;
1677 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001678
Marc Bouchere6869a82000-03-20 06:03:29 +00001679 if (chain && strcmp(chain, this) != 0)
1680 continue;
1681
1682 if (found) printf("\n");
1683
1684 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001685 i = iptc_first_rule(this, handle);
1686
1687 num = 0;
1688 while (i) {
1689 print_firewall(i,
1690 iptc_get_target(i, handle),
1691 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001692 format,
1693 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001694 i = iptc_next_rule(i, handle);
1695 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 found = 1;
1697 }
1698
1699 errno = ENOENT;
1700 return found;
1701}
1702
Harald Welte82dd2ec2000-12-19 05:18:15 +00001703static char *get_modprobe(void)
1704{
1705 int procfile;
1706 char *ret;
1707
Harald Welte10f7f142004-10-22 08:14:07 +00001708#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001709 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1710 if (procfile < 0)
1711 return NULL;
1712
Harald Welte10f7f142004-10-22 08:14:07 +00001713 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001714 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001715 memset(ret, 0, PROCFILE_BUFSIZ);
1716 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001717 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001718 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001719 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001720 if (ret[strlen(ret)-1]=='\n')
1721 ret[strlen(ret)-1]=0;
1722 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001723 return ret;
1724 }
1725 fail:
1726 free(ret);
1727 close(procfile);
1728 return NULL;
1729}
1730
Harald Welte58918652001-06-16 18:25:25 +00001731int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001732{
1733 char *buf = NULL;
1734 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001735 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001736
1737 /* If they don't explicitly set it, read out of kernel */
1738 if (!modprobe) {
1739 buf = get_modprobe();
1740 if (!buf)
1741 return -1;
1742 modprobe = buf;
1743 }
1744
1745 switch (fork()) {
1746 case 0:
1747 argv[0] = (char *)modprobe;
1748 argv[1] = (char *)modname;
1749 argv[2] = NULL;
1750 execv(argv[0], argv);
1751
1752 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001753 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001754 case -1:
1755 return -1;
1756
1757 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001758 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001759 }
1760
1761 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001762 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1763 return 0;
1764 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001765}
1766
Marc Bouchere6869a82000-03-20 06:03:29 +00001767static struct ipt_entry *
1768generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001769 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001770 struct ipt_entry_target *target)
1771{
1772 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001773 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 struct ipt_entry *e;
1775
1776 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001777 for (matchp = matches; matchp; matchp = matchp->next)
1778 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001779
Rusty Russell228e98d2000-04-27 10:28:06 +00001780 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 *e = *fw;
1782 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001783 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001784
1785 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001786 for (matchp = matches; matchp; matchp = matchp->next) {
1787 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1788 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001789 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001790 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001791
1792 return e;
1793}
1794
Martin Josefsson78cafda2004-02-02 20:01:18 +00001795void clear_rule_matches(struct iptables_rule_match **matches)
1796{
1797 struct iptables_rule_match *matchp, *tmp;
1798
1799 for (matchp = *matches; matchp;) {
1800 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001801 if (matchp->match->m)
1802 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001803 free(matchp);
1804 matchp = tmp;
1805 }
1806
1807 *matches = NULL;
1808}
1809
Rusty Russell3aef54d2005-01-03 03:48:40 +00001810static void set_revision(char *name, u_int8_t revision)
1811{
1812 /* Old kernel sources don't have ".revision" field,
1813 but we stole a byte from name. */
1814 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1815 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1816}
1817
Phil Oester8cf65912005-09-19 15:00:33 +00001818void
1819get_kernel_version(void) {
1820 static struct utsname uts;
1821 int x = 0, y = 0, z = 0;
1822
1823 if (uname(&uts) == -1) {
1824 fprintf(stderr, "Unable to retrieve kernel version.\n");
1825 free_opts(1);
1826 exit(1);
1827 }
1828
1829 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1830 kernel_version = LINUX_VERSION(x, y, z);
1831}
1832
Marc Bouchere6869a82000-03-20 06:03:29 +00001833int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1834{
1835 struct ipt_entry fw, *e = NULL;
1836 int invert = 0;
1837 unsigned int nsaddrs = 0, ndaddrs = 0;
1838 struct in_addr *saddrs = NULL, *daddrs = NULL;
1839
1840 int c, verbose = 0;
1841 const char *chain = NULL;
1842 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1843 const char *policy = NULL, *newname = NULL;
1844 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001845 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001846 int ret = 1;
1847 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001848 struct iptables_rule_match *matches = NULL;
1849 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001851 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001852 const char *jumpto = "";
1853 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001854 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001855 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856
1857 memset(&fw, 0, sizeof(fw));
1858
Harald Welteae1ff9f2000-12-01 14:26:20 +00001859 /* re-set optind to 0 in case do_command gets called
1860 * a second time */
1861 optind = 0;
1862
1863 /* clear mflags in case do_command gets called a second time
1864 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001865 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001866 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001867
1868 for (t = iptables_targets; t; t = t->next) {
1869 t->tflags = 0;
1870 t->used = 0;
1871 }
1872
Marc Bouchere6869a82000-03-20 06:03:29 +00001873 /* Suppress error messages: we may add new options if we
1874 demand-load a protocol. */
1875 opterr = 0;
1876
1877 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001878 "-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 +00001879 opts, NULL)) != -1) {
1880 switch (c) {
1881 /*
1882 * Command selection
1883 */
1884 case 'A':
1885 add_command(&command, CMD_APPEND, CMD_NONE,
1886 invert);
1887 chain = optarg;
1888 break;
1889
1890 case 'D':
1891 add_command(&command, CMD_DELETE, CMD_NONE,
1892 invert);
1893 chain = optarg;
1894 if (optind < argc && argv[optind][0] != '-'
1895 && argv[optind][0] != '!') {
1896 rulenum = parse_rulenumber(argv[optind++]);
1897 command = CMD_DELETE_NUM;
1898 }
1899 break;
1900
Marc Bouchere6869a82000-03-20 06:03:29 +00001901 case 'R':
1902 add_command(&command, CMD_REPLACE, CMD_NONE,
1903 invert);
1904 chain = optarg;
1905 if (optind < argc && argv[optind][0] != '-'
1906 && argv[optind][0] != '!')
1907 rulenum = parse_rulenumber(argv[optind++]);
1908 else
1909 exit_error(PARAMETER_PROBLEM,
1910 "-%c requires a rule number",
1911 cmd2char(CMD_REPLACE));
1912 break;
1913
1914 case 'I':
1915 add_command(&command, CMD_INSERT, CMD_NONE,
1916 invert);
1917 chain = optarg;
1918 if (optind < argc && argv[optind][0] != '-'
1919 && argv[optind][0] != '!')
1920 rulenum = parse_rulenumber(argv[optind++]);
1921 else rulenum = 1;
1922 break;
1923
1924 case 'L':
1925 add_command(&command, CMD_LIST, CMD_ZERO,
1926 invert);
1927 if (optarg) chain = optarg;
1928 else if (optind < argc && argv[optind][0] != '-'
1929 && argv[optind][0] != '!')
1930 chain = argv[optind++];
1931 break;
1932
1933 case 'F':
1934 add_command(&command, CMD_FLUSH, CMD_NONE,
1935 invert);
1936 if (optarg) chain = optarg;
1937 else if (optind < argc && argv[optind][0] != '-'
1938 && argv[optind][0] != '!')
1939 chain = argv[optind++];
1940 break;
1941
1942 case 'Z':
1943 add_command(&command, CMD_ZERO, CMD_LIST,
1944 invert);
1945 if (optarg) chain = optarg;
1946 else if (optind < argc && argv[optind][0] != '-'
1947 && argv[optind][0] != '!')
1948 chain = argv[optind++];
1949 break;
1950
1951 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001952 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001953 exit_error(PARAMETER_PROBLEM,
1954 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001955 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001956 if (find_target(optarg, TRY_LOAD))
1957 exit_error(PARAMETER_PROBLEM,
1958 "chain name may not clash "
1959 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001960 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1961 invert);
1962 chain = optarg;
1963 break;
1964
1965 case 'X':
1966 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1967 invert);
1968 if (optarg) chain = optarg;
1969 else if (optind < argc && argv[optind][0] != '-'
1970 && argv[optind][0] != '!')
1971 chain = argv[optind++];
1972 break;
1973
1974 case 'E':
1975 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1976 invert);
1977 chain = optarg;
1978 if (optind < argc && argv[optind][0] != '-'
1979 && argv[optind][0] != '!')
1980 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001981 else
1982 exit_error(PARAMETER_PROBLEM,
1983 "-%c requires old-chain-name and "
1984 "new-chain-name",
1985 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001986 break;
1987
1988 case 'P':
1989 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1990 invert);
1991 chain = optarg;
1992 if (optind < argc && argv[optind][0] != '-'
1993 && argv[optind][0] != '!')
1994 policy = argv[optind++];
1995 else
1996 exit_error(PARAMETER_PROBLEM,
1997 "-%c requires a chain and a policy",
1998 cmd2char(CMD_SET_POLICY));
1999 break;
2000
2001 case 'h':
2002 if (!optarg)
2003 optarg = argv[optind];
2004
Rusty Russell2e0a3212000-04-19 11:23:18 +00002005 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002006 if (!matches && protocol)
2007 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002008
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002009 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002010
2011 /*
2012 * Option selection
2013 */
2014 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002015 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2017 invert);
2018
2019 /* Canonicalize into lower case */
2020 for (protocol = argv[optind-1]; *protocol; protocol++)
2021 *protocol = tolower(*protocol);
2022
2023 protocol = argv[optind-1];
2024 fw.ip.proto = parse_protocol(protocol);
2025
2026 if (fw.ip.proto == 0
2027 && (fw.ip.invflags & IPT_INV_PROTO))
2028 exit_error(PARAMETER_PROBLEM,
2029 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002030 break;
2031
2032 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002033 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002034 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2035 invert);
2036 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002037 break;
2038
2039 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002040 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2042 invert);
2043 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002044 break;
2045
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002046#ifdef IPT_F_GOTO
2047 case 'g':
2048 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2049 invert);
2050 fw.ip.flags |= IPT_F_GOTO;
2051 jumpto = parse_target(optarg);
2052 break;
2053#endif
2054
Marc Bouchere6869a82000-03-20 06:03:29 +00002055 case 'j':
2056 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2057 invert);
2058 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002059 /* TRY_LOAD (may be chain name) */
2060 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002061
2062 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002063 size_t size;
2064
Rusty Russell73f72f52000-07-03 10:17:57 +00002065 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2066 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002067
Rusty Russell2e0a3212000-04-19 11:23:18 +00002068 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002069 target->t->u.target_size = size;
2070 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002071 set_revision(target->t->u.user.name,
2072 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002073 if (target->init != NULL)
2074 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002075 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 }
2077 break;
2078
2079
2080 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002081 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002082 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2083 invert);
2084 parse_interface(argv[optind-1],
2085 fw.ip.iniface,
2086 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002087 break;
2088
2089 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002090 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002091 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2092 invert);
2093 parse_interface(argv[optind-1],
2094 fw.ip.outiface,
2095 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002096 break;
2097
2098 case 'f':
2099 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2100 invert);
2101 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002102 break;
2103
2104 case 'v':
2105 if (!verbose)
2106 set_option(&options, OPT_VERBOSE,
2107 &fw.ip.invflags, invert);
2108 verbose++;
2109 break;
2110
Rusty Russell52a51492000-05-02 16:44:29 +00002111 case 'm': {
2112 size_t size;
2113
Marc Bouchere6869a82000-03-20 06:03:29 +00002114 if (invert)
2115 exit_error(PARAMETER_PROBLEM,
2116 "unexpected ! flag before --match");
2117
Martin Josefsson78cafda2004-02-02 20:01:18 +00002118 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002119 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2120 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002121 m->m = fw_calloc(1, size);
2122 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002123 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002124 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002125 if (m->init != NULL)
2126 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002127 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002128 }
2129 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002130
2131 case 'n':
2132 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2133 invert);
2134 break;
2135
2136 case 't':
2137 if (invert)
2138 exit_error(PARAMETER_PROBLEM,
2139 "unexpected ! flag before --table");
2140 *table = argv[optind-1];
2141 break;
2142
2143 case 'x':
2144 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2145 invert);
2146 break;
2147
2148 case 'V':
2149 if (invert)
2150 printf("Not %s ;-)\n", program_version);
2151 else
2152 printf("%s v%s\n",
2153 program_name, program_version);
2154 exit(0);
2155
2156 case '0':
2157 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2158 invert);
2159 break;
2160
Harald Welte82dd2ec2000-12-19 05:18:15 +00002161 case 'M':
2162 modprobe = optarg;
2163 break;
2164
Harald Welteccd49e52001-01-23 22:54:34 +00002165 case 'c':
2166
2167 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2168 invert);
2169 pcnt = optarg;
2170 if (optind < argc && argv[optind][0] != '-'
2171 && argv[optind][0] != '!')
2172 bcnt = argv[optind++];
2173 else
2174 exit_error(PARAMETER_PROBLEM,
2175 "-%c requires packet and byte counter",
2176 opt2char(OPT_COUNTERS));
2177
Martin Josefssona28d4952004-05-26 16:04:48 +00002178 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002179 exit_error(PARAMETER_PROBLEM,
2180 "-%c packet counter not numeric",
2181 opt2char(OPT_COUNTERS));
2182
Martin Josefssona28d4952004-05-26 16:04:48 +00002183 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002184 exit_error(PARAMETER_PROBLEM,
2185 "-%c byte counter not numeric",
2186 opt2char(OPT_COUNTERS));
2187
2188 break;
2189
2190
Marc Bouchere6869a82000-03-20 06:03:29 +00002191 case 1: /* non option */
2192 if (optarg[0] == '!' && optarg[1] == '\0') {
2193 if (invert)
2194 exit_error(PARAMETER_PROBLEM,
2195 "multiple consecutive ! not"
2196 " allowed");
2197 invert = TRUE;
2198 optarg[0] = '\0';
2199 continue;
2200 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002201 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002202 exit_tryhelp(2);
2203
2204 default:
2205 /* FIXME: This scheme doesn't allow two of the same
2206 matches --RR */
2207 if (!target
2208 || !(target->parse(c - target->option_offset,
2209 argv, invert,
2210 &target->tflags,
2211 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002212 for (matchp = matches; matchp; matchp = matchp->next) {
2213 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002214 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002215 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002216 &fw,
2217 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002218 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002219 break;
2220 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002221 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002222
2223 /* If you listen carefully, you can
2224 actually hear this code suck. */
2225
2226 /* some explanations (after four different bugs
2227 * in 3 different releases): If we encountere a
2228 * parameter, that has not been parsed yet,
2229 * it's not an option of an explicitly loaded
2230 * match or a target. However, we support
2231 * implicit loading of the protocol match
2232 * extension. '-p tcp' means 'l4 proto 6' and
2233 * at the same time 'load tcp protocol match on
2234 * demand if we specify --dport'.
2235 *
2236 * To make this work, we need to make sure:
2237 * - the parameter has not been parsed by
2238 * a match (m above)
2239 * - a protocol has been specified
2240 * - the protocol extension has not been
2241 * loaded yet, or is loaded and unused
2242 * [think of iptables-restore!]
2243 * - the protocol extension can be successively
2244 * loaded
2245 */
2246 if (m == NULL
2247 && protocol
2248 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002249 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002250 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002251 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002252 && (proto_used == 0))
2253 )
2254 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002255 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002256 /* Try loading protocol */
2257 size_t size;
2258
2259 proto_used = 1;
2260
2261 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2262 + m->size;
2263
2264 m->m = fw_calloc(1, size);
2265 m->m->u.match_size = size;
2266 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002267 set_revision(m->m->u.user.name,
2268 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002269 if (m->init != NULL)
2270 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002271
2272 opts = merge_options(opts,
2273 m->extra_opts, &m->option_offset);
2274
2275 optind--;
2276 continue;
2277 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002278 if (!m)
2279 exit_error(PARAMETER_PROBLEM,
2280 "Unknown arg `%s'",
2281 argv[optind-1]);
2282 }
2283 }
2284 invert = FALSE;
2285 }
2286
Martin Josefsson78cafda2004-02-02 20:01:18 +00002287 for (matchp = matches; matchp; matchp = matchp->next)
2288 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002289
Marc Bouchere6869a82000-03-20 06:03:29 +00002290 if (target)
2291 target->final_check(target->tflags);
2292
2293 /* Fix me: must put inverse options checking here --MN */
2294
2295 if (optind < argc)
2296 exit_error(PARAMETER_PROBLEM,
2297 "unknown arguments found on commandline");
2298 if (!command)
2299 exit_error(PARAMETER_PROBLEM, "no command specified");
2300 if (invert)
2301 exit_error(PARAMETER_PROBLEM,
2302 "nothing appropriate following !");
2303
Harald Welte6336bfd2002-05-07 14:41:43 +00002304 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002305 if (!(options & OPT_DESTINATION))
2306 dhostnetworkmask = "0.0.0.0/0";
2307 if (!(options & OPT_SOURCE))
2308 shostnetworkmask = "0.0.0.0/0";
2309 }
2310
2311 if (shostnetworkmask)
2312 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2313 &(fw.ip.smsk), &nsaddrs);
2314
2315 if (dhostnetworkmask)
2316 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2317 &(fw.ip.dmsk), &ndaddrs);
2318
2319 if ((nsaddrs > 1 || ndaddrs > 1) &&
2320 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2321 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2322 " source or destination IP addresses");
2323
Marc Bouchere6869a82000-03-20 06:03:29 +00002324 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2325 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2326 "specify a unique address");
2327
2328 generic_opt_check(command, options);
2329
2330 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2331 exit_error(PARAMETER_PROBLEM,
2332 "chain name `%s' too long (must be under %i chars)",
2333 chain, IPT_FUNCTION_MAXNAMELEN);
2334
Harald Welteae1ff9f2000-12-01 14:26:20 +00002335 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002336 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002337 *handle = iptc_init(*table);
2338
Rusty Russell8beb0492004-12-22 00:37:10 +00002339 /* try to insmod the module if iptc_init failed */
2340 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002341 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002342
Marc Bouchere6869a82000-03-20 06:03:29 +00002343 if (!*handle)
2344 exit_error(VERSION_PROBLEM,
2345 "can't initialize iptables table `%s': %s",
2346 *table, iptc_strerror(errno));
2347
Harald Welte6336bfd2002-05-07 14:41:43 +00002348 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002349 || command == CMD_DELETE
2350 || command == CMD_INSERT
2351 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002352 if (strcmp(chain, "PREROUTING") == 0
2353 || strcmp(chain, "INPUT") == 0) {
2354 /* -o not valid with incoming packets. */
2355 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002356 exit_error(PARAMETER_PROBLEM,
2357 "Can't use -%c with %s\n",
2358 opt2char(OPT_VIANAMEOUT),
2359 chain);
2360 }
2361
Rusty Russella4860fd2000-06-17 16:13:02 +00002362 if (strcmp(chain, "POSTROUTING") == 0
2363 || strcmp(chain, "OUTPUT") == 0) {
2364 /* -i not valid with outgoing packets */
2365 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002366 exit_error(PARAMETER_PROBLEM,
2367 "Can't use -%c with %s\n",
2368 opt2char(OPT_VIANAMEIN),
2369 chain);
2370 }
2371
2372 if (target && iptc_is_chain(jumpto, *handle)) {
2373 printf("Warning: using chain %s, not extension\n",
2374 jumpto);
2375
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002376 if (target->t)
2377 free(target->t);
2378
Marc Bouchere6869a82000-03-20 06:03:29 +00002379 target = NULL;
2380 }
2381
2382 /* If they didn't specify a target, or it's a chain
2383 name, use standard. */
2384 if (!target
2385 && (strlen(jumpto) == 0
2386 || iptc_is_chain(jumpto, *handle))) {
2387 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002388
Rusty Russell52a51492000-05-02 16:44:29 +00002389 target = find_target(IPT_STANDARD_TARGET,
2390 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002391
2392 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002393 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002394 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002395 target->t->u.target_size = size;
2396 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002397 set_revision(target->t->u.user.name, target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002398 if (target->init != NULL)
2399 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002400 }
2401
Rusty Russell7e53bf92000-03-20 07:03:28 +00002402 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002403 /* it is no chain, and we can't load a plugin.
2404 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002405 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002406 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002407#ifdef IPT_F_GOTO
2408 if (fw.ip.flags & IPT_F_GOTO)
2409 exit_error(PARAMETER_PROBLEM,
2410 "goto '%s' is not a chain\n", jumpto);
2411#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002412 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002413 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002414 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002415 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002416 }
2417 }
2418
2419 switch (command) {
2420 case CMD_APPEND:
2421 ret = append_entry(chain, e,
2422 nsaddrs, saddrs, ndaddrs, daddrs,
2423 options&OPT_VERBOSE,
2424 handle);
2425 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002426 case CMD_DELETE:
2427 ret = delete_entry(chain, e,
2428 nsaddrs, saddrs, ndaddrs, daddrs,
2429 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002430 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002431 break;
2432 case CMD_DELETE_NUM:
2433 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2434 break;
2435 case CMD_REPLACE:
2436 ret = replace_entry(chain, e, rulenum - 1,
2437 saddrs, daddrs, options&OPT_VERBOSE,
2438 handle);
2439 break;
2440 case CMD_INSERT:
2441 ret = insert_entry(chain, e, rulenum - 1,
2442 nsaddrs, saddrs, ndaddrs, daddrs,
2443 options&OPT_VERBOSE,
2444 handle);
2445 break;
2446 case CMD_LIST:
2447 ret = list_entries(chain,
2448 options&OPT_VERBOSE,
2449 options&OPT_NUMERIC,
2450 options&OPT_EXPANDED,
2451 options&OPT_LINENUMBERS,
2452 handle);
2453 break;
2454 case CMD_FLUSH:
2455 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2456 break;
2457 case CMD_ZERO:
2458 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2459 break;
2460 case CMD_LIST|CMD_ZERO:
2461 ret = list_entries(chain,
2462 options&OPT_VERBOSE,
2463 options&OPT_NUMERIC,
2464 options&OPT_EXPANDED,
2465 options&OPT_LINENUMBERS,
2466 handle);
2467 if (ret)
2468 ret = zero_entries(chain,
2469 options&OPT_VERBOSE, handle);
2470 break;
2471 case CMD_NEW_CHAIN:
2472 ret = iptc_create_chain(chain, handle);
2473 break;
2474 case CMD_DELETE_CHAIN:
2475 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2476 break;
2477 case CMD_RENAME_CHAIN:
2478 ret = iptc_rename_chain(chain, newname, handle);
2479 break;
2480 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002481 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002482 break;
2483 default:
2484 /* We should never reach this... */
2485 exit_tryhelp(2);
2486 }
2487
2488 if (verbose > 1)
2489 dump_entries(*handle);
2490
Martin Josefsson78cafda2004-02-02 20:01:18 +00002491 clear_rule_matches(&matches);
2492
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002493 if (e != NULL) {
2494 free(e);
2495 e = NULL;
2496 }
2497
keso6997cdf2004-07-04 15:20:53 +00002498 free(saddrs);
2499 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002500 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002501
Marc Bouchere6869a82000-03-20 06:03:29 +00002502 return ret;
2503}