blob: c8ab71ec4fddb113984bd022489073c6168b7017 [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};
233
234static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000235proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000236{
237 unsigned int i;
238
Rusty Russell28381a42000-05-10 00:19:50 +0000239 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000240 struct protoent *pent = getprotobynumber(proto);
241 if (pent)
242 return pent->p_name;
243 }
244
245 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
246 if (chain_protos[i].num == proto)
247 return chain_protos[i].name;
248
249 return NULL;
250}
251
252struct in_addr *
253dotted_to_addr(const char *dotted)
254{
255 static struct in_addr addr;
256 unsigned char *addrp;
257 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000258 unsigned int onebyte;
259 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000260 char buf[20];
261
262 /* copy dotted string, because we need to modify it */
263 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000264 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000265 addrp = (unsigned char *) &(addr.s_addr);
266
267 p = buf;
268 for (i = 0; i < 3; i++) {
269 if ((q = strchr(p, '.')) == NULL)
270 return (struct in_addr *) NULL;
271
272 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000273 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 return (struct in_addr *) NULL;
275
276 addrp[i] = (unsigned char) onebyte;
277 p = q + 1;
278 }
279
280 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000281 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 return (struct in_addr *) NULL;
283
284 addrp[3] = (unsigned char) onebyte;
285
286 return &addr;
287}
288
289static struct in_addr *
290network_to_addr(const char *name)
291{
292 struct netent *net;
293 static struct in_addr addr;
294
295 if ((net = getnetbyname(name)) != NULL) {
296 if (net->n_addrtype != AF_INET)
297 return (struct in_addr *) NULL;
298 addr.s_addr = htonl((unsigned long) net->n_net);
299 return &addr;
300 }
301
302 return (struct in_addr *) NULL;
303}
304
305static void
306inaddrcpy(struct in_addr *dst, struct in_addr *src)
307{
308 /* memcpy(dst, src, sizeof(struct in_addr)); */
309 dst->s_addr = src->s_addr;
310}
311
Pablo Neiradfdcd642005-05-29 19:05:23 +0000312static void free_opts(int reset_offset)
313{
314 if (opts != original_opts) {
315 free(opts);
316 opts = original_opts;
317 if (reset_offset)
318 global_option_offset = 0;
319 }
320}
321
Marc Bouchere6869a82000-03-20 06:03:29 +0000322void
323exit_error(enum exittype status, char *msg, ...)
324{
325 va_list args;
326
327 va_start(args, msg);
328 fprintf(stderr, "%s v%s: ", program_name, program_version);
329 vfprintf(stderr, msg, args);
330 va_end(args);
331 fprintf(stderr, "\n");
332 if (status == PARAMETER_PROBLEM)
333 exit_tryhelp(status);
334 if (status == VERSION_PROBLEM)
335 fprintf(stderr,
336 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000337 /* On error paths, make sure that we don't leak memory */
338 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 exit(status);
340}
341
342void
343exit_tryhelp(int status)
344{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000345 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000346 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
348 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000349 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000350 exit(status);
351}
352
353void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000355{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000357 struct iptables_target *t = NULL;
358
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000360"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000361" %s -[RI] chain rulenum rule-specification [options]\n"
362" %s -D chain rulenum [options]\n"
363" %s -[LFZ] [chain] [options]\n"
364" %s -[NX] chain\n"
365" %s -E old-chain-name new-chain-name\n"
366" %s -P chain target [options]\n"
367" %s -h (print this help information)\n\n",
368 program_name, program_version, program_name, program_name,
369 program_name, program_name, program_name, program_name,
370 program_name, program_name);
371
372 printf(
373"Commands:\n"
374"Either long or short options are allowed.\n"
375" --append -A chain Append to chain\n"
376" --delete -D chain Delete matching rule from chain\n"
377" --delete -D chain rulenum\n"
378" Delete rule rulenum (1 = first) from chain\n"
379" --insert -I chain [rulenum]\n"
380" Insert in chain as rulenum (default 1=first)\n"
381" --replace -R chain rulenum\n"
382" Replace rule rulenum (1 = first) in chain\n"
383" --list -L [chain] List the rules in a chain or all chains\n"
384" --flush -F [chain] Delete all rules in chain or all chains\n"
385" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000386" --new -N chain Create a new user-defined chain\n"
387" --delete-chain\n"
388" -X [chain] Delete a user-defined chain\n"
389" --policy -P chain target\n"
390" Change policy on chain to target\n"
391" --rename-chain\n"
392" -E old-chain new-chain\n"
393" Change chain name, (moving any references)\n"
394
395"Options:\n"
396" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
397" --source -s [!] address[/mask]\n"
398" source specification\n"
399" --destination -d [!] address[/mask]\n"
400" destination specification\n"
401" --in-interface -i [!] input name[+]\n"
402" network interface name ([+] for wildcard)\n"
403" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000404" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000405#ifdef IPT_F_GOTO
406" --goto -g chain\n"
407" jump to chain with no return\n"
408#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000409" --match -m match\n"
410" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000411" --numeric -n numeric output of addresses and ports\n"
412" --out-interface -o [!] output name[+]\n"
413" network interface name ([+] for wildcard)\n"
414" --table -t table table to manipulate (default: `filter')\n"
415" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000416" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000417" --exact -x expand numbers (display exact values)\n"
418"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000419" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000420" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000421"[!] --version -V print package version.\n");
422
Rusty Russell363112d2000-08-11 13:49:26 +0000423 /* Print out any special helps. A user might like to be able
424 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000425 results. So we call help for all specified matches & targets */
426 for (t = iptables_targets; t ;t = t->next) {
427 if (t->used) {
428 printf("\n");
429 t->help();
430 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000431 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000432 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000434 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000435 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000436 exit(0);
437}
438
439static void
440generic_opt_check(int command, int options)
441{
442 int i, j, legal = 0;
443
444 /* Check that commands are valid with options. Complicated by the
445 * fact that if an option is legal with *any* command given, it is
446 * legal overall (ie. -z and -l).
447 */
448 for (i = 0; i < NUMBER_OF_OPT; i++) {
449 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
450
451 for (j = 0; j < NUMBER_OF_CMD; j++) {
452 if (!(command & (1<<j)))
453 continue;
454
455 if (!(options & (1<<i))) {
456 if (commands_v_options[j][i] == '+')
457 exit_error(PARAMETER_PROBLEM,
458 "You need to supply the `-%c' "
459 "option for this command\n",
460 optflags[i]);
461 } else {
462 if (commands_v_options[j][i] != 'x')
463 legal = 1;
464 else if (legal == 0)
465 legal = -1;
466 }
467 }
468 if (legal == -1)
469 exit_error(PARAMETER_PROBLEM,
470 "Illegal option `-%c' with this command\n",
471 optflags[i]);
472 }
473}
474
475static char
476opt2char(int option)
477{
478 const char *ptr;
479 for (ptr = optflags; option > 1; option >>= 1, ptr++);
480
481 return *ptr;
482}
483
484static char
485cmd2char(int option)
486{
487 const char *ptr;
488 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
489
490 return *ptr;
491}
492
493static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000494add_command(unsigned int *cmd, const int newcmd, const int othercmds,
495 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000496{
497 if (invert)
498 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
499 if (*cmd & (~othercmds))
500 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
501 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
502 *cmd |= newcmd;
503}
504
505int
Harald Welteb77f1da2002-03-14 11:35:58 +0000506check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000507{
508 if (option && strcmp(option, "!") == 0) {
509 if (*invert)
510 exit_error(PARAMETER_PROBLEM,
511 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000512 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000513 if (optind) {
514 *optind = *optind+1;
515 if (argc && *optind > argc)
516 exit_error(PARAMETER_PROBLEM,
517 "no argument following `!'");
518 }
519
Marc Bouchere6869a82000-03-20 06:03:29 +0000520 return TRUE;
521 }
522 return FALSE;
523}
524
525static void *
526fw_calloc(size_t count, size_t size)
527{
528 void *p;
529
530 if ((p = calloc(count, size)) == NULL) {
531 perror("iptables: calloc failed");
532 exit(1);
533 }
534 return p;
535}
536
537static void *
538fw_malloc(size_t size)
539{
540 void *p;
541
542 if ((p = malloc(size)) == NULL) {
543 perror("iptables: malloc failed");
544 exit(1);
545 }
546 return p;
547}
548
549static struct in_addr *
550host_to_addr(const char *name, unsigned int *naddr)
551{
552 struct hostent *host;
553 struct in_addr *addr;
554 unsigned int i;
555
556 *naddr = 0;
557 if ((host = gethostbyname(name)) != NULL) {
558 if (host->h_addrtype != AF_INET ||
559 host->h_length != sizeof(struct in_addr))
560 return (struct in_addr *) NULL;
561
562 while (host->h_addr_list[*naddr] != (char *) NULL)
563 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000564 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000565 for (i = 0; i < *naddr; i++)
566 inaddrcpy(&(addr[i]),
567 (struct in_addr *) host->h_addr_list[i]);
568 return addr;
569 }
570
571 return (struct in_addr *) NULL;
572}
573
574static char *
575addr_to_host(const struct in_addr *addr)
576{
577 struct hostent *host;
578
579 if ((host = gethostbyaddr((char *) addr,
580 sizeof(struct in_addr), AF_INET)) != NULL)
581 return (char *) host->h_name;
582
583 return (char *) NULL;
584}
585
586/*
587 * All functions starting with "parse" should succeed, otherwise
588 * the program fails.
589 * Most routines return pointers to static data that may change
590 * between calls to the same or other routines with a few exceptions:
591 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
592 * return global static data.
593*/
594
595static struct in_addr *
596parse_hostnetwork(const char *name, unsigned int *naddrs)
597{
598 struct in_addr *addrp, *addrptmp;
599
600 if ((addrptmp = dotted_to_addr(name)) != NULL ||
601 (addrptmp = network_to_addr(name)) != NULL) {
602 addrp = fw_malloc(sizeof(struct in_addr));
603 inaddrcpy(addrp, addrptmp);
604 *naddrs = 1;
605 return addrp;
606 }
607 if ((addrp = host_to_addr(name, naddrs)) != NULL)
608 return addrp;
609
610 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
611}
612
613static struct in_addr *
614parse_mask(char *mask)
615{
616 static struct in_addr maskaddr;
617 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000618 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000619
620 if (mask == NULL) {
621 /* no mask at all defaults to 32 bits */
622 maskaddr.s_addr = 0xFFFFFFFF;
623 return &maskaddr;
624 }
625 if ((addrp = dotted_to_addr(mask)) != NULL)
626 /* dotted_to_addr already returns a network byte order addr */
627 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000628 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000629 exit_error(PARAMETER_PROBLEM,
630 "invalid mask `%s' specified", mask);
631 if (bits != 0) {
632 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
633 return &maskaddr;
634 }
635
636 maskaddr.s_addr = 0L;
637 return &maskaddr;
638}
639
Marc Boucherb93c7982001-12-06 14:50:19 +0000640void
Marc Bouchere6869a82000-03-20 06:03:29 +0000641parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
642 struct in_addr *maskp, unsigned int *naddrs)
643{
644 struct in_addr *addrp;
645 char buf[256];
646 char *p;
647 int i, j, k, n;
648
649 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000650 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000651 if ((p = strrchr(buf, '/')) != NULL) {
652 *p = '\0';
653 addrp = parse_mask(p + 1);
654 } else
655 addrp = parse_mask(NULL);
656 inaddrcpy(maskp, addrp);
657
658 /* if a null mask is given, the name is ignored, like in "any/0" */
659 if (maskp->s_addr == 0L)
660 strcpy(buf, "0.0.0.0");
661
662 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
663 n = *naddrs;
664 for (i = 0, j = 0; i < n; i++) {
665 addrp[j++].s_addr &= maskp->s_addr;
666 for (k = 0; k < j - 1; k++) {
667 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
668 (*naddrs)--;
669 j--;
670 break;
671 }
672 }
673 }
674}
675
676struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000677find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000678{
679 struct iptables_match *ptr;
680
681 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
682 if (strcmp(name, ptr->name) == 0)
683 break;
684 }
685
Harald Welte3efb6ea2001-08-06 18:50:21 +0000686#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000687 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000688 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000689 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000690 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000691 if (dlopen(path, RTLD_NOW)) {
692 /* Found library. If it didn't register itself,
693 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000694 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000695
Rusty Russell9e1d2142000-04-23 09:11:12 +0000696 if (!ptr)
697 exit_error(PARAMETER_PROBLEM,
698 "Couldn't load match `%s'\n",
699 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000700 } else if (tryload == LOAD_MUST_SUCCEED)
701 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000702 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000703 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000705#else
706 if (ptr && !ptr->loaded) {
707 if (tryload != DONT_LOAD)
708 ptr->loaded = 1;
709 else
710 ptr = NULL;
711 }
Marc Boucher067477b2002-03-24 15:09:31 +0000712 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
713 exit_error(PARAMETER_PROBLEM,
714 "Couldn't find match `%s'\n", name);
715 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000716#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000717
Martin Josefsson78cafda2004-02-02 20:01:18 +0000718 if (ptr && matches) {
719 struct iptables_rule_match **i;
720 struct iptables_rule_match *newentry;
721
722 newentry = fw_malloc(sizeof(struct iptables_rule_match));
723
724 for (i = matches; *i; i = &(*i)->next);
725 newentry->match = ptr;
726 newentry->next = NULL;
727 *i = newentry;
728 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000729
Marc Bouchere6869a82000-03-20 06:03:29 +0000730 return ptr;
731}
732
Rusty Russell28381a42000-05-10 00:19:50 +0000733/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
734static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000735find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000736{
Harald Welteed498492001-07-23 01:24:22 +0000737 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000738
Harald Welte0b0013a2002-02-18 16:15:31 +0000739 if (string_to_number(pname, 0, 255, &proto) != -1) {
740 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000741
Harald Welte0b0013a2002-02-18 16:15:31 +0000742 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000743 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000744 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000745 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000746
747 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000748}
749
Marc Boucherb93c7982001-12-06 14:50:19 +0000750u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000751parse_protocol(const char *s)
752{
Harald Welteed498492001-07-23 01:24:22 +0000753 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000754
Harald Welteed498492001-07-23 01:24:22 +0000755 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000756 struct protoent *pent;
757
Harald Weltecbe1ec72006-02-11 09:50:11 +0000758 /* first deal with the special case of 'all' to prevent
759 * people from being able to redefine 'all' in nsswitch
760 * and/or provoke expensive [not working] ldap/nis/...
761 * lookups */
762 if (!strcmp(s, "all"))
763 return 0;
764
Marc Bouchere6869a82000-03-20 06:03:29 +0000765 if ((pent = getprotobyname(s)))
766 proto = pent->p_proto;
767 else {
768 unsigned int i;
769 for (i = 0;
770 i < sizeof(chain_protos)/sizeof(struct pprot);
771 i++) {
772 if (strcmp(s, chain_protos[i].name) == 0) {
773 proto = chain_protos[i].num;
774 break;
775 }
776 }
777 if (i == sizeof(chain_protos)/sizeof(struct pprot))
778 exit_error(PARAMETER_PROBLEM,
779 "unknown protocol `%s' specified",
780 s);
781 }
782 }
783
784 return (u_int16_t)proto;
785}
786
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000787void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000788{
789 int vialen = strlen(arg);
790 unsigned int i;
791
792 memset(mask, 0, IFNAMSIZ);
793 memset(vianame, 0, IFNAMSIZ);
794
795 if (vialen + 1 > IFNAMSIZ)
796 exit_error(PARAMETER_PROBLEM,
797 "interface name `%s' must be shorter than IFNAMSIZ"
798 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000799
Marc Bouchere6869a82000-03-20 06:03:29 +0000800 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000801 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 memset(mask, 0, IFNAMSIZ);
803 else if (vianame[vialen - 1] == '+') {
804 memset(mask, 0xFF, vialen - 1);
805 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000806 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000807 } else {
808 /* Include nul-terminator in match */
809 memset(mask, 0xFF, vialen + 1);
810 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000811 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000812 if (!isalnum(vianame[i])
813 && vianame[i] != '_'
814 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000815 printf("Warning: wierd character in interface"
816 " `%s' (No aliases, :, ! or *).\n",
817 vianame);
818 break;
819 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 }
821 }
822}
823
824/* Can't be zero. */
825static int
826parse_rulenumber(const char *rule)
827{
Harald Welteed498492001-07-23 01:24:22 +0000828 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000829
Harald Welteed498492001-07-23 01:24:22 +0000830 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000831 exit_error(PARAMETER_PROBLEM,
832 "Invalid rule number `%s'", rule);
833
834 return rulenum;
835}
836
837static const char *
838parse_target(const char *targetname)
839{
840 const char *ptr;
841
842 if (strlen(targetname) < 1)
843 exit_error(PARAMETER_PROBLEM,
844 "Invalid target name (too short)");
845
846 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
847 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000848 "Invalid target name `%s' (%u chars max)",
849 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000850
851 for (ptr = targetname; *ptr; ptr++)
852 if (isspace(*ptr))
853 exit_error(PARAMETER_PROBLEM,
854 "Invalid target name `%s'", targetname);
855 return targetname;
856}
857
858static char *
859addr_to_network(const struct in_addr *addr)
860{
861 struct netent *net;
862
863 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
864 return (char *) net->n_name;
865
866 return (char *) NULL;
867}
868
869char *
870addr_to_dotted(const struct in_addr *addrp)
871{
872 static char buf[20];
873 const unsigned char *bytep;
874
875 bytep = (const unsigned char *) &(addrp->s_addr);
876 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
877 return buf;
878}
Marc Boucherb93c7982001-12-06 14:50:19 +0000879
880char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000881addr_to_anyname(const struct in_addr *addr)
882{
883 char *name;
884
885 if ((name = addr_to_host(addr)) != NULL ||
886 (name = addr_to_network(addr)) != NULL)
887 return name;
888
889 return addr_to_dotted(addr);
890}
891
Marc Boucherb93c7982001-12-06 14:50:19 +0000892char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000893mask_to_dotted(const struct in_addr *mask)
894{
895 int i;
896 static char buf[20];
897 u_int32_t maskaddr, bits;
898
899 maskaddr = ntohl(mask->s_addr);
900
901 if (maskaddr == 0xFFFFFFFFL)
902 /* we don't want to see "/32" */
903 return "";
904
905 i = 32;
906 bits = 0xFFFFFFFEL;
907 while (--i >= 0 && maskaddr != bits)
908 bits <<= 1;
909 if (i >= 0)
910 sprintf(buf, "/%d", i);
911 else
912 /* mask was not a decent combination of 1's and 0's */
913 sprintf(buf, "/%s", addr_to_dotted(mask));
914
915 return buf;
916}
917
918int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000919string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
920 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000921{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000922 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000923 char *end;
924
925 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000926 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000927 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 if (*end == '\0' && end != s) {
929 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000930 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000931 *ret = number;
932 return 0;
933 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000934 }
935 return -1;
936}
937
Martin Josefssonb105bc92004-05-26 15:54:49 +0000938int
939string_to_number_l(const char *s, unsigned long min, unsigned long max,
940 unsigned long *ret)
941{
942 int result;
943 unsigned long long number;
944
945 result = string_to_number_ll(s, min, max, &number);
946 *ret = (unsigned long)number;
947
948 return result;
949}
950
951int string_to_number(const char *s, unsigned int min, unsigned int max,
952 unsigned int *ret)
953{
954 int result;
955 unsigned long number;
956
957 result = string_to_number_l(s, min, max, &number);
958 *ret = (unsigned int)number;
959
960 return result;
961}
962
Marc Bouchere6869a82000-03-20 06:03:29 +0000963static void
964set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
965 int invert)
966{
967 if (*options & option)
968 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
969 opt2char(option));
970 *options |= option;
971
972 if (invert) {
973 unsigned int i;
974 for (i = 0; 1 << i != option; i++);
975
976 if (!inverse_for_options[i])
977 exit_error(PARAMETER_PROBLEM,
978 "cannot have ! before -%c",
979 opt2char(option));
980 *invflg |= inverse_for_options[i];
981 }
982}
983
984struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000985find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000986{
987 struct iptables_target *ptr;
988
989 /* Standard target? */
990 if (strcmp(name, "") == 0
991 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
992 || strcmp(name, IPTC_LABEL_DROP) == 0
993 || strcmp(name, IPTC_LABEL_QUEUE) == 0
994 || strcmp(name, IPTC_LABEL_RETURN) == 0)
995 name = "standard";
996
997 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
998 if (strcmp(name, ptr->name) == 0)
999 break;
1000 }
1001
Harald Welte3efb6ea2001-08-06 18:50:21 +00001002#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001003 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001004 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001006 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001007 if (dlopen(path, RTLD_NOW)) {
1008 /* Found library. If it didn't register itself,
1009 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001010 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001011 if (!ptr)
1012 exit_error(PARAMETER_PROBLEM,
1013 "Couldn't load target `%s'\n",
1014 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001015 } else if (tryload == LOAD_MUST_SUCCEED)
1016 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001017 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001018 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001019 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001020#else
1021 if (ptr && !ptr->loaded) {
1022 if (tryload != DONT_LOAD)
1023 ptr->loaded = 1;
1024 else
1025 ptr = NULL;
1026 }
Marc Boucher067477b2002-03-24 15:09:31 +00001027 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1028 exit_error(PARAMETER_PROBLEM,
1029 "Couldn't find target `%s'\n", name);
1030 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001031#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001032
Harald Welteae1ff9f2000-12-01 14:26:20 +00001033 if (ptr)
1034 ptr->used = 1;
1035
Marc Bouchere6869a82000-03-20 06:03:29 +00001036 return ptr;
1037}
1038
1039static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001040merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001041 unsigned int *option_offset)
1042{
1043 unsigned int num_old, num_new, i;
1044 struct option *merge;
1045
1046 for (num_old = 0; oldopts[num_old].name; num_old++);
1047 for (num_new = 0; newopts[num_new].name; num_new++);
1048
1049 global_option_offset += OPTION_OFFSET;
1050 *option_offset = global_option_offset;
1051
1052 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1053 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001054 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001055 for (i = 0; i < num_new; i++) {
1056 merge[num_old + i] = newopts[i];
1057 merge[num_old + i].val += *option_offset;
1058 }
1059 memset(merge + num_old + num_new, 0, sizeof(struct option));
1060
1061 return merge;
1062}
1063
Rusty Russell3aef54d2005-01-03 03:48:40 +00001064static int compatible_revision(const char *name, u_int8_t revision, int opt)
1065{
1066 struct ipt_get_revision rev;
1067 socklen_t s = sizeof(rev);
1068 int max_rev, sockfd;
1069
1070 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1071 if (sockfd < 0) {
1072 fprintf(stderr, "Could not open socket to kernel: %s\n",
1073 strerror(errno));
1074 exit(1);
1075 }
1076
1077 strcpy(rev.name, name);
1078 rev.revision = revision;
1079
1080 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1081 if (max_rev < 0) {
1082 /* Definitely don't support this? */
1083 if (errno == EPROTONOSUPPORT) {
1084 close(sockfd);
1085 return 0;
1086 } else if (errno == ENOPROTOOPT) {
1087 close(sockfd);
1088 /* Assume only revision 0 support (old kernel) */
1089 return (revision == 0);
1090 } else {
1091 fprintf(stderr, "getsockopt failed strangely: %s\n",
1092 strerror(errno));
1093 exit(1);
1094 }
1095 }
1096 close(sockfd);
1097 return 1;
1098}
1099
1100static int compatible_match_revision(const char *name, u_int8_t revision)
1101{
1102 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1103}
1104
1105static int compatible_target_revision(const char *name, u_int8_t revision)
1106{
1107 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1108}
1109
Marc Bouchere6869a82000-03-20 06:03:29 +00001110void
1111register_match(struct iptables_match *me)
1112{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001113 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001114
Marc Bouchere6869a82000-03-20 06:03:29 +00001115 if (strcmp(me->version, program_version) != 0) {
1116 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1117 program_name, me->name, me->version, program_version);
1118 exit(1);
1119 }
1120
Martin Josefsson93911bf2005-01-03 07:46:07 +00001121 /* Revision field stole a char from name. */
1122 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001123 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001124 program_name, me->name);
1125 exit(1);
1126 }
1127
Jones Desougif5b86e62005-12-22 03:33:50 +00001128 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001129 if (old) {
1130 if (old->revision == me->revision) {
1131 fprintf(stderr,
1132 "%s: match `%s' already registered.\n",
1133 program_name, me->name);
1134 exit(1);
1135 }
1136
1137 /* Now we have two (or more) options, check compatibility. */
1138 if (compatible_match_revision(old->name, old->revision)
1139 && old->revision > me->revision)
1140 return;
1141
1142 /* Replace if compatible. */
1143 if (!compatible_match_revision(me->name, me->revision))
1144 return;
1145
1146 /* Delete old one. */
1147 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1148 *i = old->next;
1149 }
1150
Rusty Russell73f72f52000-07-03 10:17:57 +00001151 if (me->size != IPT_ALIGN(me->size)) {
1152 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001153 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001154 exit(1);
1155 }
1156
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001157 /* Append to list. */
1158 for (i = &iptables_matches; *i; i = &(*i)->next);
1159 me->next = NULL;
1160 *i = me;
1161
Marc Bouchere6869a82000-03-20 06:03:29 +00001162 me->m = NULL;
1163 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001164}
1165
1166void
1167register_target(struct iptables_target *me)
1168{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001169 struct iptables_target *old;
1170
Marc Bouchere6869a82000-03-20 06:03:29 +00001171 if (strcmp(me->version, program_version) != 0) {
1172 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1173 program_name, me->name, me->version, program_version);
1174 exit(1);
1175 }
1176
Martin Josefsson93911bf2005-01-03 07:46:07 +00001177 /* Revision field stole a char from name. */
1178 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001179 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001180 program_name, me->name);
1181 exit(1);
1182 }
1183
Jones Desougif5b86e62005-12-22 03:33:50 +00001184 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001185 if (old) {
1186 struct iptables_target **i;
1187
1188 if (old->revision == me->revision) {
1189 fprintf(stderr,
1190 "%s: target `%s' already registered.\n",
1191 program_name, me->name);
1192 exit(1);
1193 }
1194
Rusty Russell3aef54d2005-01-03 03:48:40 +00001195 /* Now we have two (or more) options, check compatibility. */
1196 if (compatible_target_revision(old->name, old->revision)
1197 && old->revision > me->revision)
1198 return;
1199
1200 /* Replace if compatible. */
1201 if (!compatible_target_revision(me->name, me->revision))
1202 return;
1203
1204 /* Delete old one. */
1205 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1206 *i = old->next;
1207 }
1208
Rusty Russell73f72f52000-07-03 10:17:57 +00001209 if (me->size != IPT_ALIGN(me->size)) {
1210 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001211 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001212 exit(1);
1213 }
1214
Marc Bouchere6869a82000-03-20 06:03:29 +00001215 /* Prepend to list. */
1216 me->next = iptables_targets;
1217 iptables_targets = me;
1218 me->t = NULL;
1219 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001220}
1221
1222static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001223print_num(u_int64_t number, unsigned int format)
1224{
1225 if (format & FMT_KILOMEGAGIGA) {
1226 if (number > 99999) {
1227 number = (number + 500) / 1000;
1228 if (number > 9999) {
1229 number = (number + 500) / 1000;
1230 if (number > 9999) {
1231 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001232 if (number > 9999) {
1233 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001234 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001235 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001236 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001237 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001238 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001239 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001240 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001241 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001242 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001243 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001244 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001245}
1246
1247
1248static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001249print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1250{
1251 struct ipt_counters counters;
1252 const char *pol = iptc_get_policy(chain, &counters, handle);
1253 printf("Chain %s", chain);
1254 if (pol) {
1255 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001256 if (!(format & FMT_NOCOUNTS)) {
1257 fputc(' ', stdout);
1258 print_num(counters.pcnt, (format|FMT_NOTABLE));
1259 fputs("packets, ", stdout);
1260 print_num(counters.bcnt, (format|FMT_NOTABLE));
1261 fputs("bytes", stdout);
1262 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001263 printf(")\n");
1264 } else {
1265 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001266 if (!iptc_get_references(&refs, chain, handle))
1267 printf(" (ERROR obtaining refs)\n");
1268 else
1269 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 }
1271
1272 if (format & FMT_LINENUMBERS)
1273 printf(FMT("%-4s ", "%s "), "num");
1274 if (!(format & FMT_NOCOUNTS)) {
1275 if (format & FMT_KILOMEGAGIGA) {
1276 printf(FMT("%5s ","%s "), "pkts");
1277 printf(FMT("%5s ","%s "), "bytes");
1278 } else {
1279 printf(FMT("%8s ","%s "), "pkts");
1280 printf(FMT("%10s ","%s "), "bytes");
1281 }
1282 }
1283 if (!(format & FMT_NOTARGET))
1284 printf(FMT("%-9s ","%s "), "target");
1285 fputs(" prot ", stdout);
1286 if (format & FMT_OPTIONS)
1287 fputs("opt", stdout);
1288 if (format & FMT_VIA) {
1289 printf(FMT(" %-6s ","%s "), "in");
1290 printf(FMT("%-6s ","%s "), "out");
1291 }
1292 printf(FMT(" %-19s ","%s "), "source");
1293 printf(FMT(" %-19s "," %s "), "destination");
1294 printf("\n");
1295}
1296
Marc Bouchere6869a82000-03-20 06:03:29 +00001297
1298static int
1299print_match(const struct ipt_entry_match *m,
1300 const struct ipt_ip *ip,
1301 int numeric)
1302{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001303 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001304
1305 if (match) {
1306 if (match->print)
1307 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001308 else
Rusty Russellb039b022000-09-01 06:04:05 +00001309 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001310 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001311 if (m->u.user.name[0])
1312 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001313 }
1314 /* Don't stop iterating. */
1315 return 0;
1316}
1317
1318/* e is called `fw' here for hysterical raisins */
1319static void
1320print_firewall(const struct ipt_entry *fw,
1321 const char *targname,
1322 unsigned int num,
1323 unsigned int format,
1324 const iptc_handle_t handle)
1325{
1326 struct iptables_target *target = NULL;
1327 const struct ipt_entry_target *t;
1328 u_int8_t flags;
1329 char buf[BUFSIZ];
1330
Marc Bouchere6869a82000-03-20 06:03:29 +00001331 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001332 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001333 else
Rusty Russell52a51492000-05-02 16:44:29 +00001334 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001335
1336 t = ipt_get_target((struct ipt_entry *)fw);
1337 flags = fw->ip.flags;
1338
1339 if (format & FMT_LINENUMBERS)
1340 printf(FMT("%-4u ", "%u "), num+1);
1341
1342 if (!(format & FMT_NOCOUNTS)) {
1343 print_num(fw->counters.pcnt, format);
1344 print_num(fw->counters.bcnt, format);
1345 }
1346
1347 if (!(format & FMT_NOTARGET))
1348 printf(FMT("%-9s ", "%s "), targname);
1349
1350 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1351 {
Rusty Russell28381a42000-05-10 00:19:50 +00001352 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001353 if (pname)
1354 printf(FMT("%-5s", "%s "), pname);
1355 else
1356 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1357 }
1358
1359 if (format & FMT_OPTIONS) {
1360 if (format & FMT_NOTABLE)
1361 fputs("opt ", stdout);
1362 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1363 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1364 fputc(' ', stdout);
1365 }
1366
1367 if (format & FMT_VIA) {
1368 char iface[IFNAMSIZ+2];
1369
1370 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1371 iface[0] = '!';
1372 iface[1] = '\0';
1373 }
1374 else iface[0] = '\0';
1375
1376 if (fw->ip.iniface[0] != '\0') {
1377 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001378 }
1379 else if (format & FMT_NUMERIC) strcat(iface, "*");
1380 else strcat(iface, "any");
1381 printf(FMT(" %-6s ","in %s "), iface);
1382
1383 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1384 iface[0] = '!';
1385 iface[1] = '\0';
1386 }
1387 else iface[0] = '\0';
1388
1389 if (fw->ip.outiface[0] != '\0') {
1390 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001391 }
1392 else if (format & FMT_NUMERIC) strcat(iface, "*");
1393 else strcat(iface, "any");
1394 printf(FMT("%-6s ","out %s "), iface);
1395 }
1396
1397 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1398 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1399 printf(FMT("%-19s ","%s "), "anywhere");
1400 else {
1401 if (format & FMT_NUMERIC)
1402 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1403 else
1404 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1405 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1406 printf(FMT("%-19s ","%s "), buf);
1407 }
1408
1409 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1410 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001411 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001412 else {
1413 if (format & FMT_NUMERIC)
1414 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1415 else
1416 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1417 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001418 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001419 }
1420
1421 if (format & FMT_NOTABLE)
1422 fputs(" ", stdout);
1423
Harald Welte72bd87e2005-11-24 17:04:05 +00001424#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001425 if(fw->ip.flags & IPT_F_GOTO)
1426 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001427#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001428
Marc Bouchere6869a82000-03-20 06:03:29 +00001429 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1430
1431 if (target) {
1432 if (target->print)
1433 /* Print the target information. */
1434 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001435 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001437 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001438
1439 if (!(format & FMT_NONEWLINE))
1440 fputc('\n', stdout);
1441}
1442
1443static void
1444print_firewall_line(const struct ipt_entry *fw,
1445 const iptc_handle_t h)
1446{
1447 struct ipt_entry_target *t;
1448
1449 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001450 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001451}
1452
1453static int
1454append_entry(const ipt_chainlabel chain,
1455 struct ipt_entry *fw,
1456 unsigned int nsaddrs,
1457 const struct in_addr saddrs[],
1458 unsigned int ndaddrs,
1459 const struct in_addr daddrs[],
1460 int verbose,
1461 iptc_handle_t *handle)
1462{
1463 unsigned int i, j;
1464 int ret = 1;
1465
1466 for (i = 0; i < nsaddrs; i++) {
1467 fw->ip.src.s_addr = saddrs[i].s_addr;
1468 for (j = 0; j < ndaddrs; j++) {
1469 fw->ip.dst.s_addr = daddrs[j].s_addr;
1470 if (verbose)
1471 print_firewall_line(fw, *handle);
1472 ret &= iptc_append_entry(chain, fw, handle);
1473 }
1474 }
1475
1476 return ret;
1477}
1478
1479static int
1480replace_entry(const ipt_chainlabel chain,
1481 struct ipt_entry *fw,
1482 unsigned int rulenum,
1483 const struct in_addr *saddr,
1484 const struct in_addr *daddr,
1485 int verbose,
1486 iptc_handle_t *handle)
1487{
1488 fw->ip.src.s_addr = saddr->s_addr;
1489 fw->ip.dst.s_addr = daddr->s_addr;
1490
1491 if (verbose)
1492 print_firewall_line(fw, *handle);
1493 return iptc_replace_entry(chain, fw, rulenum, handle);
1494}
1495
1496static int
1497insert_entry(const ipt_chainlabel chain,
1498 struct ipt_entry *fw,
1499 unsigned int rulenum,
1500 unsigned int nsaddrs,
1501 const struct in_addr saddrs[],
1502 unsigned int ndaddrs,
1503 const struct in_addr daddrs[],
1504 int verbose,
1505 iptc_handle_t *handle)
1506{
1507 unsigned int i, j;
1508 int ret = 1;
1509
1510 for (i = 0; i < nsaddrs; i++) {
1511 fw->ip.src.s_addr = saddrs[i].s_addr;
1512 for (j = 0; j < ndaddrs; j++) {
1513 fw->ip.dst.s_addr = daddrs[j].s_addr;
1514 if (verbose)
1515 print_firewall_line(fw, *handle);
1516 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1517 }
1518 }
1519
1520 return ret;
1521}
1522
Rusty Russell2e0a3212000-04-19 11:23:18 +00001523static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001524make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001525{
1526 /* Establish mask for comparison */
1527 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001528 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001529 unsigned char *mask, *mptr;
1530
1531 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001532 for (matchp = matches; matchp; matchp = matchp->next)
1533 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001534
Rusty Russell9e1d2142000-04-23 09:11:12 +00001535 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001536 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001537 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001538
Rusty Russell9e1d2142000-04-23 09:11:12 +00001539 memset(mask, 0xFF, sizeof(struct ipt_entry));
1540 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001541
Martin Josefsson78cafda2004-02-02 20:01:18 +00001542 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001543 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001544 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001545 + matchp->match->userspacesize);
1546 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001547 }
1548
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001549 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001550 IPT_ALIGN(sizeof(struct ipt_entry_target))
1551 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001552
1553 return mask;
1554}
1555
Marc Bouchere6869a82000-03-20 06:03:29 +00001556static int
1557delete_entry(const ipt_chainlabel chain,
1558 struct ipt_entry *fw,
1559 unsigned int nsaddrs,
1560 const struct in_addr saddrs[],
1561 unsigned int ndaddrs,
1562 const struct in_addr daddrs[],
1563 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001564 iptc_handle_t *handle,
1565 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001566{
1567 unsigned int i, j;
1568 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001569 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001570
Martin Josefsson78cafda2004-02-02 20:01:18 +00001571 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001572 for (i = 0; i < nsaddrs; i++) {
1573 fw->ip.src.s_addr = saddrs[i].s_addr;
1574 for (j = 0; j < ndaddrs; j++) {
1575 fw->ip.dst.s_addr = daddrs[j].s_addr;
1576 if (verbose)
1577 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001578 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001579 }
1580 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001581 free(mask);
1582
Marc Bouchere6869a82000-03-20 06:03:29 +00001583 return ret;
1584}
1585
Harald Welteae1ff9f2000-12-01 14:26:20 +00001586int
Marc Bouchere6869a82000-03-20 06:03:29 +00001587for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001588 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001589{
1590 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001591 const char *chain;
1592 char *chains;
1593 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001594
Rusty Russell9e1d2142000-04-23 09:11:12 +00001595 chain = iptc_first_chain(handle);
1596 while (chain) {
1597 chaincount++;
1598 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001599 }
1600
Rusty Russell9e1d2142000-04-23 09:11:12 +00001601 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1602 i = 0;
1603 chain = iptc_first_chain(handle);
1604 while (chain) {
1605 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1606 i++;
1607 chain = iptc_next_chain(handle);
1608 }
1609
1610 for (i = 0; i < chaincount; i++) {
1611 if (!builtinstoo
1612 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001613 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001614 continue;
1615 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1616 }
1617
1618 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001619 return ret;
1620}
1621
Harald Welteae1ff9f2000-12-01 14:26:20 +00001622int
Marc Bouchere6869a82000-03-20 06:03:29 +00001623flush_entries(const ipt_chainlabel chain, int verbose,
1624 iptc_handle_t *handle)
1625{
1626 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001627 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001628
1629 if (verbose)
1630 fprintf(stdout, "Flushing chain `%s'\n", chain);
1631 return iptc_flush_entries(chain, handle);
1632}
Marc Bouchere6869a82000-03-20 06:03:29 +00001633
1634static int
1635zero_entries(const ipt_chainlabel chain, int verbose,
1636 iptc_handle_t *handle)
1637{
1638 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001639 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001640
Marc Bouchere6869a82000-03-20 06:03:29 +00001641 if (verbose)
1642 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1643 return iptc_zero_entries(chain, handle);
1644}
1645
Harald Welteae1ff9f2000-12-01 14:26:20 +00001646int
Marc Bouchere6869a82000-03-20 06:03:29 +00001647delete_chain(const ipt_chainlabel chain, int verbose,
1648 iptc_handle_t *handle)
1649{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001650 if (!chain)
1651 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001652
1653 if (verbose)
1654 fprintf(stdout, "Deleting chain `%s'\n", chain);
1655 return iptc_delete_chain(chain, handle);
1656}
1657
1658static int
1659list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1660 int expanded, int linenumbers, iptc_handle_t *handle)
1661{
1662 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001663 unsigned int format;
1664 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001665
1666 format = FMT_OPTIONS;
1667 if (!verbose)
1668 format |= FMT_NOCOUNTS;
1669 else
1670 format |= FMT_VIA;
1671
1672 if (numeric)
1673 format |= FMT_NUMERIC;
1674
1675 if (!expanded)
1676 format |= FMT_KILOMEGAGIGA;
1677
1678 if (linenumbers)
1679 format |= FMT_LINENUMBERS;
1680
Rusty Russell9e1d2142000-04-23 09:11:12 +00001681 for (this = iptc_first_chain(handle);
1682 this;
1683 this = iptc_next_chain(handle)) {
1684 const struct ipt_entry *i;
1685 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001686
Marc Bouchere6869a82000-03-20 06:03:29 +00001687 if (chain && strcmp(chain, this) != 0)
1688 continue;
1689
1690 if (found) printf("\n");
1691
1692 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001693 i = iptc_first_rule(this, handle);
1694
1695 num = 0;
1696 while (i) {
1697 print_firewall(i,
1698 iptc_get_target(i, handle),
1699 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001700 format,
1701 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001702 i = iptc_next_rule(i, handle);
1703 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 found = 1;
1705 }
1706
1707 errno = ENOENT;
1708 return found;
1709}
1710
Harald Welte82dd2ec2000-12-19 05:18:15 +00001711static char *get_modprobe(void)
1712{
1713 int procfile;
1714 char *ret;
1715
Harald Welte10f7f142004-10-22 08:14:07 +00001716#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001717 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1718 if (procfile < 0)
1719 return NULL;
1720
Harald Welte10f7f142004-10-22 08:14:07 +00001721 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001722 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001723 memset(ret, 0, PROCFILE_BUFSIZ);
1724 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001725 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001726 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001727 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001728 if (ret[strlen(ret)-1]=='\n')
1729 ret[strlen(ret)-1]=0;
1730 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001731 return ret;
1732 }
1733 fail:
1734 free(ret);
1735 close(procfile);
1736 return NULL;
1737}
1738
Harald Welte58918652001-06-16 18:25:25 +00001739int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001740{
1741 char *buf = NULL;
1742 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001743 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001744
1745 /* If they don't explicitly set it, read out of kernel */
1746 if (!modprobe) {
1747 buf = get_modprobe();
1748 if (!buf)
1749 return -1;
1750 modprobe = buf;
1751 }
1752
1753 switch (fork()) {
1754 case 0:
1755 argv[0] = (char *)modprobe;
1756 argv[1] = (char *)modname;
1757 argv[2] = NULL;
1758 execv(argv[0], argv);
1759
1760 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001761 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001762 case -1:
1763 return -1;
1764
1765 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001766 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001767 }
1768
1769 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001770 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1771 return 0;
1772 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001773}
1774
Marc Bouchere6869a82000-03-20 06:03:29 +00001775static struct ipt_entry *
1776generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001777 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 struct ipt_entry_target *target)
1779{
1780 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001781 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001782 struct ipt_entry *e;
1783
1784 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001785 for (matchp = matches; matchp; matchp = matchp->next)
1786 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001787
Rusty Russell228e98d2000-04-27 10:28:06 +00001788 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001789 *e = *fw;
1790 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001791 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792
1793 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001794 for (matchp = matches; matchp; matchp = matchp->next) {
1795 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1796 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001798 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001799
1800 return e;
1801}
1802
Martin Josefsson78cafda2004-02-02 20:01:18 +00001803void clear_rule_matches(struct iptables_rule_match **matches)
1804{
1805 struct iptables_rule_match *matchp, *tmp;
1806
1807 for (matchp = *matches; matchp;) {
1808 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001809 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001810 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001811 matchp->match->m = NULL;
1812 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001813 free(matchp);
1814 matchp = tmp;
1815 }
1816
1817 *matches = NULL;
1818}
1819
Rusty Russell3aef54d2005-01-03 03:48:40 +00001820static void set_revision(char *name, u_int8_t revision)
1821{
1822 /* Old kernel sources don't have ".revision" field,
1823 but we stole a byte from name. */
1824 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1825 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1826}
1827
Phil Oester8cf65912005-09-19 15:00:33 +00001828void
1829get_kernel_version(void) {
1830 static struct utsname uts;
1831 int x = 0, y = 0, z = 0;
1832
1833 if (uname(&uts) == -1) {
1834 fprintf(stderr, "Unable to retrieve kernel version.\n");
1835 free_opts(1);
1836 exit(1);
1837 }
1838
1839 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1840 kernel_version = LINUX_VERSION(x, y, z);
1841}
1842
Marc Bouchere6869a82000-03-20 06:03:29 +00001843int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1844{
1845 struct ipt_entry fw, *e = NULL;
1846 int invert = 0;
1847 unsigned int nsaddrs = 0, ndaddrs = 0;
1848 struct in_addr *saddrs = NULL, *daddrs = NULL;
1849
1850 int c, verbose = 0;
1851 const char *chain = NULL;
1852 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1853 const char *policy = NULL, *newname = NULL;
1854 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001855 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 int ret = 1;
1857 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001858 struct iptables_rule_match *matches = NULL;
1859 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001861 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 const char *jumpto = "";
1863 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001864 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001865 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
1867 memset(&fw, 0, sizeof(fw));
1868
Harald Welteae1ff9f2000-12-01 14:26:20 +00001869 /* re-set optind to 0 in case do_command gets called
1870 * a second time */
1871 optind = 0;
1872
1873 /* clear mflags in case do_command gets called a second time
1874 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001875 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001876 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001877
1878 for (t = iptables_targets; t; t = t->next) {
1879 t->tflags = 0;
1880 t->used = 0;
1881 }
1882
Marc Bouchere6869a82000-03-20 06:03:29 +00001883 /* Suppress error messages: we may add new options if we
1884 demand-load a protocol. */
1885 opterr = 0;
1886
1887 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001888 "-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 +00001889 opts, NULL)) != -1) {
1890 switch (c) {
1891 /*
1892 * Command selection
1893 */
1894 case 'A':
1895 add_command(&command, CMD_APPEND, CMD_NONE,
1896 invert);
1897 chain = optarg;
1898 break;
1899
1900 case 'D':
1901 add_command(&command, CMD_DELETE, CMD_NONE,
1902 invert);
1903 chain = optarg;
1904 if (optind < argc && argv[optind][0] != '-'
1905 && argv[optind][0] != '!') {
1906 rulenum = parse_rulenumber(argv[optind++]);
1907 command = CMD_DELETE_NUM;
1908 }
1909 break;
1910
Marc Bouchere6869a82000-03-20 06:03:29 +00001911 case 'R':
1912 add_command(&command, CMD_REPLACE, CMD_NONE,
1913 invert);
1914 chain = optarg;
1915 if (optind < argc && argv[optind][0] != '-'
1916 && argv[optind][0] != '!')
1917 rulenum = parse_rulenumber(argv[optind++]);
1918 else
1919 exit_error(PARAMETER_PROBLEM,
1920 "-%c requires a rule number",
1921 cmd2char(CMD_REPLACE));
1922 break;
1923
1924 case 'I':
1925 add_command(&command, CMD_INSERT, CMD_NONE,
1926 invert);
1927 chain = optarg;
1928 if (optind < argc && argv[optind][0] != '-'
1929 && argv[optind][0] != '!')
1930 rulenum = parse_rulenumber(argv[optind++]);
1931 else rulenum = 1;
1932 break;
1933
1934 case 'L':
1935 add_command(&command, CMD_LIST, CMD_ZERO,
1936 invert);
1937 if (optarg) chain = optarg;
1938 else if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 chain = argv[optind++];
1941 break;
1942
1943 case 'F':
1944 add_command(&command, CMD_FLUSH, CMD_NONE,
1945 invert);
1946 if (optarg) chain = optarg;
1947 else if (optind < argc && argv[optind][0] != '-'
1948 && argv[optind][0] != '!')
1949 chain = argv[optind++];
1950 break;
1951
1952 case 'Z':
1953 add_command(&command, CMD_ZERO, CMD_LIST,
1954 invert);
1955 if (optarg) chain = optarg;
1956 else if (optind < argc && argv[optind][0] != '-'
1957 && argv[optind][0] != '!')
1958 chain = argv[optind++];
1959 break;
1960
1961 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001962 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001963 exit_error(PARAMETER_PROBLEM,
1964 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001965 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001966 if (find_target(optarg, TRY_LOAD))
1967 exit_error(PARAMETER_PROBLEM,
1968 "chain name may not clash "
1969 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001970 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1971 invert);
1972 chain = optarg;
1973 break;
1974
1975 case 'X':
1976 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1977 invert);
1978 if (optarg) chain = optarg;
1979 else if (optind < argc && argv[optind][0] != '-'
1980 && argv[optind][0] != '!')
1981 chain = argv[optind++];
1982 break;
1983
1984 case 'E':
1985 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1986 invert);
1987 chain = optarg;
1988 if (optind < argc && argv[optind][0] != '-'
1989 && argv[optind][0] != '!')
1990 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001991 else
1992 exit_error(PARAMETER_PROBLEM,
1993 "-%c requires old-chain-name and "
1994 "new-chain-name",
1995 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001996 break;
1997
1998 case 'P':
1999 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2000 invert);
2001 chain = optarg;
2002 if (optind < argc && argv[optind][0] != '-'
2003 && argv[optind][0] != '!')
2004 policy = argv[optind++];
2005 else
2006 exit_error(PARAMETER_PROBLEM,
2007 "-%c requires a chain and a policy",
2008 cmd2char(CMD_SET_POLICY));
2009 break;
2010
2011 case 'h':
2012 if (!optarg)
2013 optarg = argv[optind];
2014
Rusty Russell2e0a3212000-04-19 11:23:18 +00002015 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002016 if (!matches && protocol)
2017 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002018
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002019 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002020
2021 /*
2022 * Option selection
2023 */
2024 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002025 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2027 invert);
2028
2029 /* Canonicalize into lower case */
2030 for (protocol = argv[optind-1]; *protocol; protocol++)
2031 *protocol = tolower(*protocol);
2032
2033 protocol = argv[optind-1];
2034 fw.ip.proto = parse_protocol(protocol);
2035
2036 if (fw.ip.proto == 0
2037 && (fw.ip.invflags & IPT_INV_PROTO))
2038 exit_error(PARAMETER_PROBLEM,
2039 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 break;
2041
2042 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002043 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002044 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2045 invert);
2046 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 break;
2048
2049 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002050 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002051 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2052 invert);
2053 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002054 break;
2055
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002056#ifdef IPT_F_GOTO
2057 case 'g':
2058 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2059 invert);
2060 fw.ip.flags |= IPT_F_GOTO;
2061 jumpto = parse_target(optarg);
2062 break;
2063#endif
2064
Marc Bouchere6869a82000-03-20 06:03:29 +00002065 case 'j':
2066 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2067 invert);
2068 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002069 /* TRY_LOAD (may be chain name) */
2070 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002071
2072 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002073 size_t size;
2074
Rusty Russell73f72f52000-07-03 10:17:57 +00002075 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2076 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002077
Rusty Russell2e0a3212000-04-19 11:23:18 +00002078 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002079 target->t->u.target_size = size;
2080 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002081 set_revision(target->t->u.user.name,
2082 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002083 if (target->init != NULL)
2084 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002085 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002086 }
2087 break;
2088
2089
2090 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002091 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002092 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2093 invert);
2094 parse_interface(argv[optind-1],
2095 fw.ip.iniface,
2096 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002097 break;
2098
2099 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002100 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002101 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2102 invert);
2103 parse_interface(argv[optind-1],
2104 fw.ip.outiface,
2105 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002106 break;
2107
2108 case 'f':
2109 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2110 invert);
2111 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002112 break;
2113
2114 case 'v':
2115 if (!verbose)
2116 set_option(&options, OPT_VERBOSE,
2117 &fw.ip.invflags, invert);
2118 verbose++;
2119 break;
2120
Rusty Russell52a51492000-05-02 16:44:29 +00002121 case 'm': {
2122 size_t size;
2123
Marc Bouchere6869a82000-03-20 06:03:29 +00002124 if (invert)
2125 exit_error(PARAMETER_PROBLEM,
2126 "unexpected ! flag before --match");
2127
Martin Josefsson78cafda2004-02-02 20:01:18 +00002128 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002129 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2130 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002131 m->m = fw_calloc(1, size);
2132 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002133 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002134 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002135 if (m->init != NULL)
2136 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002137 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002138 }
2139 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002140
2141 case 'n':
2142 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2143 invert);
2144 break;
2145
2146 case 't':
2147 if (invert)
2148 exit_error(PARAMETER_PROBLEM,
2149 "unexpected ! flag before --table");
2150 *table = argv[optind-1];
2151 break;
2152
2153 case 'x':
2154 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2155 invert);
2156 break;
2157
2158 case 'V':
2159 if (invert)
2160 printf("Not %s ;-)\n", program_version);
2161 else
2162 printf("%s v%s\n",
2163 program_name, program_version);
2164 exit(0);
2165
2166 case '0':
2167 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2168 invert);
2169 break;
2170
Harald Welte82dd2ec2000-12-19 05:18:15 +00002171 case 'M':
2172 modprobe = optarg;
2173 break;
2174
Harald Welteccd49e52001-01-23 22:54:34 +00002175 case 'c':
2176
2177 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2178 invert);
2179 pcnt = optarg;
2180 if (optind < argc && argv[optind][0] != '-'
2181 && argv[optind][0] != '!')
2182 bcnt = argv[optind++];
2183 else
2184 exit_error(PARAMETER_PROBLEM,
2185 "-%c requires packet and byte counter",
2186 opt2char(OPT_COUNTERS));
2187
Martin Josefssona28d4952004-05-26 16:04:48 +00002188 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002189 exit_error(PARAMETER_PROBLEM,
2190 "-%c packet counter not numeric",
2191 opt2char(OPT_COUNTERS));
2192
Martin Josefssona28d4952004-05-26 16:04:48 +00002193 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002194 exit_error(PARAMETER_PROBLEM,
2195 "-%c byte counter not numeric",
2196 opt2char(OPT_COUNTERS));
2197
2198 break;
2199
2200
Marc Bouchere6869a82000-03-20 06:03:29 +00002201 case 1: /* non option */
2202 if (optarg[0] == '!' && optarg[1] == '\0') {
2203 if (invert)
2204 exit_error(PARAMETER_PROBLEM,
2205 "multiple consecutive ! not"
2206 " allowed");
2207 invert = TRUE;
2208 optarg[0] = '\0';
2209 continue;
2210 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002211 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002212 exit_tryhelp(2);
2213
2214 default:
2215 /* FIXME: This scheme doesn't allow two of the same
2216 matches --RR */
2217 if (!target
2218 || !(target->parse(c - target->option_offset,
2219 argv, invert,
2220 &target->tflags,
2221 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002222 for (matchp = matches; matchp; matchp = matchp->next) {
2223 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002224 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002225 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002226 &fw,
2227 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002228 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002229 break;
2230 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002231 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002232
2233 /* If you listen carefully, you can
2234 actually hear this code suck. */
2235
2236 /* some explanations (after four different bugs
2237 * in 3 different releases): If we encountere a
2238 * parameter, that has not been parsed yet,
2239 * it's not an option of an explicitly loaded
2240 * match or a target. However, we support
2241 * implicit loading of the protocol match
2242 * extension. '-p tcp' means 'l4 proto 6' and
2243 * at the same time 'load tcp protocol match on
2244 * demand if we specify --dport'.
2245 *
2246 * To make this work, we need to make sure:
2247 * - the parameter has not been parsed by
2248 * a match (m above)
2249 * - a protocol has been specified
2250 * - the protocol extension has not been
2251 * loaded yet, or is loaded and unused
2252 * [think of iptables-restore!]
2253 * - the protocol extension can be successively
2254 * loaded
2255 */
2256 if (m == NULL
2257 && protocol
2258 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002259 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002260 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002261 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002262 && (proto_used == 0))
2263 )
2264 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002265 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002266 /* Try loading protocol */
2267 size_t size;
2268
2269 proto_used = 1;
2270
2271 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2272 + m->size;
2273
2274 m->m = fw_calloc(1, size);
2275 m->m->u.match_size = size;
2276 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002277 set_revision(m->m->u.user.name,
2278 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002279 if (m->init != NULL)
2280 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002281
2282 opts = merge_options(opts,
2283 m->extra_opts, &m->option_offset);
2284
2285 optind--;
2286 continue;
2287 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002288 if (!m)
2289 exit_error(PARAMETER_PROBLEM,
2290 "Unknown arg `%s'",
2291 argv[optind-1]);
2292 }
2293 }
2294 invert = FALSE;
2295 }
2296
Martin Josefsson78cafda2004-02-02 20:01:18 +00002297 for (matchp = matches; matchp; matchp = matchp->next)
2298 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002299
Marc Bouchere6869a82000-03-20 06:03:29 +00002300 if (target)
2301 target->final_check(target->tflags);
2302
2303 /* Fix me: must put inverse options checking here --MN */
2304
2305 if (optind < argc)
2306 exit_error(PARAMETER_PROBLEM,
2307 "unknown arguments found on commandline");
2308 if (!command)
2309 exit_error(PARAMETER_PROBLEM, "no command specified");
2310 if (invert)
2311 exit_error(PARAMETER_PROBLEM,
2312 "nothing appropriate following !");
2313
Harald Welte6336bfd2002-05-07 14:41:43 +00002314 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002315 if (!(options & OPT_DESTINATION))
2316 dhostnetworkmask = "0.0.0.0/0";
2317 if (!(options & OPT_SOURCE))
2318 shostnetworkmask = "0.0.0.0/0";
2319 }
2320
2321 if (shostnetworkmask)
2322 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2323 &(fw.ip.smsk), &nsaddrs);
2324
2325 if (dhostnetworkmask)
2326 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2327 &(fw.ip.dmsk), &ndaddrs);
2328
2329 if ((nsaddrs > 1 || ndaddrs > 1) &&
2330 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2331 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2332 " source or destination IP addresses");
2333
Marc Bouchere6869a82000-03-20 06:03:29 +00002334 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2335 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2336 "specify a unique address");
2337
2338 generic_opt_check(command, options);
2339
2340 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2341 exit_error(PARAMETER_PROBLEM,
2342 "chain name `%s' too long (must be under %i chars)",
2343 chain, IPT_FUNCTION_MAXNAMELEN);
2344
Harald Welteae1ff9f2000-12-01 14:26:20 +00002345 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002346 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002347 *handle = iptc_init(*table);
2348
Rusty Russell8beb0492004-12-22 00:37:10 +00002349 /* try to insmod the module if iptc_init failed */
2350 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002351 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002352
Marc Bouchere6869a82000-03-20 06:03:29 +00002353 if (!*handle)
2354 exit_error(VERSION_PROBLEM,
2355 "can't initialize iptables table `%s': %s",
2356 *table, iptc_strerror(errno));
2357
Harald Welte6336bfd2002-05-07 14:41:43 +00002358 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002359 || command == CMD_DELETE
2360 || command == CMD_INSERT
2361 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002362 if (strcmp(chain, "PREROUTING") == 0
2363 || strcmp(chain, "INPUT") == 0) {
2364 /* -o not valid with incoming packets. */
2365 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002366 exit_error(PARAMETER_PROBLEM,
2367 "Can't use -%c with %s\n",
2368 opt2char(OPT_VIANAMEOUT),
2369 chain);
2370 }
2371
Rusty Russella4860fd2000-06-17 16:13:02 +00002372 if (strcmp(chain, "POSTROUTING") == 0
2373 || strcmp(chain, "OUTPUT") == 0) {
2374 /* -i not valid with outgoing packets */
2375 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002376 exit_error(PARAMETER_PROBLEM,
2377 "Can't use -%c with %s\n",
2378 opt2char(OPT_VIANAMEIN),
2379 chain);
2380 }
2381
2382 if (target && iptc_is_chain(jumpto, *handle)) {
2383 printf("Warning: using chain %s, not extension\n",
2384 jumpto);
2385
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002386 if (target->t)
2387 free(target->t);
2388
Marc Bouchere6869a82000-03-20 06:03:29 +00002389 target = NULL;
2390 }
2391
2392 /* If they didn't specify a target, or it's a chain
2393 name, use standard. */
2394 if (!target
2395 && (strlen(jumpto) == 0
2396 || iptc_is_chain(jumpto, *handle))) {
2397 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002398
Rusty Russell52a51492000-05-02 16:44:29 +00002399 target = find_target(IPT_STANDARD_TARGET,
2400 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002401
2402 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002403 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002404 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002405 target->t->u.target_size = size;
2406 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002407 if (!iptc_is_chain(jumpto, *handle))
2408 set_revision(target->t->u.user.name,
2409 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002410 if (target->init != NULL)
2411 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002412 }
2413
Rusty Russell7e53bf92000-03-20 07:03:28 +00002414 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002415 /* it is no chain, and we can't load a plugin.
2416 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002417 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002418 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002419#ifdef IPT_F_GOTO
2420 if (fw.ip.flags & IPT_F_GOTO)
2421 exit_error(PARAMETER_PROBLEM,
2422 "goto '%s' is not a chain\n", jumpto);
2423#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002424 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002425 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002426 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002427 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002428 }
2429 }
2430
2431 switch (command) {
2432 case CMD_APPEND:
2433 ret = append_entry(chain, e,
2434 nsaddrs, saddrs, ndaddrs, daddrs,
2435 options&OPT_VERBOSE,
2436 handle);
2437 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002438 case CMD_DELETE:
2439 ret = delete_entry(chain, e,
2440 nsaddrs, saddrs, ndaddrs, daddrs,
2441 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002442 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002443 break;
2444 case CMD_DELETE_NUM:
2445 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2446 break;
2447 case CMD_REPLACE:
2448 ret = replace_entry(chain, e, rulenum - 1,
2449 saddrs, daddrs, options&OPT_VERBOSE,
2450 handle);
2451 break;
2452 case CMD_INSERT:
2453 ret = insert_entry(chain, e, rulenum - 1,
2454 nsaddrs, saddrs, ndaddrs, daddrs,
2455 options&OPT_VERBOSE,
2456 handle);
2457 break;
2458 case CMD_LIST:
2459 ret = list_entries(chain,
2460 options&OPT_VERBOSE,
2461 options&OPT_NUMERIC,
2462 options&OPT_EXPANDED,
2463 options&OPT_LINENUMBERS,
2464 handle);
2465 break;
2466 case CMD_FLUSH:
2467 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2468 break;
2469 case CMD_ZERO:
2470 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2471 break;
2472 case CMD_LIST|CMD_ZERO:
2473 ret = list_entries(chain,
2474 options&OPT_VERBOSE,
2475 options&OPT_NUMERIC,
2476 options&OPT_EXPANDED,
2477 options&OPT_LINENUMBERS,
2478 handle);
2479 if (ret)
2480 ret = zero_entries(chain,
2481 options&OPT_VERBOSE, handle);
2482 break;
2483 case CMD_NEW_CHAIN:
2484 ret = iptc_create_chain(chain, handle);
2485 break;
2486 case CMD_DELETE_CHAIN:
2487 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2488 break;
2489 case CMD_RENAME_CHAIN:
2490 ret = iptc_rename_chain(chain, newname, handle);
2491 break;
2492 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002493 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002494 break;
2495 default:
2496 /* We should never reach this... */
2497 exit_tryhelp(2);
2498 }
2499
2500 if (verbose > 1)
2501 dump_entries(*handle);
2502
Martin Josefsson78cafda2004-02-02 20:01:18 +00002503 clear_rule_matches(&matches);
2504
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002505 if (e != NULL) {
2506 free(e);
2507 e = NULL;
2508 }
2509
keso6997cdf2004-07-04 15:20:53 +00002510 free(saddrs);
2511 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002512 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002513
Marc Bouchere6869a82000-03-20 06:03:29 +00002514 return ret;
2515}