blob: 7e8ba59ef61d1137e38f03bea0b67a225678c105 [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>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
Harald Welte82dd2ec2000-12-19 05:18:15 +000050#ifndef PROC_SYS_MODPROBE
51#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
52#endif
53
Marc Bouchere6869a82000-03-20 06:03:29 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
81#define CMD_CHECK 0x0800U
82#define CMD_RENAME_CHAIN 0x1000U
83#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000085 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000086
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_FRAGMENT 0x00200U
100#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000101#define OPT_COUNTERS 0x00800U
102#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000103static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000104= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000105
106static struct option original_opts[] = {
107 { "append", 1, 0, 'A' },
108 { "delete", 1, 0, 'D' },
109 { "insert", 1, 0, 'I' },
110 { "replace", 1, 0, 'R' },
111 { "list", 2, 0, 'L' },
112 { "flush", 2, 0, 'F' },
113 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000116 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000122 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "fragments", 0, 0, 'f' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000135 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000136 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000137 { 0 }
138};
139
Illes Marci63e90632003-03-03 08:08:37 +0000140/* we need this for iptables-restore. iptables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. iptables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000194char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195
Rusty Russell2e0a3212000-04-19 11:23:18 +0000196/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000197struct iptables_match *iptables_matches = NULL;
198struct iptables_target *iptables_targets = NULL;
199
200/* Extra debugging from libiptc */
201extern void dump_entries(const iptc_handle_t handle);
202
203/* A few hardcoded protocols for 'all' and in case the user has no
204 /etc/protocols */
205struct pprot {
206 char *name;
207 u_int8_t num;
208};
209
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000210/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000211/* defined in netinet/in.h */
212#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000213#ifndef IPPROTO_ESP
214#define IPPROTO_ESP 50
215#endif
216#ifndef IPPROTO_AH
217#define IPPROTO_AH 51
218#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000219#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000220
Marc Bouchere6869a82000-03-20 06:03:29 +0000221static const struct pprot chain_protos[] = {
222 { "tcp", IPPROTO_TCP },
223 { "udp", IPPROTO_UDP },
224 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000225 { "esp", IPPROTO_ESP },
226 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000227 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 { "all", 0 },
229};
230
231static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000232proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000233{
234 unsigned int i;
235
Rusty Russell28381a42000-05-10 00:19:50 +0000236 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000237 struct protoent *pent = getprotobynumber(proto);
238 if (pent)
239 return pent->p_name;
240 }
241
242 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
243 if (chain_protos[i].num == proto)
244 return chain_protos[i].name;
245
246 return NULL;
247}
248
249struct in_addr *
250dotted_to_addr(const char *dotted)
251{
252 static struct in_addr addr;
253 unsigned char *addrp;
254 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000255 unsigned int onebyte;
256 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000257 char buf[20];
258
259 /* copy dotted string, because we need to modify it */
260 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000261 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000262 addrp = (unsigned char *) &(addr.s_addr);
263
264 p = buf;
265 for (i = 0; i < 3; i++) {
266 if ((q = strchr(p, '.')) == NULL)
267 return (struct in_addr *) NULL;
268
269 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000270 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000271 return (struct in_addr *) NULL;
272
273 addrp[i] = (unsigned char) onebyte;
274 p = q + 1;
275 }
276
277 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000278 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000279 return (struct in_addr *) NULL;
280
281 addrp[3] = (unsigned char) onebyte;
282
283 return &addr;
284}
285
286static struct in_addr *
287network_to_addr(const char *name)
288{
289 struct netent *net;
290 static struct in_addr addr;
291
292 if ((net = getnetbyname(name)) != NULL) {
293 if (net->n_addrtype != AF_INET)
294 return (struct in_addr *) NULL;
295 addr.s_addr = htonl((unsigned long) net->n_net);
296 return &addr;
297 }
298
299 return (struct in_addr *) NULL;
300}
301
302static void
303inaddrcpy(struct in_addr *dst, struct in_addr *src)
304{
305 /* memcpy(dst, src, sizeof(struct in_addr)); */
306 dst->s_addr = src->s_addr;
307}
308
Pablo Neiradfdcd642005-05-29 19:05:23 +0000309static void free_opts(int reset_offset)
310{
311 if (opts != original_opts) {
312 free(opts);
313 opts = original_opts;
314 if (reset_offset)
315 global_option_offset = 0;
316 }
317}
318
Marc Bouchere6869a82000-03-20 06:03:29 +0000319void
320exit_error(enum exittype status, char *msg, ...)
321{
322 va_list args;
323
324 va_start(args, msg);
325 fprintf(stderr, "%s v%s: ", program_name, program_version);
326 vfprintf(stderr, msg, args);
327 va_end(args);
328 fprintf(stderr, "\n");
329 if (status == PARAMETER_PROBLEM)
330 exit_tryhelp(status);
331 if (status == VERSION_PROBLEM)
332 fprintf(stderr,
333 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000334 /* On error paths, make sure that we don't leak memory */
335 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000336 exit(status);
337}
338
339void
340exit_tryhelp(int status)
341{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000342 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000343 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000344 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
345 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000346 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 exit(status);
348}
349
350void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000352{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000353 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000354 struct iptables_target *t = NULL;
355
Marc Bouchere6869a82000-03-20 06:03:29 +0000356 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000357"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000358" %s -[RI] chain rulenum rule-specification [options]\n"
359" %s -D chain rulenum [options]\n"
360" %s -[LFZ] [chain] [options]\n"
361" %s -[NX] chain\n"
362" %s -E old-chain-name new-chain-name\n"
363" %s -P chain target [options]\n"
364" %s -h (print this help information)\n\n",
365 program_name, program_version, program_name, program_name,
366 program_name, program_name, program_name, program_name,
367 program_name, program_name);
368
369 printf(
370"Commands:\n"
371"Either long or short options are allowed.\n"
372" --append -A chain Append to chain\n"
373" --delete -D chain Delete matching rule from chain\n"
374" --delete -D chain rulenum\n"
375" Delete rule rulenum (1 = first) from chain\n"
376" --insert -I chain [rulenum]\n"
377" Insert in chain as rulenum (default 1=first)\n"
378" --replace -R chain rulenum\n"
379" Replace rule rulenum (1 = first) in chain\n"
380" --list -L [chain] List the rules in a chain or all chains\n"
381" --flush -F [chain] Delete all rules in chain or all chains\n"
382" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000383" --new -N chain Create a new user-defined chain\n"
384" --delete-chain\n"
385" -X [chain] Delete a user-defined chain\n"
386" --policy -P chain target\n"
387" Change policy on chain to target\n"
388" --rename-chain\n"
389" -E old-chain new-chain\n"
390" Change chain name, (moving any references)\n"
391
392"Options:\n"
393" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
394" --source -s [!] address[/mask]\n"
395" source specification\n"
396" --destination -d [!] address[/mask]\n"
397" destination specification\n"
398" --in-interface -i [!] input name[+]\n"
399" network interface name ([+] for wildcard)\n"
400" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000401" target for rule (may load target extension)\n"
402" --match -m match\n"
403" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000404" --numeric -n numeric output of addresses and ports\n"
405" --out-interface -o [!] output name[+]\n"
406" network interface name ([+] for wildcard)\n"
407" --table -t table table to manipulate (default: `filter')\n"
408" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000409" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000410" --exact -x expand numbers (display exact values)\n"
411"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000412" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000413" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000414"[!] --version -V print package version.\n");
415
Rusty Russell363112d2000-08-11 13:49:26 +0000416 /* Print out any special helps. A user might like to be able
417 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000418 results. So we call help for all specified matches & targets */
419 for (t = iptables_targets; t ;t = t->next) {
420 if (t->used) {
421 printf("\n");
422 t->help();
423 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000424 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000425 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000426 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000427 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000428 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000429 exit(0);
430}
431
432static void
433generic_opt_check(int command, int options)
434{
435 int i, j, legal = 0;
436
437 /* Check that commands are valid with options. Complicated by the
438 * fact that if an option is legal with *any* command given, it is
439 * legal overall (ie. -z and -l).
440 */
441 for (i = 0; i < NUMBER_OF_OPT; i++) {
442 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
443
444 for (j = 0; j < NUMBER_OF_CMD; j++) {
445 if (!(command & (1<<j)))
446 continue;
447
448 if (!(options & (1<<i))) {
449 if (commands_v_options[j][i] == '+')
450 exit_error(PARAMETER_PROBLEM,
451 "You need to supply the `-%c' "
452 "option for this command\n",
453 optflags[i]);
454 } else {
455 if (commands_v_options[j][i] != 'x')
456 legal = 1;
457 else if (legal == 0)
458 legal = -1;
459 }
460 }
461 if (legal == -1)
462 exit_error(PARAMETER_PROBLEM,
463 "Illegal option `-%c' with this command\n",
464 optflags[i]);
465 }
466}
467
468static char
469opt2char(int option)
470{
471 const char *ptr;
472 for (ptr = optflags; option > 1; option >>= 1, ptr++);
473
474 return *ptr;
475}
476
477static char
478cmd2char(int option)
479{
480 const char *ptr;
481 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
482
483 return *ptr;
484}
485
486static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000487add_command(unsigned int *cmd, const int newcmd, const int othercmds,
488 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000489{
490 if (invert)
491 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
492 if (*cmd & (~othercmds))
493 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
494 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
495 *cmd |= newcmd;
496}
497
498int
Harald Welteb77f1da2002-03-14 11:35:58 +0000499check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000500{
501 if (option && strcmp(option, "!") == 0) {
502 if (*invert)
503 exit_error(PARAMETER_PROBLEM,
504 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000505 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000506 if (optind) {
507 *optind = *optind+1;
508 if (argc && *optind > argc)
509 exit_error(PARAMETER_PROBLEM,
510 "no argument following `!'");
511 }
512
Marc Bouchere6869a82000-03-20 06:03:29 +0000513 return TRUE;
514 }
515 return FALSE;
516}
517
518static void *
519fw_calloc(size_t count, size_t size)
520{
521 void *p;
522
523 if ((p = calloc(count, size)) == NULL) {
524 perror("iptables: calloc failed");
525 exit(1);
526 }
527 return p;
528}
529
530static void *
531fw_malloc(size_t size)
532{
533 void *p;
534
535 if ((p = malloc(size)) == NULL) {
536 perror("iptables: malloc failed");
537 exit(1);
538 }
539 return p;
540}
541
542static struct in_addr *
543host_to_addr(const char *name, unsigned int *naddr)
544{
545 struct hostent *host;
546 struct in_addr *addr;
547 unsigned int i;
548
549 *naddr = 0;
550 if ((host = gethostbyname(name)) != NULL) {
551 if (host->h_addrtype != AF_INET ||
552 host->h_length != sizeof(struct in_addr))
553 return (struct in_addr *) NULL;
554
555 while (host->h_addr_list[*naddr] != (char *) NULL)
556 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000557 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000558 for (i = 0; i < *naddr; i++)
559 inaddrcpy(&(addr[i]),
560 (struct in_addr *) host->h_addr_list[i]);
561 return addr;
562 }
563
564 return (struct in_addr *) NULL;
565}
566
567static char *
568addr_to_host(const struct in_addr *addr)
569{
570 struct hostent *host;
571
572 if ((host = gethostbyaddr((char *) addr,
573 sizeof(struct in_addr), AF_INET)) != NULL)
574 return (char *) host->h_name;
575
576 return (char *) NULL;
577}
578
579/*
580 * All functions starting with "parse" should succeed, otherwise
581 * the program fails.
582 * Most routines return pointers to static data that may change
583 * between calls to the same or other routines with a few exceptions:
584 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
585 * return global static data.
586*/
587
588static struct in_addr *
589parse_hostnetwork(const char *name, unsigned int *naddrs)
590{
591 struct in_addr *addrp, *addrptmp;
592
593 if ((addrptmp = dotted_to_addr(name)) != NULL ||
594 (addrptmp = network_to_addr(name)) != NULL) {
595 addrp = fw_malloc(sizeof(struct in_addr));
596 inaddrcpy(addrp, addrptmp);
597 *naddrs = 1;
598 return addrp;
599 }
600 if ((addrp = host_to_addr(name, naddrs)) != NULL)
601 return addrp;
602
603 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
604}
605
606static struct in_addr *
607parse_mask(char *mask)
608{
609 static struct in_addr maskaddr;
610 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000611 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000612
613 if (mask == NULL) {
614 /* no mask at all defaults to 32 bits */
615 maskaddr.s_addr = 0xFFFFFFFF;
616 return &maskaddr;
617 }
618 if ((addrp = dotted_to_addr(mask)) != NULL)
619 /* dotted_to_addr already returns a network byte order addr */
620 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000621 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000622 exit_error(PARAMETER_PROBLEM,
623 "invalid mask `%s' specified", mask);
624 if (bits != 0) {
625 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
626 return &maskaddr;
627 }
628
629 maskaddr.s_addr = 0L;
630 return &maskaddr;
631}
632
Marc Boucherb93c7982001-12-06 14:50:19 +0000633void
Marc Bouchere6869a82000-03-20 06:03:29 +0000634parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
635 struct in_addr *maskp, unsigned int *naddrs)
636{
637 struct in_addr *addrp;
638 char buf[256];
639 char *p;
640 int i, j, k, n;
641
642 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000643 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000644 if ((p = strrchr(buf, '/')) != NULL) {
645 *p = '\0';
646 addrp = parse_mask(p + 1);
647 } else
648 addrp = parse_mask(NULL);
649 inaddrcpy(maskp, addrp);
650
651 /* if a null mask is given, the name is ignored, like in "any/0" */
652 if (maskp->s_addr == 0L)
653 strcpy(buf, "0.0.0.0");
654
655 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
656 n = *naddrs;
657 for (i = 0, j = 0; i < n; i++) {
658 addrp[j++].s_addr &= maskp->s_addr;
659 for (k = 0; k < j - 1; k++) {
660 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
661 (*naddrs)--;
662 j--;
663 break;
664 }
665 }
666 }
667}
668
669struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000670find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000671{
672 struct iptables_match *ptr;
673
674 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
675 if (strcmp(name, ptr->name) == 0)
676 break;
677 }
678
Harald Welte3efb6ea2001-08-06 18:50:21 +0000679#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000680 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000681 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000682 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000683 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000684 if (dlopen(path, RTLD_NOW)) {
685 /* Found library. If it didn't register itself,
686 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000687 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000688
Rusty Russell9e1d2142000-04-23 09:11:12 +0000689 if (!ptr)
690 exit_error(PARAMETER_PROBLEM,
691 "Couldn't load match `%s'\n",
692 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000693 } else if (tryload == LOAD_MUST_SUCCEED)
694 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000695 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000696 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000697 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000698#else
699 if (ptr && !ptr->loaded) {
700 if (tryload != DONT_LOAD)
701 ptr->loaded = 1;
702 else
703 ptr = NULL;
704 }
Marc Boucher067477b2002-03-24 15:09:31 +0000705 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
706 exit_error(PARAMETER_PROBLEM,
707 "Couldn't find match `%s'\n", name);
708 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000709#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000710
Martin Josefsson78cafda2004-02-02 20:01:18 +0000711 if (ptr && matches) {
712 struct iptables_rule_match **i;
713 struct iptables_rule_match *newentry;
714
715 newentry = fw_malloc(sizeof(struct iptables_rule_match));
716
717 for (i = matches; *i; i = &(*i)->next);
718 newentry->match = ptr;
719 newentry->next = NULL;
720 *i = newentry;
721 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000722
Marc Bouchere6869a82000-03-20 06:03:29 +0000723 return ptr;
724}
725
Rusty Russell28381a42000-05-10 00:19:50 +0000726/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
727static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000728find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000729{
Harald Welteed498492001-07-23 01:24:22 +0000730 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000731
Harald Welte0b0013a2002-02-18 16:15:31 +0000732 if (string_to_number(pname, 0, 255, &proto) != -1) {
733 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000734
Harald Welte0b0013a2002-02-18 16:15:31 +0000735 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000736 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000737 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000738 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000739
740 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000741}
742
Marc Boucherb93c7982001-12-06 14:50:19 +0000743u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000744parse_protocol(const char *s)
745{
Harald Welteed498492001-07-23 01:24:22 +0000746 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000747
Harald Welteed498492001-07-23 01:24:22 +0000748 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 struct protoent *pent;
750
751 if ((pent = getprotobyname(s)))
752 proto = pent->p_proto;
753 else {
754 unsigned int i;
755 for (i = 0;
756 i < sizeof(chain_protos)/sizeof(struct pprot);
757 i++) {
758 if (strcmp(s, chain_protos[i].name) == 0) {
759 proto = chain_protos[i].num;
760 break;
761 }
762 }
763 if (i == sizeof(chain_protos)/sizeof(struct pprot))
764 exit_error(PARAMETER_PROBLEM,
765 "unknown protocol `%s' specified",
766 s);
767 }
768 }
769
770 return (u_int16_t)proto;
771}
772
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000773void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000774{
775 int vialen = strlen(arg);
776 unsigned int i;
777
778 memset(mask, 0, IFNAMSIZ);
779 memset(vianame, 0, IFNAMSIZ);
780
781 if (vialen + 1 > IFNAMSIZ)
782 exit_error(PARAMETER_PROBLEM,
783 "interface name `%s' must be shorter than IFNAMSIZ"
784 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000785
Marc Bouchere6869a82000-03-20 06:03:29 +0000786 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000787 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000788 memset(mask, 0, IFNAMSIZ);
789 else if (vianame[vialen - 1] == '+') {
790 memset(mask, 0xFF, vialen - 1);
791 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000792 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000793 } else {
794 /* Include nul-terminator in match */
795 memset(mask, 0xFF, vialen + 1);
796 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000797 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000798 if (!isalnum(vianame[i])
799 && vianame[i] != '_'
800 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000801 printf("Warning: wierd character in interface"
802 " `%s' (No aliases, :, ! or *).\n",
803 vianame);
804 break;
805 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000806 }
807 }
808}
809
810/* Can't be zero. */
811static int
812parse_rulenumber(const char *rule)
813{
Harald Welteed498492001-07-23 01:24:22 +0000814 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000815
Harald Welteed498492001-07-23 01:24:22 +0000816 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000817 exit_error(PARAMETER_PROBLEM,
818 "Invalid rule number `%s'", rule);
819
820 return rulenum;
821}
822
823static const char *
824parse_target(const char *targetname)
825{
826 const char *ptr;
827
828 if (strlen(targetname) < 1)
829 exit_error(PARAMETER_PROBLEM,
830 "Invalid target name (too short)");
831
832 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
833 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000834 "Invalid target name `%s' (%u chars max)",
835 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000836
837 for (ptr = targetname; *ptr; ptr++)
838 if (isspace(*ptr))
839 exit_error(PARAMETER_PROBLEM,
840 "Invalid target name `%s'", targetname);
841 return targetname;
842}
843
844static char *
845addr_to_network(const struct in_addr *addr)
846{
847 struct netent *net;
848
849 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
850 return (char *) net->n_name;
851
852 return (char *) NULL;
853}
854
855char *
856addr_to_dotted(const struct in_addr *addrp)
857{
858 static char buf[20];
859 const unsigned char *bytep;
860
861 bytep = (const unsigned char *) &(addrp->s_addr);
862 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
863 return buf;
864}
Marc Boucherb93c7982001-12-06 14:50:19 +0000865
866char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000867addr_to_anyname(const struct in_addr *addr)
868{
869 char *name;
870
871 if ((name = addr_to_host(addr)) != NULL ||
872 (name = addr_to_network(addr)) != NULL)
873 return name;
874
875 return addr_to_dotted(addr);
876}
877
Marc Boucherb93c7982001-12-06 14:50:19 +0000878char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000879mask_to_dotted(const struct in_addr *mask)
880{
881 int i;
882 static char buf[20];
883 u_int32_t maskaddr, bits;
884
885 maskaddr = ntohl(mask->s_addr);
886
887 if (maskaddr == 0xFFFFFFFFL)
888 /* we don't want to see "/32" */
889 return "";
890
891 i = 32;
892 bits = 0xFFFFFFFEL;
893 while (--i >= 0 && maskaddr != bits)
894 bits <<= 1;
895 if (i >= 0)
896 sprintf(buf, "/%d", i);
897 else
898 /* mask was not a decent combination of 1's and 0's */
899 sprintf(buf, "/%s", addr_to_dotted(mask));
900
901 return buf;
902}
903
904int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000905string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
906 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000907{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000909 char *end;
910
911 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000912 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000913 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000914 if (*end == '\0' && end != s) {
915 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000917 *ret = number;
918 return 0;
919 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000920 }
921 return -1;
922}
923
Martin Josefssonb105bc92004-05-26 15:54:49 +0000924int
925string_to_number_l(const char *s, unsigned long min, unsigned long max,
926 unsigned long *ret)
927{
928 int result;
929 unsigned long long number;
930
931 result = string_to_number_ll(s, min, max, &number);
932 *ret = (unsigned long)number;
933
934 return result;
935}
936
937int string_to_number(const char *s, unsigned int min, unsigned int max,
938 unsigned int *ret)
939{
940 int result;
941 unsigned long number;
942
943 result = string_to_number_l(s, min, max, &number);
944 *ret = (unsigned int)number;
945
946 return result;
947}
948
Marc Bouchere6869a82000-03-20 06:03:29 +0000949static void
950set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
951 int invert)
952{
953 if (*options & option)
954 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
955 opt2char(option));
956 *options |= option;
957
958 if (invert) {
959 unsigned int i;
960 for (i = 0; 1 << i != option; i++);
961
962 if (!inverse_for_options[i])
963 exit_error(PARAMETER_PROBLEM,
964 "cannot have ! before -%c",
965 opt2char(option));
966 *invflg |= inverse_for_options[i];
967 }
968}
969
970struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000971find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000972{
973 struct iptables_target *ptr;
974
975 /* Standard target? */
976 if (strcmp(name, "") == 0
977 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
978 || strcmp(name, IPTC_LABEL_DROP) == 0
979 || strcmp(name, IPTC_LABEL_QUEUE) == 0
980 || strcmp(name, IPTC_LABEL_RETURN) == 0)
981 name = "standard";
982
983 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
984 if (strcmp(name, ptr->name) == 0)
985 break;
986 }
987
Harald Welte3efb6ea2001-08-06 18:50:21 +0000988#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000989 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000990 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000991 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000992 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000993 if (dlopen(path, RTLD_NOW)) {
994 /* Found library. If it didn't register itself,
995 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000996 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000997 if (!ptr)
998 exit_error(PARAMETER_PROBLEM,
999 "Couldn't load target `%s'\n",
1000 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001001 } else if (tryload == LOAD_MUST_SUCCEED)
1002 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001003 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001004 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001006#else
1007 if (ptr && !ptr->loaded) {
1008 if (tryload != DONT_LOAD)
1009 ptr->loaded = 1;
1010 else
1011 ptr = NULL;
1012 }
Marc Boucher067477b2002-03-24 15:09:31 +00001013 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1014 exit_error(PARAMETER_PROBLEM,
1015 "Couldn't find target `%s'\n", name);
1016 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001017#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001018
Harald Welteae1ff9f2000-12-01 14:26:20 +00001019 if (ptr)
1020 ptr->used = 1;
1021
Marc Bouchere6869a82000-03-20 06:03:29 +00001022 return ptr;
1023}
1024
1025static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001026merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001027 unsigned int *option_offset)
1028{
1029 unsigned int num_old, num_new, i;
1030 struct option *merge;
1031
1032 for (num_old = 0; oldopts[num_old].name; num_old++);
1033 for (num_new = 0; newopts[num_new].name; num_new++);
1034
1035 global_option_offset += OPTION_OFFSET;
1036 *option_offset = global_option_offset;
1037
1038 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1039 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001040 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001041 for (i = 0; i < num_new; i++) {
1042 merge[num_old + i] = newopts[i];
1043 merge[num_old + i].val += *option_offset;
1044 }
1045 memset(merge + num_old + num_new, 0, sizeof(struct option));
1046
1047 return merge;
1048}
1049
Rusty Russell3aef54d2005-01-03 03:48:40 +00001050static int compatible_revision(const char *name, u_int8_t revision, int opt)
1051{
1052 struct ipt_get_revision rev;
1053 socklen_t s = sizeof(rev);
1054 int max_rev, sockfd;
1055
1056 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1057 if (sockfd < 0) {
1058 fprintf(stderr, "Could not open socket to kernel: %s\n",
1059 strerror(errno));
1060 exit(1);
1061 }
1062
1063 strcpy(rev.name, name);
1064 rev.revision = revision;
1065
1066 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1067 if (max_rev < 0) {
1068 /* Definitely don't support this? */
1069 if (errno == EPROTONOSUPPORT) {
1070 close(sockfd);
1071 return 0;
1072 } else if (errno == ENOPROTOOPT) {
1073 close(sockfd);
1074 /* Assume only revision 0 support (old kernel) */
1075 return (revision == 0);
1076 } else {
1077 fprintf(stderr, "getsockopt failed strangely: %s\n",
1078 strerror(errno));
1079 exit(1);
1080 }
1081 }
1082 close(sockfd);
1083 return 1;
1084}
1085
1086static int compatible_match_revision(const char *name, u_int8_t revision)
1087{
1088 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1089}
1090
1091static int compatible_target_revision(const char *name, u_int8_t revision)
1092{
1093 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1094}
1095
Marc Bouchere6869a82000-03-20 06:03:29 +00001096void
1097register_match(struct iptables_match *me)
1098{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001099 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001100
Marc Bouchere6869a82000-03-20 06:03:29 +00001101 if (strcmp(me->version, program_version) != 0) {
1102 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1103 program_name, me->name, me->version, program_version);
1104 exit(1);
1105 }
1106
Martin Josefsson93911bf2005-01-03 07:46:07 +00001107 /* Revision field stole a char from name. */
1108 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001109 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001110 program_name, me->name);
1111 exit(1);
1112 }
1113
Rusty Russell3aef54d2005-01-03 03:48:40 +00001114 old = find_match(me->name, DONT_LOAD, NULL);
1115 if (old) {
1116 if (old->revision == me->revision) {
1117 fprintf(stderr,
1118 "%s: match `%s' already registered.\n",
1119 program_name, me->name);
1120 exit(1);
1121 }
1122
1123 /* Now we have two (or more) options, check compatibility. */
1124 if (compatible_match_revision(old->name, old->revision)
1125 && old->revision > me->revision)
1126 return;
1127
1128 /* Replace if compatible. */
1129 if (!compatible_match_revision(me->name, me->revision))
1130 return;
1131
1132 /* Delete old one. */
1133 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1134 *i = old->next;
1135 }
1136
Rusty Russell73f72f52000-07-03 10:17:57 +00001137 if (me->size != IPT_ALIGN(me->size)) {
1138 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001139 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001140 exit(1);
1141 }
1142
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001143 /* Append to list. */
1144 for (i = &iptables_matches; *i; i = &(*i)->next);
1145 me->next = NULL;
1146 *i = me;
1147
Marc Bouchere6869a82000-03-20 06:03:29 +00001148 me->m = NULL;
1149 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001150}
1151
1152void
1153register_target(struct iptables_target *me)
1154{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001155 struct iptables_target *old;
1156
Marc Bouchere6869a82000-03-20 06:03:29 +00001157 if (strcmp(me->version, program_version) != 0) {
1158 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1159 program_name, me->name, me->version, program_version);
1160 exit(1);
1161 }
1162
Martin Josefsson93911bf2005-01-03 07:46:07 +00001163 /* Revision field stole a char from name. */
1164 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001165 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001166 program_name, me->name);
1167 exit(1);
1168 }
1169
Rusty Russell3aef54d2005-01-03 03:48:40 +00001170 old = find_target(me->name, DONT_LOAD);
1171 if (old) {
1172 struct iptables_target **i;
1173
1174 if (old->revision == me->revision) {
1175 fprintf(stderr,
1176 "%s: target `%s' already registered.\n",
1177 program_name, me->name);
1178 exit(1);
1179 }
1180
Rusty Russell3aef54d2005-01-03 03:48:40 +00001181 /* Now we have two (or more) options, check compatibility. */
1182 if (compatible_target_revision(old->name, old->revision)
1183 && old->revision > me->revision)
1184 return;
1185
1186 /* Replace if compatible. */
1187 if (!compatible_target_revision(me->name, me->revision))
1188 return;
1189
1190 /* Delete old one. */
1191 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1192 *i = old->next;
1193 }
1194
Rusty Russell73f72f52000-07-03 10:17:57 +00001195 if (me->size != IPT_ALIGN(me->size)) {
1196 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001197 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001198 exit(1);
1199 }
1200
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 /* Prepend to list. */
1202 me->next = iptables_targets;
1203 iptables_targets = me;
1204 me->t = NULL;
1205 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001206}
1207
1208static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001209print_num(u_int64_t number, unsigned int format)
1210{
1211 if (format & FMT_KILOMEGAGIGA) {
1212 if (number > 99999) {
1213 number = (number + 500) / 1000;
1214 if (number > 9999) {
1215 number = (number + 500) / 1000;
1216 if (number > 9999) {
1217 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001218 if (number > 9999) {
1219 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001220 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001221 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001222 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001223 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001224 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001225 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001226 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001227 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001228 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001229 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001231}
1232
1233
1234static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001235print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1236{
1237 struct ipt_counters counters;
1238 const char *pol = iptc_get_policy(chain, &counters, handle);
1239 printf("Chain %s", chain);
1240 if (pol) {
1241 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001242 if (!(format & FMT_NOCOUNTS)) {
1243 fputc(' ', stdout);
1244 print_num(counters.pcnt, (format|FMT_NOTABLE));
1245 fputs("packets, ", stdout);
1246 print_num(counters.bcnt, (format|FMT_NOTABLE));
1247 fputs("bytes", stdout);
1248 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001249 printf(")\n");
1250 } else {
1251 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001252 if (!iptc_get_references(&refs, chain, handle))
1253 printf(" (ERROR obtaining refs)\n");
1254 else
1255 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001256 }
1257
1258 if (format & FMT_LINENUMBERS)
1259 printf(FMT("%-4s ", "%s "), "num");
1260 if (!(format & FMT_NOCOUNTS)) {
1261 if (format & FMT_KILOMEGAGIGA) {
1262 printf(FMT("%5s ","%s "), "pkts");
1263 printf(FMT("%5s ","%s "), "bytes");
1264 } else {
1265 printf(FMT("%8s ","%s "), "pkts");
1266 printf(FMT("%10s ","%s "), "bytes");
1267 }
1268 }
1269 if (!(format & FMT_NOTARGET))
1270 printf(FMT("%-9s ","%s "), "target");
1271 fputs(" prot ", stdout);
1272 if (format & FMT_OPTIONS)
1273 fputs("opt", stdout);
1274 if (format & FMT_VIA) {
1275 printf(FMT(" %-6s ","%s "), "in");
1276 printf(FMT("%-6s ","%s "), "out");
1277 }
1278 printf(FMT(" %-19s ","%s "), "source");
1279 printf(FMT(" %-19s "," %s "), "destination");
1280 printf("\n");
1281}
1282
Marc Bouchere6869a82000-03-20 06:03:29 +00001283
1284static int
1285print_match(const struct ipt_entry_match *m,
1286 const struct ipt_ip *ip,
1287 int numeric)
1288{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001289 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001290
1291 if (match) {
1292 if (match->print)
1293 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001294 else
Rusty Russellb039b022000-09-01 06:04:05 +00001295 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001296 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001297 if (m->u.user.name[0])
1298 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001299 }
1300 /* Don't stop iterating. */
1301 return 0;
1302}
1303
1304/* e is called `fw' here for hysterical raisins */
1305static void
1306print_firewall(const struct ipt_entry *fw,
1307 const char *targname,
1308 unsigned int num,
1309 unsigned int format,
1310 const iptc_handle_t handle)
1311{
1312 struct iptables_target *target = NULL;
1313 const struct ipt_entry_target *t;
1314 u_int8_t flags;
1315 char buf[BUFSIZ];
1316
Marc Bouchere6869a82000-03-20 06:03:29 +00001317 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001318 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001319 else
Rusty Russell52a51492000-05-02 16:44:29 +00001320 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001321
1322 t = ipt_get_target((struct ipt_entry *)fw);
1323 flags = fw->ip.flags;
1324
1325 if (format & FMT_LINENUMBERS)
1326 printf(FMT("%-4u ", "%u "), num+1);
1327
1328 if (!(format & FMT_NOCOUNTS)) {
1329 print_num(fw->counters.pcnt, format);
1330 print_num(fw->counters.bcnt, format);
1331 }
1332
1333 if (!(format & FMT_NOTARGET))
1334 printf(FMT("%-9s ", "%s "), targname);
1335
1336 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1337 {
Rusty Russell28381a42000-05-10 00:19:50 +00001338 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 if (pname)
1340 printf(FMT("%-5s", "%s "), pname);
1341 else
1342 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1343 }
1344
1345 if (format & FMT_OPTIONS) {
1346 if (format & FMT_NOTABLE)
1347 fputs("opt ", stdout);
1348 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1349 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1350 fputc(' ', stdout);
1351 }
1352
1353 if (format & FMT_VIA) {
1354 char iface[IFNAMSIZ+2];
1355
1356 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1357 iface[0] = '!';
1358 iface[1] = '\0';
1359 }
1360 else iface[0] = '\0';
1361
1362 if (fw->ip.iniface[0] != '\0') {
1363 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001364 }
1365 else if (format & FMT_NUMERIC) strcat(iface, "*");
1366 else strcat(iface, "any");
1367 printf(FMT(" %-6s ","in %s "), iface);
1368
1369 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1370 iface[0] = '!';
1371 iface[1] = '\0';
1372 }
1373 else iface[0] = '\0';
1374
1375 if (fw->ip.outiface[0] != '\0') {
1376 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001377 }
1378 else if (format & FMT_NUMERIC) strcat(iface, "*");
1379 else strcat(iface, "any");
1380 printf(FMT("%-6s ","out %s "), iface);
1381 }
1382
1383 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1384 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1385 printf(FMT("%-19s ","%s "), "anywhere");
1386 else {
1387 if (format & FMT_NUMERIC)
1388 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1389 else
1390 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1391 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1392 printf(FMT("%-19s ","%s "), buf);
1393 }
1394
1395 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1396 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001397 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001398 else {
1399 if (format & FMT_NUMERIC)
1400 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1401 else
1402 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1403 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001404 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001405 }
1406
1407 if (format & FMT_NOTABLE)
1408 fputs(" ", stdout);
1409
1410 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1411
1412 if (target) {
1413 if (target->print)
1414 /* Print the target information. */
1415 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001416 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001417 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001418 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001419
1420 if (!(format & FMT_NONEWLINE))
1421 fputc('\n', stdout);
1422}
1423
1424static void
1425print_firewall_line(const struct ipt_entry *fw,
1426 const iptc_handle_t h)
1427{
1428 struct ipt_entry_target *t;
1429
1430 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001431 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001432}
1433
1434static int
1435append_entry(const ipt_chainlabel chain,
1436 struct ipt_entry *fw,
1437 unsigned int nsaddrs,
1438 const struct in_addr saddrs[],
1439 unsigned int ndaddrs,
1440 const struct in_addr daddrs[],
1441 int verbose,
1442 iptc_handle_t *handle)
1443{
1444 unsigned int i, j;
1445 int ret = 1;
1446
1447 for (i = 0; i < nsaddrs; i++) {
1448 fw->ip.src.s_addr = saddrs[i].s_addr;
1449 for (j = 0; j < ndaddrs; j++) {
1450 fw->ip.dst.s_addr = daddrs[j].s_addr;
1451 if (verbose)
1452 print_firewall_line(fw, *handle);
1453 ret &= iptc_append_entry(chain, fw, handle);
1454 }
1455 }
1456
1457 return ret;
1458}
1459
1460static int
1461replace_entry(const ipt_chainlabel chain,
1462 struct ipt_entry *fw,
1463 unsigned int rulenum,
1464 const struct in_addr *saddr,
1465 const struct in_addr *daddr,
1466 int verbose,
1467 iptc_handle_t *handle)
1468{
1469 fw->ip.src.s_addr = saddr->s_addr;
1470 fw->ip.dst.s_addr = daddr->s_addr;
1471
1472 if (verbose)
1473 print_firewall_line(fw, *handle);
1474 return iptc_replace_entry(chain, fw, rulenum, handle);
1475}
1476
1477static int
1478insert_entry(const ipt_chainlabel chain,
1479 struct ipt_entry *fw,
1480 unsigned int rulenum,
1481 unsigned int nsaddrs,
1482 const struct in_addr saddrs[],
1483 unsigned int ndaddrs,
1484 const struct in_addr daddrs[],
1485 int verbose,
1486 iptc_handle_t *handle)
1487{
1488 unsigned int i, j;
1489 int ret = 1;
1490
1491 for (i = 0; i < nsaddrs; i++) {
1492 fw->ip.src.s_addr = saddrs[i].s_addr;
1493 for (j = 0; j < ndaddrs; j++) {
1494 fw->ip.dst.s_addr = daddrs[j].s_addr;
1495 if (verbose)
1496 print_firewall_line(fw, *handle);
1497 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1498 }
1499 }
1500
1501 return ret;
1502}
1503
Rusty Russell2e0a3212000-04-19 11:23:18 +00001504static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001505make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001506{
1507 /* Establish mask for comparison */
1508 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001509 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001510 unsigned char *mask, *mptr;
1511
1512 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001513 for (matchp = matches; matchp; matchp = matchp->next)
1514 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001515
Rusty Russell9e1d2142000-04-23 09:11:12 +00001516 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001517 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001518 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001519
Rusty Russell9e1d2142000-04-23 09:11:12 +00001520 memset(mask, 0xFF, sizeof(struct ipt_entry));
1521 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001522
Martin Josefsson78cafda2004-02-02 20:01:18 +00001523 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001524 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001525 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001526 + matchp->match->userspacesize);
1527 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001528 }
1529
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001530 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001531 IPT_ALIGN(sizeof(struct ipt_entry_target))
1532 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001533
1534 return mask;
1535}
1536
Marc Bouchere6869a82000-03-20 06:03:29 +00001537static int
1538delete_entry(const ipt_chainlabel chain,
1539 struct ipt_entry *fw,
1540 unsigned int nsaddrs,
1541 const struct in_addr saddrs[],
1542 unsigned int ndaddrs,
1543 const struct in_addr daddrs[],
1544 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001545 iptc_handle_t *handle,
1546 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001547{
1548 unsigned int i, j;
1549 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001550 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001551
Martin Josefsson78cafda2004-02-02 20:01:18 +00001552 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001553 for (i = 0; i < nsaddrs; i++) {
1554 fw->ip.src.s_addr = saddrs[i].s_addr;
1555 for (j = 0; j < ndaddrs; j++) {
1556 fw->ip.dst.s_addr = daddrs[j].s_addr;
1557 if (verbose)
1558 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001559 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001560 }
1561 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001562 free(mask);
1563
Marc Bouchere6869a82000-03-20 06:03:29 +00001564 return ret;
1565}
1566
Harald Welteae1ff9f2000-12-01 14:26:20 +00001567int
Marc Bouchere6869a82000-03-20 06:03:29 +00001568for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001569 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001570{
1571 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001572 const char *chain;
1573 char *chains;
1574 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
Rusty Russell9e1d2142000-04-23 09:11:12 +00001576 chain = iptc_first_chain(handle);
1577 while (chain) {
1578 chaincount++;
1579 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001580 }
1581
Rusty Russell9e1d2142000-04-23 09:11:12 +00001582 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1583 i = 0;
1584 chain = iptc_first_chain(handle);
1585 while (chain) {
1586 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1587 i++;
1588 chain = iptc_next_chain(handle);
1589 }
1590
1591 for (i = 0; i < chaincount; i++) {
1592 if (!builtinstoo
1593 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001594 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001595 continue;
1596 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1597 }
1598
1599 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001600 return ret;
1601}
1602
Harald Welteae1ff9f2000-12-01 14:26:20 +00001603int
Marc Bouchere6869a82000-03-20 06:03:29 +00001604flush_entries(const ipt_chainlabel chain, int verbose,
1605 iptc_handle_t *handle)
1606{
1607 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001608 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001609
1610 if (verbose)
1611 fprintf(stdout, "Flushing chain `%s'\n", chain);
1612 return iptc_flush_entries(chain, handle);
1613}
Marc Bouchere6869a82000-03-20 06:03:29 +00001614
1615static int
1616zero_entries(const ipt_chainlabel chain, int verbose,
1617 iptc_handle_t *handle)
1618{
1619 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001620 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001621
Marc Bouchere6869a82000-03-20 06:03:29 +00001622 if (verbose)
1623 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1624 return iptc_zero_entries(chain, handle);
1625}
1626
Harald Welteae1ff9f2000-12-01 14:26:20 +00001627int
Marc Bouchere6869a82000-03-20 06:03:29 +00001628delete_chain(const ipt_chainlabel chain, int verbose,
1629 iptc_handle_t *handle)
1630{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001631 if (!chain)
1632 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001633
1634 if (verbose)
1635 fprintf(stdout, "Deleting chain `%s'\n", chain);
1636 return iptc_delete_chain(chain, handle);
1637}
1638
1639static int
1640list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1641 int expanded, int linenumbers, iptc_handle_t *handle)
1642{
1643 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001644 unsigned int format;
1645 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001646
1647 format = FMT_OPTIONS;
1648 if (!verbose)
1649 format |= FMT_NOCOUNTS;
1650 else
1651 format |= FMT_VIA;
1652
1653 if (numeric)
1654 format |= FMT_NUMERIC;
1655
1656 if (!expanded)
1657 format |= FMT_KILOMEGAGIGA;
1658
1659 if (linenumbers)
1660 format |= FMT_LINENUMBERS;
1661
Rusty Russell9e1d2142000-04-23 09:11:12 +00001662 for (this = iptc_first_chain(handle);
1663 this;
1664 this = iptc_next_chain(handle)) {
1665 const struct ipt_entry *i;
1666 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667
Marc Bouchere6869a82000-03-20 06:03:29 +00001668 if (chain && strcmp(chain, this) != 0)
1669 continue;
1670
1671 if (found) printf("\n");
1672
1673 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001674 i = iptc_first_rule(this, handle);
1675
1676 num = 0;
1677 while (i) {
1678 print_firewall(i,
1679 iptc_get_target(i, handle),
1680 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 format,
1682 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001683 i = iptc_next_rule(i, handle);
1684 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 found = 1;
1686 }
1687
1688 errno = ENOENT;
1689 return found;
1690}
1691
Harald Welte82dd2ec2000-12-19 05:18:15 +00001692static char *get_modprobe(void)
1693{
1694 int procfile;
1695 char *ret;
1696
Harald Welte10f7f142004-10-22 08:14:07 +00001697#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001698 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1699 if (procfile < 0)
1700 return NULL;
1701
Harald Welte10f7f142004-10-22 08:14:07 +00001702 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001703 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001704 memset(ret, 0, PROCFILE_BUFSIZ);
1705 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001706 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001707 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001708 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001709 if (ret[strlen(ret)-1]=='\n')
1710 ret[strlen(ret)-1]=0;
1711 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001712 return ret;
1713 }
1714 fail:
1715 free(ret);
1716 close(procfile);
1717 return NULL;
1718}
1719
Harald Welte58918652001-06-16 18:25:25 +00001720int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001721{
1722 char *buf = NULL;
1723 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001724 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001725
1726 /* If they don't explicitly set it, read out of kernel */
1727 if (!modprobe) {
1728 buf = get_modprobe();
1729 if (!buf)
1730 return -1;
1731 modprobe = buf;
1732 }
1733
1734 switch (fork()) {
1735 case 0:
1736 argv[0] = (char *)modprobe;
1737 argv[1] = (char *)modname;
1738 argv[2] = NULL;
1739 execv(argv[0], argv);
1740
1741 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001742 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001743 case -1:
1744 return -1;
1745
1746 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001747 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001748 }
1749
1750 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001751 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1752 return 0;
1753 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001754}
1755
Marc Bouchere6869a82000-03-20 06:03:29 +00001756static struct ipt_entry *
1757generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001758 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001759 struct ipt_entry_target *target)
1760{
1761 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001762 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001763 struct ipt_entry *e;
1764
1765 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001766 for (matchp = matches; matchp; matchp = matchp->next)
1767 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001768
Rusty Russell228e98d2000-04-27 10:28:06 +00001769 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001770 *e = *fw;
1771 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001772 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001773
1774 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001775 for (matchp = matches; matchp; matchp = matchp->next) {
1776 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1777 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001779 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001780
1781 return e;
1782}
1783
Martin Josefsson78cafda2004-02-02 20:01:18 +00001784void clear_rule_matches(struct iptables_rule_match **matches)
1785{
1786 struct iptables_rule_match *matchp, *tmp;
1787
1788 for (matchp = *matches; matchp;) {
1789 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001790 if (matchp->match->m)
1791 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001792 free(matchp);
1793 matchp = tmp;
1794 }
1795
1796 *matches = NULL;
1797}
1798
Rusty Russell3aef54d2005-01-03 03:48:40 +00001799static void set_revision(char *name, u_int8_t revision)
1800{
1801 /* Old kernel sources don't have ".revision" field,
1802 but we stole a byte from name. */
1803 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1804 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1805}
1806
Marc Bouchere6869a82000-03-20 06:03:29 +00001807int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1808{
1809 struct ipt_entry fw, *e = NULL;
1810 int invert = 0;
1811 unsigned int nsaddrs = 0, ndaddrs = 0;
1812 struct in_addr *saddrs = NULL, *daddrs = NULL;
1813
1814 int c, verbose = 0;
1815 const char *chain = NULL;
1816 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1817 const char *policy = NULL, *newname = NULL;
1818 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001819 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001820 int ret = 1;
1821 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001822 struct iptables_rule_match *matches = NULL;
1823 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001824 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001825 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 const char *jumpto = "";
1827 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001828 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001829 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001830
1831 memset(&fw, 0, sizeof(fw));
1832
Harald Welteae1ff9f2000-12-01 14:26:20 +00001833 /* re-set optind to 0 in case do_command gets called
1834 * a second time */
1835 optind = 0;
1836
1837 /* clear mflags in case do_command gets called a second time
1838 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001839 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001840 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001841
1842 for (t = iptables_targets; t; t = t->next) {
1843 t->tflags = 0;
1844 t->used = 0;
1845 }
1846
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 /* Suppress error messages: we may add new options if we
1848 demand-load a protocol. */
1849 opterr = 0;
1850
1851 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001852 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001853 opts, NULL)) != -1) {
1854 switch (c) {
1855 /*
1856 * Command selection
1857 */
1858 case 'A':
1859 add_command(&command, CMD_APPEND, CMD_NONE,
1860 invert);
1861 chain = optarg;
1862 break;
1863
1864 case 'D':
1865 add_command(&command, CMD_DELETE, CMD_NONE,
1866 invert);
1867 chain = optarg;
1868 if (optind < argc && argv[optind][0] != '-'
1869 && argv[optind][0] != '!') {
1870 rulenum = parse_rulenumber(argv[optind++]);
1871 command = CMD_DELETE_NUM;
1872 }
1873 break;
1874
Marc Bouchere6869a82000-03-20 06:03:29 +00001875 case 'R':
1876 add_command(&command, CMD_REPLACE, CMD_NONE,
1877 invert);
1878 chain = optarg;
1879 if (optind < argc && argv[optind][0] != '-'
1880 && argv[optind][0] != '!')
1881 rulenum = parse_rulenumber(argv[optind++]);
1882 else
1883 exit_error(PARAMETER_PROBLEM,
1884 "-%c requires a rule number",
1885 cmd2char(CMD_REPLACE));
1886 break;
1887
1888 case 'I':
1889 add_command(&command, CMD_INSERT, CMD_NONE,
1890 invert);
1891 chain = optarg;
1892 if (optind < argc && argv[optind][0] != '-'
1893 && argv[optind][0] != '!')
1894 rulenum = parse_rulenumber(argv[optind++]);
1895 else rulenum = 1;
1896 break;
1897
1898 case 'L':
1899 add_command(&command, CMD_LIST, CMD_ZERO,
1900 invert);
1901 if (optarg) chain = optarg;
1902 else if (optind < argc && argv[optind][0] != '-'
1903 && argv[optind][0] != '!')
1904 chain = argv[optind++];
1905 break;
1906
1907 case 'F':
1908 add_command(&command, CMD_FLUSH, CMD_NONE,
1909 invert);
1910 if (optarg) chain = optarg;
1911 else if (optind < argc && argv[optind][0] != '-'
1912 && argv[optind][0] != '!')
1913 chain = argv[optind++];
1914 break;
1915
1916 case 'Z':
1917 add_command(&command, CMD_ZERO, CMD_LIST,
1918 invert);
1919 if (optarg) chain = optarg;
1920 else if (optind < argc && argv[optind][0] != '-'
1921 && argv[optind][0] != '!')
1922 chain = argv[optind++];
1923 break;
1924
1925 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001926 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001927 exit_error(PARAMETER_PROBLEM,
1928 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001929 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001930 if (find_target(optarg, TRY_LOAD))
1931 exit_error(PARAMETER_PROBLEM,
1932 "chain name may not clash "
1933 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1935 invert);
1936 chain = optarg;
1937 break;
1938
1939 case 'X':
1940 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1941 invert);
1942 if (optarg) chain = optarg;
1943 else if (optind < argc && argv[optind][0] != '-'
1944 && argv[optind][0] != '!')
1945 chain = argv[optind++];
1946 break;
1947
1948 case 'E':
1949 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1950 invert);
1951 chain = optarg;
1952 if (optind < argc && argv[optind][0] != '-'
1953 && argv[optind][0] != '!')
1954 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001955 else
1956 exit_error(PARAMETER_PROBLEM,
1957 "-%c requires old-chain-name and "
1958 "new-chain-name",
1959 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001960 break;
1961
1962 case 'P':
1963 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1964 invert);
1965 chain = optarg;
1966 if (optind < argc && argv[optind][0] != '-'
1967 && argv[optind][0] != '!')
1968 policy = argv[optind++];
1969 else
1970 exit_error(PARAMETER_PROBLEM,
1971 "-%c requires a chain and a policy",
1972 cmd2char(CMD_SET_POLICY));
1973 break;
1974
1975 case 'h':
1976 if (!optarg)
1977 optarg = argv[optind];
1978
Rusty Russell2e0a3212000-04-19 11:23:18 +00001979 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001980 if (!matches && protocol)
1981 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001982
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001983 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001984
1985 /*
1986 * Option selection
1987 */
1988 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001989 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001990 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1991 invert);
1992
1993 /* Canonicalize into lower case */
1994 for (protocol = argv[optind-1]; *protocol; protocol++)
1995 *protocol = tolower(*protocol);
1996
1997 protocol = argv[optind-1];
1998 fw.ip.proto = parse_protocol(protocol);
1999
2000 if (fw.ip.proto == 0
2001 && (fw.ip.invflags & IPT_INV_PROTO))
2002 exit_error(PARAMETER_PROBLEM,
2003 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 break;
2005
2006 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002007 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2009 invert);
2010 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002011 break;
2012
2013 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002014 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002015 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2016 invert);
2017 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002018 break;
2019
2020 case 'j':
2021 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2022 invert);
2023 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002024 /* TRY_LOAD (may be chain name) */
2025 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026
2027 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002028 size_t size;
2029
Rusty Russell73f72f52000-07-03 10:17:57 +00002030 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2031 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002032
Rusty Russell2e0a3212000-04-19 11:23:18 +00002033 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002034 target->t->u.target_size = size;
2035 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002036 set_revision(target->t->u.user.name,
2037 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002038 if (target->init != NULL)
2039 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002040 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 }
2042 break;
2043
2044
2045 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002046 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2048 invert);
2049 parse_interface(argv[optind-1],
2050 fw.ip.iniface,
2051 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002052 break;
2053
2054 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002055 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002056 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2057 invert);
2058 parse_interface(argv[optind-1],
2059 fw.ip.outiface,
2060 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002061 break;
2062
2063 case 'f':
2064 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2065 invert);
2066 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 break;
2068
2069 case 'v':
2070 if (!verbose)
2071 set_option(&options, OPT_VERBOSE,
2072 &fw.ip.invflags, invert);
2073 verbose++;
2074 break;
2075
Rusty Russell52a51492000-05-02 16:44:29 +00002076 case 'm': {
2077 size_t size;
2078
Marc Bouchere6869a82000-03-20 06:03:29 +00002079 if (invert)
2080 exit_error(PARAMETER_PROBLEM,
2081 "unexpected ! flag before --match");
2082
Martin Josefsson78cafda2004-02-02 20:01:18 +00002083 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002084 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2085 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002086 m->m = fw_calloc(1, size);
2087 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002088 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002089 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002090 if (m->init != NULL)
2091 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002092 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002093 }
2094 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002095
2096 case 'n':
2097 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2098 invert);
2099 break;
2100
2101 case 't':
2102 if (invert)
2103 exit_error(PARAMETER_PROBLEM,
2104 "unexpected ! flag before --table");
2105 *table = argv[optind-1];
2106 break;
2107
2108 case 'x':
2109 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2110 invert);
2111 break;
2112
2113 case 'V':
2114 if (invert)
2115 printf("Not %s ;-)\n", program_version);
2116 else
2117 printf("%s v%s\n",
2118 program_name, program_version);
2119 exit(0);
2120
2121 case '0':
2122 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2123 invert);
2124 break;
2125
Harald Welte82dd2ec2000-12-19 05:18:15 +00002126 case 'M':
2127 modprobe = optarg;
2128 break;
2129
Harald Welteccd49e52001-01-23 22:54:34 +00002130 case 'c':
2131
2132 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2133 invert);
2134 pcnt = optarg;
2135 if (optind < argc && argv[optind][0] != '-'
2136 && argv[optind][0] != '!')
2137 bcnt = argv[optind++];
2138 else
2139 exit_error(PARAMETER_PROBLEM,
2140 "-%c requires packet and byte counter",
2141 opt2char(OPT_COUNTERS));
2142
Martin Josefssona28d4952004-05-26 16:04:48 +00002143 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002144 exit_error(PARAMETER_PROBLEM,
2145 "-%c packet counter not numeric",
2146 opt2char(OPT_COUNTERS));
2147
Martin Josefssona28d4952004-05-26 16:04:48 +00002148 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002149 exit_error(PARAMETER_PROBLEM,
2150 "-%c byte counter not numeric",
2151 opt2char(OPT_COUNTERS));
2152
2153 break;
2154
2155
Marc Bouchere6869a82000-03-20 06:03:29 +00002156 case 1: /* non option */
2157 if (optarg[0] == '!' && optarg[1] == '\0') {
2158 if (invert)
2159 exit_error(PARAMETER_PROBLEM,
2160 "multiple consecutive ! not"
2161 " allowed");
2162 invert = TRUE;
2163 optarg[0] = '\0';
2164 continue;
2165 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002166 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002167 exit_tryhelp(2);
2168
2169 default:
2170 /* FIXME: This scheme doesn't allow two of the same
2171 matches --RR */
2172 if (!target
2173 || !(target->parse(c - target->option_offset,
2174 argv, invert,
2175 &target->tflags,
2176 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002177 for (matchp = matches; matchp; matchp = matchp->next) {
2178 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002179 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002180 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002181 &fw,
2182 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002183 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002184 break;
2185 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002186 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002187
2188 /* If you listen carefully, you can
2189 actually hear this code suck. */
2190
2191 /* some explanations (after four different bugs
2192 * in 3 different releases): If we encountere a
2193 * parameter, that has not been parsed yet,
2194 * it's not an option of an explicitly loaded
2195 * match or a target. However, we support
2196 * implicit loading of the protocol match
2197 * extension. '-p tcp' means 'l4 proto 6' and
2198 * at the same time 'load tcp protocol match on
2199 * demand if we specify --dport'.
2200 *
2201 * To make this work, we need to make sure:
2202 * - the parameter has not been parsed by
2203 * a match (m above)
2204 * - a protocol has been specified
2205 * - the protocol extension has not been
2206 * loaded yet, or is loaded and unused
2207 * [think of iptables-restore!]
2208 * - the protocol extension can be successively
2209 * loaded
2210 */
2211 if (m == NULL
2212 && protocol
2213 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002214 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002215 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002216 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002217 && (proto_used == 0))
2218 )
2219 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002220 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002221 /* Try loading protocol */
2222 size_t size;
2223
2224 proto_used = 1;
2225
2226 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2227 + m->size;
2228
2229 m->m = fw_calloc(1, size);
2230 m->m->u.match_size = size;
2231 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002232 set_revision(m->m->u.user.name,
2233 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002234 if (m->init != NULL)
2235 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002236
2237 opts = merge_options(opts,
2238 m->extra_opts, &m->option_offset);
2239
2240 optind--;
2241 continue;
2242 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002243 if (!m)
2244 exit_error(PARAMETER_PROBLEM,
2245 "Unknown arg `%s'",
2246 argv[optind-1]);
2247 }
2248 }
2249 invert = FALSE;
2250 }
2251
Martin Josefsson78cafda2004-02-02 20:01:18 +00002252 for (matchp = matches; matchp; matchp = matchp->next)
2253 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002254
Marc Bouchere6869a82000-03-20 06:03:29 +00002255 if (target)
2256 target->final_check(target->tflags);
2257
2258 /* Fix me: must put inverse options checking here --MN */
2259
2260 if (optind < argc)
2261 exit_error(PARAMETER_PROBLEM,
2262 "unknown arguments found on commandline");
2263 if (!command)
2264 exit_error(PARAMETER_PROBLEM, "no command specified");
2265 if (invert)
2266 exit_error(PARAMETER_PROBLEM,
2267 "nothing appropriate following !");
2268
Harald Welte6336bfd2002-05-07 14:41:43 +00002269 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002270 if (!(options & OPT_DESTINATION))
2271 dhostnetworkmask = "0.0.0.0/0";
2272 if (!(options & OPT_SOURCE))
2273 shostnetworkmask = "0.0.0.0/0";
2274 }
2275
2276 if (shostnetworkmask)
2277 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2278 &(fw.ip.smsk), &nsaddrs);
2279
2280 if (dhostnetworkmask)
2281 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2282 &(fw.ip.dmsk), &ndaddrs);
2283
2284 if ((nsaddrs > 1 || ndaddrs > 1) &&
2285 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2286 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2287 " source or destination IP addresses");
2288
Marc Bouchere6869a82000-03-20 06:03:29 +00002289 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2290 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2291 "specify a unique address");
2292
2293 generic_opt_check(command, options);
2294
2295 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2296 exit_error(PARAMETER_PROBLEM,
2297 "chain name `%s' too long (must be under %i chars)",
2298 chain, IPT_FUNCTION_MAXNAMELEN);
2299
Harald Welteae1ff9f2000-12-01 14:26:20 +00002300 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002301 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002302 *handle = iptc_init(*table);
2303
Rusty Russell8beb0492004-12-22 00:37:10 +00002304 /* try to insmod the module if iptc_init failed */
2305 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002306 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002307
Marc Bouchere6869a82000-03-20 06:03:29 +00002308 if (!*handle)
2309 exit_error(VERSION_PROBLEM,
2310 "can't initialize iptables table `%s': %s",
2311 *table, iptc_strerror(errno));
2312
Harald Welte6336bfd2002-05-07 14:41:43 +00002313 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002314 || command == CMD_DELETE
2315 || command == CMD_INSERT
2316 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002317 if (strcmp(chain, "PREROUTING") == 0
2318 || strcmp(chain, "INPUT") == 0) {
2319 /* -o not valid with incoming packets. */
2320 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002321 exit_error(PARAMETER_PROBLEM,
2322 "Can't use -%c with %s\n",
2323 opt2char(OPT_VIANAMEOUT),
2324 chain);
2325 }
2326
Rusty Russella4860fd2000-06-17 16:13:02 +00002327 if (strcmp(chain, "POSTROUTING") == 0
2328 || strcmp(chain, "OUTPUT") == 0) {
2329 /* -i not valid with outgoing packets */
2330 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002331 exit_error(PARAMETER_PROBLEM,
2332 "Can't use -%c with %s\n",
2333 opt2char(OPT_VIANAMEIN),
2334 chain);
2335 }
2336
2337 if (target && iptc_is_chain(jumpto, *handle)) {
2338 printf("Warning: using chain %s, not extension\n",
2339 jumpto);
2340
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002341 if (target->t)
2342 free(target->t);
2343
Marc Bouchere6869a82000-03-20 06:03:29 +00002344 target = NULL;
2345 }
2346
2347 /* If they didn't specify a target, or it's a chain
2348 name, use standard. */
2349 if (!target
2350 && (strlen(jumpto) == 0
2351 || iptc_is_chain(jumpto, *handle))) {
2352 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002353
Rusty Russell52a51492000-05-02 16:44:29 +00002354 target = find_target(IPT_STANDARD_TARGET,
2355 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002356
2357 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002358 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002359 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002360 target->t->u.target_size = size;
2361 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002362 set_revision(target->t->u.user.name, target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002363 if (target->init != NULL)
2364 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002365 }
2366
Rusty Russell7e53bf92000-03-20 07:03:28 +00002367 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002368 /* it is no chain, and we can't load a plugin.
2369 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002370 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002371 * chain. */
2372 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002373 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002374 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002375 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002376 }
2377 }
2378
2379 switch (command) {
2380 case CMD_APPEND:
2381 ret = append_entry(chain, e,
2382 nsaddrs, saddrs, ndaddrs, daddrs,
2383 options&OPT_VERBOSE,
2384 handle);
2385 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002386 case CMD_DELETE:
2387 ret = delete_entry(chain, e,
2388 nsaddrs, saddrs, ndaddrs, daddrs,
2389 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002390 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002391 break;
2392 case CMD_DELETE_NUM:
2393 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2394 break;
2395 case CMD_REPLACE:
2396 ret = replace_entry(chain, e, rulenum - 1,
2397 saddrs, daddrs, options&OPT_VERBOSE,
2398 handle);
2399 break;
2400 case CMD_INSERT:
2401 ret = insert_entry(chain, e, rulenum - 1,
2402 nsaddrs, saddrs, ndaddrs, daddrs,
2403 options&OPT_VERBOSE,
2404 handle);
2405 break;
2406 case CMD_LIST:
2407 ret = list_entries(chain,
2408 options&OPT_VERBOSE,
2409 options&OPT_NUMERIC,
2410 options&OPT_EXPANDED,
2411 options&OPT_LINENUMBERS,
2412 handle);
2413 break;
2414 case CMD_FLUSH:
2415 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2416 break;
2417 case CMD_ZERO:
2418 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2419 break;
2420 case CMD_LIST|CMD_ZERO:
2421 ret = list_entries(chain,
2422 options&OPT_VERBOSE,
2423 options&OPT_NUMERIC,
2424 options&OPT_EXPANDED,
2425 options&OPT_LINENUMBERS,
2426 handle);
2427 if (ret)
2428 ret = zero_entries(chain,
2429 options&OPT_VERBOSE, handle);
2430 break;
2431 case CMD_NEW_CHAIN:
2432 ret = iptc_create_chain(chain, handle);
2433 break;
2434 case CMD_DELETE_CHAIN:
2435 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2436 break;
2437 case CMD_RENAME_CHAIN:
2438 ret = iptc_rename_chain(chain, newname, handle);
2439 break;
2440 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002441 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002442 break;
2443 default:
2444 /* We should never reach this... */
2445 exit_tryhelp(2);
2446 }
2447
2448 if (verbose > 1)
2449 dump_entries(*handle);
2450
Martin Josefsson78cafda2004-02-02 20:01:18 +00002451 clear_rule_matches(&matches);
2452
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002453 if (e != NULL) {
2454 free(e);
2455 e = NULL;
2456 }
2457
keso6997cdf2004-07-04 15:20:53 +00002458 free(saddrs);
2459 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002460 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002461
Marc Bouchere6869a82000-03-20 06:03:29 +00002462 return ret;
2463}