blob: 06d04810076c3e0275f79b66560234db0f231497 [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]
Harald Welteccd49e52001-01-23 22:54:34 +0000104= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', '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
309void
310exit_error(enum exittype status, char *msg, ...)
311{
312 va_list args;
313
314 va_start(args, msg);
315 fprintf(stderr, "%s v%s: ", program_name, program_version);
316 vfprintf(stderr, msg, args);
317 va_end(args);
318 fprintf(stderr, "\n");
319 if (status == PARAMETER_PROBLEM)
320 exit_tryhelp(status);
321 if (status == VERSION_PROBLEM)
322 fprintf(stderr,
323 "Perhaps iptables or your kernel needs to be upgraded.\n");
324 exit(status);
325}
326
327void
328exit_tryhelp(int status)
329{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000330 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000331 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000332 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
333 program_name, program_name );
334 exit(status);
335}
336
337void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000338exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000339{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000340 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000341 struct iptables_target *t = NULL;
342
Marc Bouchere6869a82000-03-20 06:03:29 +0000343 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000344"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000345" %s -[RI] chain rulenum rule-specification [options]\n"
346" %s -D chain rulenum [options]\n"
347" %s -[LFZ] [chain] [options]\n"
348" %s -[NX] chain\n"
349" %s -E old-chain-name new-chain-name\n"
350" %s -P chain target [options]\n"
351" %s -h (print this help information)\n\n",
352 program_name, program_version, program_name, program_name,
353 program_name, program_name, program_name, program_name,
354 program_name, program_name);
355
356 printf(
357"Commands:\n"
358"Either long or short options are allowed.\n"
359" --append -A chain Append to chain\n"
360" --delete -D chain Delete matching rule from chain\n"
361" --delete -D chain rulenum\n"
362" Delete rule rulenum (1 = first) from chain\n"
363" --insert -I chain [rulenum]\n"
364" Insert in chain as rulenum (default 1=first)\n"
365" --replace -R chain rulenum\n"
366" Replace rule rulenum (1 = first) in chain\n"
367" --list -L [chain] List the rules in a chain or all chains\n"
368" --flush -F [chain] Delete all rules in chain or all chains\n"
369" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000370" --new -N chain Create a new user-defined chain\n"
371" --delete-chain\n"
372" -X [chain] Delete a user-defined chain\n"
373" --policy -P chain target\n"
374" Change policy on chain to target\n"
375" --rename-chain\n"
376" -E old-chain new-chain\n"
377" Change chain name, (moving any references)\n"
378
379"Options:\n"
380" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
381" --source -s [!] address[/mask]\n"
382" source specification\n"
383" --destination -d [!] address[/mask]\n"
384" destination specification\n"
385" --in-interface -i [!] input name[+]\n"
386" network interface name ([+] for wildcard)\n"
387" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000388" target for rule (may load target extension)\n"
389" --match -m match\n"
390" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000391" --numeric -n numeric output of addresses and ports\n"
392" --out-interface -o [!] output name[+]\n"
393" network interface name ([+] for wildcard)\n"
394" --table -t table table to manipulate (default: `filter')\n"
395" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000396" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000397" --exact -x expand numbers (display exact values)\n"
398"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000399" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000400" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000401"[!] --version -V print package version.\n");
402
Rusty Russell363112d2000-08-11 13:49:26 +0000403 /* Print out any special helps. A user might like to be able
404 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000405 results. So we call help for all specified matches & targets */
406 for (t = iptables_targets; t ;t = t->next) {
407 if (t->used) {
408 printf("\n");
409 t->help();
410 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000411 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000412 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000413 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000414 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000415 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000416 exit(0);
417}
418
419static void
420generic_opt_check(int command, int options)
421{
422 int i, j, legal = 0;
423
424 /* Check that commands are valid with options. Complicated by the
425 * fact that if an option is legal with *any* command given, it is
426 * legal overall (ie. -z and -l).
427 */
428 for (i = 0; i < NUMBER_OF_OPT; i++) {
429 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
430
431 for (j = 0; j < NUMBER_OF_CMD; j++) {
432 if (!(command & (1<<j)))
433 continue;
434
435 if (!(options & (1<<i))) {
436 if (commands_v_options[j][i] == '+')
437 exit_error(PARAMETER_PROBLEM,
438 "You need to supply the `-%c' "
439 "option for this command\n",
440 optflags[i]);
441 } else {
442 if (commands_v_options[j][i] != 'x')
443 legal = 1;
444 else if (legal == 0)
445 legal = -1;
446 }
447 }
448 if (legal == -1)
449 exit_error(PARAMETER_PROBLEM,
450 "Illegal option `-%c' with this command\n",
451 optflags[i]);
452 }
453}
454
455static char
456opt2char(int option)
457{
458 const char *ptr;
459 for (ptr = optflags; option > 1; option >>= 1, ptr++);
460
461 return *ptr;
462}
463
464static char
465cmd2char(int option)
466{
467 const char *ptr;
468 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
469
470 return *ptr;
471}
472
473static void
474add_command(int *cmd, const int newcmd, const int othercmds, int invert)
475{
476 if (invert)
477 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
478 if (*cmd & (~othercmds))
479 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
480 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
481 *cmd |= newcmd;
482}
483
484int
Harald Welteb77f1da2002-03-14 11:35:58 +0000485check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000486{
487 if (option && strcmp(option, "!") == 0) {
488 if (*invert)
489 exit_error(PARAMETER_PROBLEM,
490 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000491 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000492 if (optind) {
493 *optind = *optind+1;
494 if (argc && *optind > argc)
495 exit_error(PARAMETER_PROBLEM,
496 "no argument following `!'");
497 }
498
Marc Bouchere6869a82000-03-20 06:03:29 +0000499 return TRUE;
500 }
501 return FALSE;
502}
503
504static void *
505fw_calloc(size_t count, size_t size)
506{
507 void *p;
508
509 if ((p = calloc(count, size)) == NULL) {
510 perror("iptables: calloc failed");
511 exit(1);
512 }
513 return p;
514}
515
516static void *
517fw_malloc(size_t size)
518{
519 void *p;
520
521 if ((p = malloc(size)) == NULL) {
522 perror("iptables: malloc failed");
523 exit(1);
524 }
525 return p;
526}
527
528static struct in_addr *
529host_to_addr(const char *name, unsigned int *naddr)
530{
531 struct hostent *host;
532 struct in_addr *addr;
533 unsigned int i;
534
535 *naddr = 0;
536 if ((host = gethostbyname(name)) != NULL) {
537 if (host->h_addrtype != AF_INET ||
538 host->h_length != sizeof(struct in_addr))
539 return (struct in_addr *) NULL;
540
541 while (host->h_addr_list[*naddr] != (char *) NULL)
542 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000543 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000544 for (i = 0; i < *naddr; i++)
545 inaddrcpy(&(addr[i]),
546 (struct in_addr *) host->h_addr_list[i]);
547 return addr;
548 }
549
550 return (struct in_addr *) NULL;
551}
552
553static char *
554addr_to_host(const struct in_addr *addr)
555{
556 struct hostent *host;
557
558 if ((host = gethostbyaddr((char *) addr,
559 sizeof(struct in_addr), AF_INET)) != NULL)
560 return (char *) host->h_name;
561
562 return (char *) NULL;
563}
564
565/*
566 * All functions starting with "parse" should succeed, otherwise
567 * the program fails.
568 * Most routines return pointers to static data that may change
569 * between calls to the same or other routines with a few exceptions:
570 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
571 * return global static data.
572*/
573
574static struct in_addr *
575parse_hostnetwork(const char *name, unsigned int *naddrs)
576{
577 struct in_addr *addrp, *addrptmp;
578
579 if ((addrptmp = dotted_to_addr(name)) != NULL ||
580 (addrptmp = network_to_addr(name)) != NULL) {
581 addrp = fw_malloc(sizeof(struct in_addr));
582 inaddrcpy(addrp, addrptmp);
583 *naddrs = 1;
584 return addrp;
585 }
586 if ((addrp = host_to_addr(name, naddrs)) != NULL)
587 return addrp;
588
589 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
590}
591
592static struct in_addr *
593parse_mask(char *mask)
594{
595 static struct in_addr maskaddr;
596 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000597 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000598
599 if (mask == NULL) {
600 /* no mask at all defaults to 32 bits */
601 maskaddr.s_addr = 0xFFFFFFFF;
602 return &maskaddr;
603 }
604 if ((addrp = dotted_to_addr(mask)) != NULL)
605 /* dotted_to_addr already returns a network byte order addr */
606 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000607 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000608 exit_error(PARAMETER_PROBLEM,
609 "invalid mask `%s' specified", mask);
610 if (bits != 0) {
611 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
612 return &maskaddr;
613 }
614
615 maskaddr.s_addr = 0L;
616 return &maskaddr;
617}
618
Marc Boucherb93c7982001-12-06 14:50:19 +0000619void
Marc Bouchere6869a82000-03-20 06:03:29 +0000620parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
621 struct in_addr *maskp, unsigned int *naddrs)
622{
623 struct in_addr *addrp;
624 char buf[256];
625 char *p;
626 int i, j, k, n;
627
628 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000629 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000630 if ((p = strrchr(buf, '/')) != NULL) {
631 *p = '\0';
632 addrp = parse_mask(p + 1);
633 } else
634 addrp = parse_mask(NULL);
635 inaddrcpy(maskp, addrp);
636
637 /* if a null mask is given, the name is ignored, like in "any/0" */
638 if (maskp->s_addr == 0L)
639 strcpy(buf, "0.0.0.0");
640
641 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
642 n = *naddrs;
643 for (i = 0, j = 0; i < n; i++) {
644 addrp[j++].s_addr &= maskp->s_addr;
645 for (k = 0; k < j - 1; k++) {
646 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
647 (*naddrs)--;
648 j--;
649 break;
650 }
651 }
652 }
653}
654
655struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000656find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000657{
658 struct iptables_match *ptr;
659
660 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
661 if (strcmp(name, ptr->name) == 0)
662 break;
663 }
664
Harald Welte3efb6ea2001-08-06 18:50:21 +0000665#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000666 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000667 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000668 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000669 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000670 if (dlopen(path, RTLD_NOW)) {
671 /* Found library. If it didn't register itself,
672 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000673 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000674
Rusty Russell9e1d2142000-04-23 09:11:12 +0000675 if (!ptr)
676 exit_error(PARAMETER_PROBLEM,
677 "Couldn't load match `%s'\n",
678 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000679 } else if (tryload == LOAD_MUST_SUCCEED)
680 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000681 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000682 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000683 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000684#else
685 if (ptr && !ptr->loaded) {
686 if (tryload != DONT_LOAD)
687 ptr->loaded = 1;
688 else
689 ptr = NULL;
690 }
Marc Boucher067477b2002-03-24 15:09:31 +0000691 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
692 exit_error(PARAMETER_PROBLEM,
693 "Couldn't find match `%s'\n", name);
694 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000695#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000696
Martin Josefsson78cafda2004-02-02 20:01:18 +0000697 if (ptr && matches) {
698 struct iptables_rule_match **i;
699 struct iptables_rule_match *newentry;
700
701 newentry = fw_malloc(sizeof(struct iptables_rule_match));
702
703 for (i = matches; *i; i = &(*i)->next);
704 newentry->match = ptr;
705 newentry->next = NULL;
706 *i = newentry;
707 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000708
Marc Bouchere6869a82000-03-20 06:03:29 +0000709 return ptr;
710}
711
Rusty Russell28381a42000-05-10 00:19:50 +0000712/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
713static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000714find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000715{
Harald Welteed498492001-07-23 01:24:22 +0000716 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000717
Harald Welte0b0013a2002-02-18 16:15:31 +0000718 if (string_to_number(pname, 0, 255, &proto) != -1) {
719 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000720
Harald Welte0b0013a2002-02-18 16:15:31 +0000721 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000722 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000723 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000724 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000725
726 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000727}
728
Marc Boucherb93c7982001-12-06 14:50:19 +0000729u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000730parse_protocol(const char *s)
731{
Harald Welteed498492001-07-23 01:24:22 +0000732 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000733
Harald Welteed498492001-07-23 01:24:22 +0000734 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000735 struct protoent *pent;
736
737 if ((pent = getprotobyname(s)))
738 proto = pent->p_proto;
739 else {
740 unsigned int i;
741 for (i = 0;
742 i < sizeof(chain_protos)/sizeof(struct pprot);
743 i++) {
744 if (strcmp(s, chain_protos[i].name) == 0) {
745 proto = chain_protos[i].num;
746 break;
747 }
748 }
749 if (i == sizeof(chain_protos)/sizeof(struct pprot))
750 exit_error(PARAMETER_PROBLEM,
751 "unknown protocol `%s' specified",
752 s);
753 }
754 }
755
756 return (u_int16_t)proto;
757}
758
759static void
760parse_interface(const char *arg, char *vianame, unsigned char *mask)
761{
762 int vialen = strlen(arg);
763 unsigned int i;
764
765 memset(mask, 0, IFNAMSIZ);
766 memset(vianame, 0, IFNAMSIZ);
767
768 if (vialen + 1 > IFNAMSIZ)
769 exit_error(PARAMETER_PROBLEM,
770 "interface name `%s' must be shorter than IFNAMSIZ"
771 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000772
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000774 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000775 memset(mask, 0, IFNAMSIZ);
776 else if (vianame[vialen - 1] == '+') {
777 memset(mask, 0xFF, vialen - 1);
778 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000779 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000780 } else {
781 /* Include nul-terminator in match */
782 memset(mask, 0xFF, vialen + 1);
783 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000784 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000785 if (!isalnum(vianame[i])
786 && vianame[i] != '_'
787 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000788 printf("Warning: wierd character in interface"
789 " `%s' (No aliases, :, ! or *).\n",
790 vianame);
791 break;
792 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000793 }
794 }
795}
796
797/* Can't be zero. */
798static int
799parse_rulenumber(const char *rule)
800{
Harald Welteed498492001-07-23 01:24:22 +0000801 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000802
Harald Welteed498492001-07-23 01:24:22 +0000803 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000804 exit_error(PARAMETER_PROBLEM,
805 "Invalid rule number `%s'", rule);
806
807 return rulenum;
808}
809
810static const char *
811parse_target(const char *targetname)
812{
813 const char *ptr;
814
815 if (strlen(targetname) < 1)
816 exit_error(PARAMETER_PROBLEM,
817 "Invalid target name (too short)");
818
819 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
820 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000821 "Invalid target name `%s' (%u chars max)",
822 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000823
824 for (ptr = targetname; *ptr; ptr++)
825 if (isspace(*ptr))
826 exit_error(PARAMETER_PROBLEM,
827 "Invalid target name `%s'", targetname);
828 return targetname;
829}
830
831static char *
832addr_to_network(const struct in_addr *addr)
833{
834 struct netent *net;
835
836 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
837 return (char *) net->n_name;
838
839 return (char *) NULL;
840}
841
842char *
843addr_to_dotted(const struct in_addr *addrp)
844{
845 static char buf[20];
846 const unsigned char *bytep;
847
848 bytep = (const unsigned char *) &(addrp->s_addr);
849 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
850 return buf;
851}
Marc Boucherb93c7982001-12-06 14:50:19 +0000852
853char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000854addr_to_anyname(const struct in_addr *addr)
855{
856 char *name;
857
858 if ((name = addr_to_host(addr)) != NULL ||
859 (name = addr_to_network(addr)) != NULL)
860 return name;
861
862 return addr_to_dotted(addr);
863}
864
Marc Boucherb93c7982001-12-06 14:50:19 +0000865char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000866mask_to_dotted(const struct in_addr *mask)
867{
868 int i;
869 static char buf[20];
870 u_int32_t maskaddr, bits;
871
872 maskaddr = ntohl(mask->s_addr);
873
874 if (maskaddr == 0xFFFFFFFFL)
875 /* we don't want to see "/32" */
876 return "";
877
878 i = 32;
879 bits = 0xFFFFFFFEL;
880 while (--i >= 0 && maskaddr != bits)
881 bits <<= 1;
882 if (i >= 0)
883 sprintf(buf, "/%d", i);
884 else
885 /* mask was not a decent combination of 1's and 0's */
886 sprintf(buf, "/%s", addr_to_dotted(mask));
887
888 return buf;
889}
890
891int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000892string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
893 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000894{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000895 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000896 char *end;
897
898 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000899 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000900 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000901 if (*end == '\0' && end != s) {
902 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000903 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000904 *ret = number;
905 return 0;
906 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000907 }
908 return -1;
909}
910
Martin Josefssonb105bc92004-05-26 15:54:49 +0000911int
912string_to_number_l(const char *s, unsigned long min, unsigned long max,
913 unsigned long *ret)
914{
915 int result;
916 unsigned long long number;
917
918 result = string_to_number_ll(s, min, max, &number);
919 *ret = (unsigned long)number;
920
921 return result;
922}
923
924int string_to_number(const char *s, unsigned int min, unsigned int max,
925 unsigned int *ret)
926{
927 int result;
928 unsigned long number;
929
930 result = string_to_number_l(s, min, max, &number);
931 *ret = (unsigned int)number;
932
933 return result;
934}
935
Marc Bouchere6869a82000-03-20 06:03:29 +0000936static void
937set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
938 int invert)
939{
940 if (*options & option)
941 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
942 opt2char(option));
943 *options |= option;
944
945 if (invert) {
946 unsigned int i;
947 for (i = 0; 1 << i != option; i++);
948
949 if (!inverse_for_options[i])
950 exit_error(PARAMETER_PROBLEM,
951 "cannot have ! before -%c",
952 opt2char(option));
953 *invflg |= inverse_for_options[i];
954 }
955}
956
957struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000958find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000959{
960 struct iptables_target *ptr;
961
962 /* Standard target? */
963 if (strcmp(name, "") == 0
964 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
965 || strcmp(name, IPTC_LABEL_DROP) == 0
966 || strcmp(name, IPTC_LABEL_QUEUE) == 0
967 || strcmp(name, IPTC_LABEL_RETURN) == 0)
968 name = "standard";
969
970 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
971 if (strcmp(name, ptr->name) == 0)
972 break;
973 }
974
Harald Welte3efb6ea2001-08-06 18:50:21 +0000975#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000976 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000977 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000979 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000980 if (dlopen(path, RTLD_NOW)) {
981 /* Found library. If it didn't register itself,
982 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000983 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000984 if (!ptr)
985 exit_error(PARAMETER_PROBLEM,
986 "Couldn't load target `%s'\n",
987 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000988 } else if (tryload == LOAD_MUST_SUCCEED)
989 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000990 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000991 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000992 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000993#else
994 if (ptr && !ptr->loaded) {
995 if (tryload != DONT_LOAD)
996 ptr->loaded = 1;
997 else
998 ptr = NULL;
999 }
Marc Boucher067477b2002-03-24 15:09:31 +00001000 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1001 exit_error(PARAMETER_PROBLEM,
1002 "Couldn't find target `%s'\n", name);
1003 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001004#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001005
Harald Welteae1ff9f2000-12-01 14:26:20 +00001006 if (ptr)
1007 ptr->used = 1;
1008
Marc Bouchere6869a82000-03-20 06:03:29 +00001009 return ptr;
1010}
1011
1012static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001013merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001014 unsigned int *option_offset)
1015{
1016 unsigned int num_old, num_new, i;
1017 struct option *merge;
1018
1019 for (num_old = 0; oldopts[num_old].name; num_old++);
1020 for (num_new = 0; newopts[num_new].name; num_new++);
1021
1022 global_option_offset += OPTION_OFFSET;
1023 *option_offset = global_option_offset;
1024
1025 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1026 memcpy(merge, oldopts, num_old * sizeof(struct option));
1027 for (i = 0; i < num_new; i++) {
1028 merge[num_old + i] = newopts[i];
1029 merge[num_old + i].val += *option_offset;
1030 }
1031 memset(merge + num_old + num_new, 0, sizeof(struct option));
1032
1033 return merge;
1034}
1035
Rusty Russell3aef54d2005-01-03 03:48:40 +00001036static int compatible_revision(const char *name, u_int8_t revision, int opt)
1037{
1038 struct ipt_get_revision rev;
1039 socklen_t s = sizeof(rev);
1040 int max_rev, sockfd;
1041
1042 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1043 if (sockfd < 0) {
1044 fprintf(stderr, "Could not open socket to kernel: %s\n",
1045 strerror(errno));
1046 exit(1);
1047 }
1048
1049 strcpy(rev.name, name);
1050 rev.revision = revision;
1051
1052 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1053 if (max_rev < 0) {
1054 /* Definitely don't support this? */
1055 if (errno == EPROTONOSUPPORT) {
1056 close(sockfd);
1057 return 0;
1058 } else if (errno == ENOPROTOOPT) {
1059 close(sockfd);
1060 /* Assume only revision 0 support (old kernel) */
1061 return (revision == 0);
1062 } else {
1063 fprintf(stderr, "getsockopt failed strangely: %s\n",
1064 strerror(errno));
1065 exit(1);
1066 }
1067 }
1068 close(sockfd);
1069 return 1;
1070}
1071
1072static int compatible_match_revision(const char *name, u_int8_t revision)
1073{
1074 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1075}
1076
1077static int compatible_target_revision(const char *name, u_int8_t revision)
1078{
1079 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1080}
1081
Marc Bouchere6869a82000-03-20 06:03:29 +00001082void
1083register_match(struct iptables_match *me)
1084{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001085 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001086
Marc Bouchere6869a82000-03-20 06:03:29 +00001087 if (strcmp(me->version, program_version) != 0) {
1088 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1089 program_name, me->name, me->version, program_version);
1090 exit(1);
1091 }
1092
Martin Josefsson93911bf2005-01-03 07:46:07 +00001093 /* Revision field stole a char from name. */
1094 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001095 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001096 program_name, me->name);
1097 exit(1);
1098 }
1099
Rusty Russell3aef54d2005-01-03 03:48:40 +00001100 old = find_match(me->name, DONT_LOAD, NULL);
1101 if (old) {
1102 if (old->revision == me->revision) {
1103 fprintf(stderr,
1104 "%s: match `%s' already registered.\n",
1105 program_name, me->name);
1106 exit(1);
1107 }
1108
1109 /* Now we have two (or more) options, check compatibility. */
1110 if (compatible_match_revision(old->name, old->revision)
1111 && old->revision > me->revision)
1112 return;
1113
1114 /* Replace if compatible. */
1115 if (!compatible_match_revision(me->name, me->revision))
1116 return;
1117
1118 /* Delete old one. */
1119 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1120 *i = old->next;
1121 }
1122
Rusty Russell73f72f52000-07-03 10:17:57 +00001123 if (me->size != IPT_ALIGN(me->size)) {
1124 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001125 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001126 exit(1);
1127 }
1128
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001129 /* Append to list. */
1130 for (i = &iptables_matches; *i; i = &(*i)->next);
1131 me->next = NULL;
1132 *i = me;
1133
Marc Bouchere6869a82000-03-20 06:03:29 +00001134 me->m = NULL;
1135 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001136}
1137
1138void
1139register_target(struct iptables_target *me)
1140{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001141 struct iptables_target *old;
1142
Marc Bouchere6869a82000-03-20 06:03:29 +00001143 if (strcmp(me->version, program_version) != 0) {
1144 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1145 program_name, me->name, me->version, program_version);
1146 exit(1);
1147 }
1148
Martin Josefsson93911bf2005-01-03 07:46:07 +00001149 /* Revision field stole a char from name. */
1150 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001151 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001152 program_name, me->name);
1153 exit(1);
1154 }
1155
Rusty Russell3aef54d2005-01-03 03:48:40 +00001156 old = find_target(me->name, DONT_LOAD);
1157 if (old) {
1158 struct iptables_target **i;
1159
1160 if (old->revision == me->revision) {
1161 fprintf(stderr,
1162 "%s: target `%s' already registered.\n",
1163 program_name, me->name);
1164 exit(1);
1165 }
1166
Rusty Russell3aef54d2005-01-03 03:48:40 +00001167 /* Now we have two (or more) options, check compatibility. */
1168 if (compatible_target_revision(old->name, old->revision)
1169 && old->revision > me->revision)
1170 return;
1171
1172 /* Replace if compatible. */
1173 if (!compatible_target_revision(me->name, me->revision))
1174 return;
1175
1176 /* Delete old one. */
1177 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1178 *i = old->next;
1179 }
1180
Rusty Russell73f72f52000-07-03 10:17:57 +00001181 if (me->size != IPT_ALIGN(me->size)) {
1182 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001183 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001184 exit(1);
1185 }
1186
Marc Bouchere6869a82000-03-20 06:03:29 +00001187 /* Prepend to list. */
1188 me->next = iptables_targets;
1189 iptables_targets = me;
1190 me->t = NULL;
1191 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001192}
1193
1194static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001195print_num(u_int64_t number, unsigned int format)
1196{
1197 if (format & FMT_KILOMEGAGIGA) {
1198 if (number > 99999) {
1199 number = (number + 500) / 1000;
1200 if (number > 9999) {
1201 number = (number + 500) / 1000;
1202 if (number > 9999) {
1203 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001204 if (number > 9999) {
1205 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001206 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001207 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001208 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001209 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001210 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001211 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001212 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001213 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001214 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001215 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001216 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001217}
1218
1219
1220static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001221print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1222{
1223 struct ipt_counters counters;
1224 const char *pol = iptc_get_policy(chain, &counters, handle);
1225 printf("Chain %s", chain);
1226 if (pol) {
1227 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001228 if (!(format & FMT_NOCOUNTS)) {
1229 fputc(' ', stdout);
1230 print_num(counters.pcnt, (format|FMT_NOTABLE));
1231 fputs("packets, ", stdout);
1232 print_num(counters.bcnt, (format|FMT_NOTABLE));
1233 fputs("bytes", stdout);
1234 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 printf(")\n");
1236 } else {
1237 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001238 if (!iptc_get_references(&refs, chain, handle))
1239 printf(" (ERROR obtaining refs)\n");
1240 else
1241 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001242 }
1243
1244 if (format & FMT_LINENUMBERS)
1245 printf(FMT("%-4s ", "%s "), "num");
1246 if (!(format & FMT_NOCOUNTS)) {
1247 if (format & FMT_KILOMEGAGIGA) {
1248 printf(FMT("%5s ","%s "), "pkts");
1249 printf(FMT("%5s ","%s "), "bytes");
1250 } else {
1251 printf(FMT("%8s ","%s "), "pkts");
1252 printf(FMT("%10s ","%s "), "bytes");
1253 }
1254 }
1255 if (!(format & FMT_NOTARGET))
1256 printf(FMT("%-9s ","%s "), "target");
1257 fputs(" prot ", stdout);
1258 if (format & FMT_OPTIONS)
1259 fputs("opt", stdout);
1260 if (format & FMT_VIA) {
1261 printf(FMT(" %-6s ","%s "), "in");
1262 printf(FMT("%-6s ","%s "), "out");
1263 }
1264 printf(FMT(" %-19s ","%s "), "source");
1265 printf(FMT(" %-19s "," %s "), "destination");
1266 printf("\n");
1267}
1268
Marc Bouchere6869a82000-03-20 06:03:29 +00001269
1270static int
1271print_match(const struct ipt_entry_match *m,
1272 const struct ipt_ip *ip,
1273 int numeric)
1274{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001275 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001276
1277 if (match) {
1278 if (match->print)
1279 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001280 else
Rusty Russellb039b022000-09-01 06:04:05 +00001281 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001282 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001283 if (m->u.user.name[0])
1284 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001285 }
1286 /* Don't stop iterating. */
1287 return 0;
1288}
1289
1290/* e is called `fw' here for hysterical raisins */
1291static void
1292print_firewall(const struct ipt_entry *fw,
1293 const char *targname,
1294 unsigned int num,
1295 unsigned int format,
1296 const iptc_handle_t handle)
1297{
1298 struct iptables_target *target = NULL;
1299 const struct ipt_entry_target *t;
1300 u_int8_t flags;
1301 char buf[BUFSIZ];
1302
Marc Bouchere6869a82000-03-20 06:03:29 +00001303 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001304 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001305 else
Rusty Russell52a51492000-05-02 16:44:29 +00001306 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001307
1308 t = ipt_get_target((struct ipt_entry *)fw);
1309 flags = fw->ip.flags;
1310
1311 if (format & FMT_LINENUMBERS)
1312 printf(FMT("%-4u ", "%u "), num+1);
1313
1314 if (!(format & FMT_NOCOUNTS)) {
1315 print_num(fw->counters.pcnt, format);
1316 print_num(fw->counters.bcnt, format);
1317 }
1318
1319 if (!(format & FMT_NOTARGET))
1320 printf(FMT("%-9s ", "%s "), targname);
1321
1322 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1323 {
Rusty Russell28381a42000-05-10 00:19:50 +00001324 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001325 if (pname)
1326 printf(FMT("%-5s", "%s "), pname);
1327 else
1328 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1329 }
1330
1331 if (format & FMT_OPTIONS) {
1332 if (format & FMT_NOTABLE)
1333 fputs("opt ", stdout);
1334 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1335 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1336 fputc(' ', stdout);
1337 }
1338
1339 if (format & FMT_VIA) {
1340 char iface[IFNAMSIZ+2];
1341
1342 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1343 iface[0] = '!';
1344 iface[1] = '\0';
1345 }
1346 else iface[0] = '\0';
1347
1348 if (fw->ip.iniface[0] != '\0') {
1349 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 }
1351 else if (format & FMT_NUMERIC) strcat(iface, "*");
1352 else strcat(iface, "any");
1353 printf(FMT(" %-6s ","in %s "), iface);
1354
1355 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1356 iface[0] = '!';
1357 iface[1] = '\0';
1358 }
1359 else iface[0] = '\0';
1360
1361 if (fw->ip.outiface[0] != '\0') {
1362 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001363 }
1364 else if (format & FMT_NUMERIC) strcat(iface, "*");
1365 else strcat(iface, "any");
1366 printf(FMT("%-6s ","out %s "), iface);
1367 }
1368
1369 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1370 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1371 printf(FMT("%-19s ","%s "), "anywhere");
1372 else {
1373 if (format & FMT_NUMERIC)
1374 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1375 else
1376 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1377 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1378 printf(FMT("%-19s ","%s "), buf);
1379 }
1380
1381 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1382 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001383 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001384 else {
1385 if (format & FMT_NUMERIC)
1386 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1387 else
1388 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1389 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001390 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001391 }
1392
1393 if (format & FMT_NOTABLE)
1394 fputs(" ", stdout);
1395
1396 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1397
1398 if (target) {
1399 if (target->print)
1400 /* Print the target information. */
1401 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001402 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001403 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001404 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001405
1406 if (!(format & FMT_NONEWLINE))
1407 fputc('\n', stdout);
1408}
1409
1410static void
1411print_firewall_line(const struct ipt_entry *fw,
1412 const iptc_handle_t h)
1413{
1414 struct ipt_entry_target *t;
1415
1416 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001417 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001418}
1419
1420static int
1421append_entry(const ipt_chainlabel chain,
1422 struct ipt_entry *fw,
1423 unsigned int nsaddrs,
1424 const struct in_addr saddrs[],
1425 unsigned int ndaddrs,
1426 const struct in_addr daddrs[],
1427 int verbose,
1428 iptc_handle_t *handle)
1429{
1430 unsigned int i, j;
1431 int ret = 1;
1432
1433 for (i = 0; i < nsaddrs; i++) {
1434 fw->ip.src.s_addr = saddrs[i].s_addr;
1435 for (j = 0; j < ndaddrs; j++) {
1436 fw->ip.dst.s_addr = daddrs[j].s_addr;
1437 if (verbose)
1438 print_firewall_line(fw, *handle);
1439 ret &= iptc_append_entry(chain, fw, handle);
1440 }
1441 }
1442
1443 return ret;
1444}
1445
1446static int
1447replace_entry(const ipt_chainlabel chain,
1448 struct ipt_entry *fw,
1449 unsigned int rulenum,
1450 const struct in_addr *saddr,
1451 const struct in_addr *daddr,
1452 int verbose,
1453 iptc_handle_t *handle)
1454{
1455 fw->ip.src.s_addr = saddr->s_addr;
1456 fw->ip.dst.s_addr = daddr->s_addr;
1457
1458 if (verbose)
1459 print_firewall_line(fw, *handle);
1460 return iptc_replace_entry(chain, fw, rulenum, handle);
1461}
1462
1463static int
1464insert_entry(const ipt_chainlabel chain,
1465 struct ipt_entry *fw,
1466 unsigned int rulenum,
1467 unsigned int nsaddrs,
1468 const struct in_addr saddrs[],
1469 unsigned int ndaddrs,
1470 const struct in_addr daddrs[],
1471 int verbose,
1472 iptc_handle_t *handle)
1473{
1474 unsigned int i, j;
1475 int ret = 1;
1476
1477 for (i = 0; i < nsaddrs; i++) {
1478 fw->ip.src.s_addr = saddrs[i].s_addr;
1479 for (j = 0; j < ndaddrs; j++) {
1480 fw->ip.dst.s_addr = daddrs[j].s_addr;
1481 if (verbose)
1482 print_firewall_line(fw, *handle);
1483 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1484 }
1485 }
1486
1487 return ret;
1488}
1489
Rusty Russell2e0a3212000-04-19 11:23:18 +00001490static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001491make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001492{
1493 /* Establish mask for comparison */
1494 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001495 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001496 unsigned char *mask, *mptr;
1497
1498 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001499 for (matchp = matches; matchp; matchp = matchp->next)
1500 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001501
Rusty Russell9e1d2142000-04-23 09:11:12 +00001502 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001503 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001504 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001505
Rusty Russell9e1d2142000-04-23 09:11:12 +00001506 memset(mask, 0xFF, sizeof(struct ipt_entry));
1507 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001508
Martin Josefsson78cafda2004-02-02 20:01:18 +00001509 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001510 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001511 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001512 + matchp->match->userspacesize);
1513 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001514 }
1515
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001516 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001517 IPT_ALIGN(sizeof(struct ipt_entry_target))
1518 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001519
1520 return mask;
1521}
1522
Marc Bouchere6869a82000-03-20 06:03:29 +00001523static int
1524delete_entry(const ipt_chainlabel chain,
1525 struct ipt_entry *fw,
1526 unsigned int nsaddrs,
1527 const struct in_addr saddrs[],
1528 unsigned int ndaddrs,
1529 const struct in_addr daddrs[],
1530 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001531 iptc_handle_t *handle,
1532 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001533{
1534 unsigned int i, j;
1535 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001536 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001537
Martin Josefsson78cafda2004-02-02 20:01:18 +00001538 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001539 for (i = 0; i < nsaddrs; i++) {
1540 fw->ip.src.s_addr = saddrs[i].s_addr;
1541 for (j = 0; j < ndaddrs; j++) {
1542 fw->ip.dst.s_addr = daddrs[j].s_addr;
1543 if (verbose)
1544 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001545 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001546 }
1547 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001548 free(mask);
1549
Marc Bouchere6869a82000-03-20 06:03:29 +00001550 return ret;
1551}
1552
Harald Welteae1ff9f2000-12-01 14:26:20 +00001553int
Marc Bouchere6869a82000-03-20 06:03:29 +00001554for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001555 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001556{
1557 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001558 const char *chain;
1559 char *chains;
1560 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001561
Rusty Russell9e1d2142000-04-23 09:11:12 +00001562 chain = iptc_first_chain(handle);
1563 while (chain) {
1564 chaincount++;
1565 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001566 }
1567
Rusty Russell9e1d2142000-04-23 09:11:12 +00001568 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1569 i = 0;
1570 chain = iptc_first_chain(handle);
1571 while (chain) {
1572 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1573 i++;
1574 chain = iptc_next_chain(handle);
1575 }
1576
1577 for (i = 0; i < chaincount; i++) {
1578 if (!builtinstoo
1579 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001580 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001581 continue;
1582 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1583 }
1584
1585 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001586 return ret;
1587}
1588
Harald Welteae1ff9f2000-12-01 14:26:20 +00001589int
Marc Bouchere6869a82000-03-20 06:03:29 +00001590flush_entries(const ipt_chainlabel chain, int verbose,
1591 iptc_handle_t *handle)
1592{
1593 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001594 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001595
1596 if (verbose)
1597 fprintf(stdout, "Flushing chain `%s'\n", chain);
1598 return iptc_flush_entries(chain, handle);
1599}
Marc Bouchere6869a82000-03-20 06:03:29 +00001600
1601static int
1602zero_entries(const ipt_chainlabel chain, int verbose,
1603 iptc_handle_t *handle)
1604{
1605 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001606 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001607
Marc Bouchere6869a82000-03-20 06:03:29 +00001608 if (verbose)
1609 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1610 return iptc_zero_entries(chain, handle);
1611}
1612
Harald Welteae1ff9f2000-12-01 14:26:20 +00001613int
Marc Bouchere6869a82000-03-20 06:03:29 +00001614delete_chain(const ipt_chainlabel chain, int verbose,
1615 iptc_handle_t *handle)
1616{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001617 if (!chain)
1618 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001619
1620 if (verbose)
1621 fprintf(stdout, "Deleting chain `%s'\n", chain);
1622 return iptc_delete_chain(chain, handle);
1623}
1624
1625static int
1626list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1627 int expanded, int linenumbers, iptc_handle_t *handle)
1628{
1629 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001630 unsigned int format;
1631 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001632
1633 format = FMT_OPTIONS;
1634 if (!verbose)
1635 format |= FMT_NOCOUNTS;
1636 else
1637 format |= FMT_VIA;
1638
1639 if (numeric)
1640 format |= FMT_NUMERIC;
1641
1642 if (!expanded)
1643 format |= FMT_KILOMEGAGIGA;
1644
1645 if (linenumbers)
1646 format |= FMT_LINENUMBERS;
1647
Rusty Russell9e1d2142000-04-23 09:11:12 +00001648 for (this = iptc_first_chain(handle);
1649 this;
1650 this = iptc_next_chain(handle)) {
1651 const struct ipt_entry *i;
1652 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001653
Marc Bouchere6869a82000-03-20 06:03:29 +00001654 if (chain && strcmp(chain, this) != 0)
1655 continue;
1656
1657 if (found) printf("\n");
1658
1659 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001660 i = iptc_first_rule(this, handle);
1661
1662 num = 0;
1663 while (i) {
1664 print_firewall(i,
1665 iptc_get_target(i, handle),
1666 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001667 format,
1668 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001669 i = iptc_next_rule(i, handle);
1670 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001671 found = 1;
1672 }
1673
1674 errno = ENOENT;
1675 return found;
1676}
1677
Harald Welte82dd2ec2000-12-19 05:18:15 +00001678static char *get_modprobe(void)
1679{
1680 int procfile;
1681 char *ret;
1682
Harald Welte10f7f142004-10-22 08:14:07 +00001683#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001684 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1685 if (procfile < 0)
1686 return NULL;
1687
Harald Welte10f7f142004-10-22 08:14:07 +00001688 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001689 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001690 memset(ret, 0, PROCFILE_BUFSIZ);
1691 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001692 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001693 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001694 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001695 if (ret[strlen(ret)-1]=='\n')
1696 ret[strlen(ret)-1]=0;
1697 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001698 return ret;
1699 }
1700 fail:
1701 free(ret);
1702 close(procfile);
1703 return NULL;
1704}
1705
Harald Welte58918652001-06-16 18:25:25 +00001706int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001707{
1708 char *buf = NULL;
1709 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001710 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001711
1712 /* If they don't explicitly set it, read out of kernel */
1713 if (!modprobe) {
1714 buf = get_modprobe();
1715 if (!buf)
1716 return -1;
1717 modprobe = buf;
1718 }
1719
1720 switch (fork()) {
1721 case 0:
1722 argv[0] = (char *)modprobe;
1723 argv[1] = (char *)modname;
1724 argv[2] = NULL;
1725 execv(argv[0], argv);
1726
1727 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001728 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001729 case -1:
1730 return -1;
1731
1732 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001733 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001734 }
1735
1736 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001737 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1738 return 0;
1739 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001740}
1741
Marc Bouchere6869a82000-03-20 06:03:29 +00001742static struct ipt_entry *
1743generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001744 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001745 struct ipt_entry_target *target)
1746{
1747 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001748 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001749 struct ipt_entry *e;
1750
1751 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001752 for (matchp = matches; matchp; matchp = matchp->next)
1753 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001754
Rusty Russell228e98d2000-04-27 10:28:06 +00001755 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001756 *e = *fw;
1757 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001758 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001759
1760 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001761 for (matchp = matches; matchp; matchp = matchp->next) {
1762 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1763 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001765 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001766
1767 return e;
1768}
1769
Martin Josefsson78cafda2004-02-02 20:01:18 +00001770void clear_rule_matches(struct iptables_rule_match **matches)
1771{
1772 struct iptables_rule_match *matchp, *tmp;
1773
1774 for (matchp = *matches; matchp;) {
1775 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001776 if (matchp->match->m)
1777 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001778 free(matchp);
1779 matchp = tmp;
1780 }
1781
1782 *matches = NULL;
1783}
1784
Rusty Russell3aef54d2005-01-03 03:48:40 +00001785static void set_revision(char *name, u_int8_t revision)
1786{
1787 /* Old kernel sources don't have ".revision" field,
1788 but we stole a byte from name. */
1789 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1790 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1791}
1792
Marc Bouchere6869a82000-03-20 06:03:29 +00001793int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1794{
1795 struct ipt_entry fw, *e = NULL;
1796 int invert = 0;
1797 unsigned int nsaddrs = 0, ndaddrs = 0;
1798 struct in_addr *saddrs = NULL, *daddrs = NULL;
1799
1800 int c, verbose = 0;
1801 const char *chain = NULL;
1802 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1803 const char *policy = NULL, *newname = NULL;
1804 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001805 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 int ret = 1;
1807 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001808 struct iptables_rule_match *matches = NULL;
1809 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001811 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001812 const char *jumpto = "";
1813 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001814 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001815 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001816
1817 memset(&fw, 0, sizeof(fw));
1818
Harald Welteae1ff9f2000-12-01 14:26:20 +00001819 /* re-set optind to 0 in case do_command gets called
1820 * a second time */
1821 optind = 0;
1822
1823 /* clear mflags in case do_command gets called a second time
1824 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001825 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001826 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001827
1828 for (t = iptables_targets; t; t = t->next) {
1829 t->tflags = 0;
1830 t->used = 0;
1831 }
1832
Marc Bouchere6869a82000-03-20 06:03:29 +00001833 /* Suppress error messages: we may add new options if we
1834 demand-load a protocol. */
1835 opterr = 0;
1836
1837 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001838 "-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 +00001839 opts, NULL)) != -1) {
1840 switch (c) {
1841 /*
1842 * Command selection
1843 */
1844 case 'A':
1845 add_command(&command, CMD_APPEND, CMD_NONE,
1846 invert);
1847 chain = optarg;
1848 break;
1849
1850 case 'D':
1851 add_command(&command, CMD_DELETE, CMD_NONE,
1852 invert);
1853 chain = optarg;
1854 if (optind < argc && argv[optind][0] != '-'
1855 && argv[optind][0] != '!') {
1856 rulenum = parse_rulenumber(argv[optind++]);
1857 command = CMD_DELETE_NUM;
1858 }
1859 break;
1860
Marc Bouchere6869a82000-03-20 06:03:29 +00001861 case 'R':
1862 add_command(&command, CMD_REPLACE, CMD_NONE,
1863 invert);
1864 chain = optarg;
1865 if (optind < argc && argv[optind][0] != '-'
1866 && argv[optind][0] != '!')
1867 rulenum = parse_rulenumber(argv[optind++]);
1868 else
1869 exit_error(PARAMETER_PROBLEM,
1870 "-%c requires a rule number",
1871 cmd2char(CMD_REPLACE));
1872 break;
1873
1874 case 'I':
1875 add_command(&command, CMD_INSERT, CMD_NONE,
1876 invert);
1877 chain = optarg;
1878 if (optind < argc && argv[optind][0] != '-'
1879 && argv[optind][0] != '!')
1880 rulenum = parse_rulenumber(argv[optind++]);
1881 else rulenum = 1;
1882 break;
1883
1884 case 'L':
1885 add_command(&command, CMD_LIST, CMD_ZERO,
1886 invert);
1887 if (optarg) chain = optarg;
1888 else if (optind < argc && argv[optind][0] != '-'
1889 && argv[optind][0] != '!')
1890 chain = argv[optind++];
1891 break;
1892
1893 case 'F':
1894 add_command(&command, CMD_FLUSH, CMD_NONE,
1895 invert);
1896 if (optarg) chain = optarg;
1897 else if (optind < argc && argv[optind][0] != '-'
1898 && argv[optind][0] != '!')
1899 chain = argv[optind++];
1900 break;
1901
1902 case 'Z':
1903 add_command(&command, CMD_ZERO, CMD_LIST,
1904 invert);
1905 if (optarg) chain = optarg;
1906 else if (optind < argc && argv[optind][0] != '-'
1907 && argv[optind][0] != '!')
1908 chain = argv[optind++];
1909 break;
1910
1911 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001912 if (optarg && *optarg == '-')
1913 exit_error(PARAMETER_PROBLEM,
1914 "chain name not allowed to start "
1915 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001916 if (find_target(optarg, TRY_LOAD))
1917 exit_error(PARAMETER_PROBLEM,
1918 "chain name may not clash "
1919 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001920 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1921 invert);
1922 chain = optarg;
1923 break;
1924
1925 case 'X':
1926 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1927 invert);
1928 if (optarg) chain = optarg;
1929 else if (optind < argc && argv[optind][0] != '-'
1930 && argv[optind][0] != '!')
1931 chain = argv[optind++];
1932 break;
1933
1934 case 'E':
1935 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1936 invert);
1937 chain = optarg;
1938 if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001941 else
1942 exit_error(PARAMETER_PROBLEM,
1943 "-%c requires old-chain-name and "
1944 "new-chain-name",
1945 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001946 break;
1947
1948 case 'P':
1949 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1950 invert);
1951 chain = optarg;
1952 if (optind < argc && argv[optind][0] != '-'
1953 && argv[optind][0] != '!')
1954 policy = argv[optind++];
1955 else
1956 exit_error(PARAMETER_PROBLEM,
1957 "-%c requires a chain and a policy",
1958 cmd2char(CMD_SET_POLICY));
1959 break;
1960
1961 case 'h':
1962 if (!optarg)
1963 optarg = argv[optind];
1964
Rusty Russell2e0a3212000-04-19 11:23:18 +00001965 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001966 if (!matches && protocol)
1967 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001968
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001969 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001970
1971 /*
1972 * Option selection
1973 */
1974 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001975 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001976 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1977 invert);
1978
1979 /* Canonicalize into lower case */
1980 for (protocol = argv[optind-1]; *protocol; protocol++)
1981 *protocol = tolower(*protocol);
1982
1983 protocol = argv[optind-1];
1984 fw.ip.proto = parse_protocol(protocol);
1985
1986 if (fw.ip.proto == 0
1987 && (fw.ip.invflags & IPT_INV_PROTO))
1988 exit_error(PARAMETER_PROBLEM,
1989 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001990 break;
1991
1992 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001993 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001994 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1995 invert);
1996 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001997 break;
1998
1999 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002000 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002001 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2002 invert);
2003 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 break;
2005
2006 case 'j':
2007 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2008 invert);
2009 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002010 /* TRY_LOAD (may be chain name) */
2011 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
2013 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002014 size_t size;
2015
Rusty Russell73f72f52000-07-03 10:17:57 +00002016 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2017 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002018
Rusty Russell2e0a3212000-04-19 11:23:18 +00002019 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002020 target->t->u.target_size = size;
2021 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002022 set_revision(target->t->u.user.name,
2023 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002024 if (target->init != NULL)
2025 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002026 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 }
2028 break;
2029
2030
2031 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002032 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002033 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2034 invert);
2035 parse_interface(argv[optind-1],
2036 fw.ip.iniface,
2037 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 break;
2039
2040 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002041 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002042 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2043 invert);
2044 parse_interface(argv[optind-1],
2045 fw.ip.outiface,
2046 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 break;
2048
2049 case 'f':
2050 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2051 invert);
2052 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002053 break;
2054
2055 case 'v':
2056 if (!verbose)
2057 set_option(&options, OPT_VERBOSE,
2058 &fw.ip.invflags, invert);
2059 verbose++;
2060 break;
2061
Rusty Russell52a51492000-05-02 16:44:29 +00002062 case 'm': {
2063 size_t size;
2064
Marc Bouchere6869a82000-03-20 06:03:29 +00002065 if (invert)
2066 exit_error(PARAMETER_PROBLEM,
2067 "unexpected ! flag before --match");
2068
Martin Josefsson78cafda2004-02-02 20:01:18 +00002069 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002070 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2071 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002072 m->m = fw_calloc(1, size);
2073 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002074 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002075 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002076 if (m->init != NULL)
2077 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002078 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002079 }
2080 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002081
2082 case 'n':
2083 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2084 invert);
2085 break;
2086
2087 case 't':
2088 if (invert)
2089 exit_error(PARAMETER_PROBLEM,
2090 "unexpected ! flag before --table");
2091 *table = argv[optind-1];
2092 break;
2093
2094 case 'x':
2095 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2096 invert);
2097 break;
2098
2099 case 'V':
2100 if (invert)
2101 printf("Not %s ;-)\n", program_version);
2102 else
2103 printf("%s v%s\n",
2104 program_name, program_version);
2105 exit(0);
2106
2107 case '0':
2108 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2109 invert);
2110 break;
2111
Harald Welte82dd2ec2000-12-19 05:18:15 +00002112 case 'M':
2113 modprobe = optarg;
2114 break;
2115
Harald Welteccd49e52001-01-23 22:54:34 +00002116 case 'c':
2117
2118 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2119 invert);
2120 pcnt = optarg;
2121 if (optind < argc && argv[optind][0] != '-'
2122 && argv[optind][0] != '!')
2123 bcnt = argv[optind++];
2124 else
2125 exit_error(PARAMETER_PROBLEM,
2126 "-%c requires packet and byte counter",
2127 opt2char(OPT_COUNTERS));
2128
Martin Josefssona28d4952004-05-26 16:04:48 +00002129 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002130 exit_error(PARAMETER_PROBLEM,
2131 "-%c packet counter not numeric",
2132 opt2char(OPT_COUNTERS));
2133
Martin Josefssona28d4952004-05-26 16:04:48 +00002134 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002135 exit_error(PARAMETER_PROBLEM,
2136 "-%c byte counter not numeric",
2137 opt2char(OPT_COUNTERS));
2138
2139 break;
2140
2141
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 case 1: /* non option */
2143 if (optarg[0] == '!' && optarg[1] == '\0') {
2144 if (invert)
2145 exit_error(PARAMETER_PROBLEM,
2146 "multiple consecutive ! not"
2147 " allowed");
2148 invert = TRUE;
2149 optarg[0] = '\0';
2150 continue;
2151 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002152 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002153 exit_tryhelp(2);
2154
2155 default:
2156 /* FIXME: This scheme doesn't allow two of the same
2157 matches --RR */
2158 if (!target
2159 || !(target->parse(c - target->option_offset,
2160 argv, invert,
2161 &target->tflags,
2162 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002163 for (matchp = matches; matchp; matchp = matchp->next) {
2164 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002166 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002167 &fw,
2168 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002169 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002170 break;
2171 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002172 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002173
2174 /* If you listen carefully, you can
2175 actually hear this code suck. */
2176
2177 /* some explanations (after four different bugs
2178 * in 3 different releases): If we encountere a
2179 * parameter, that has not been parsed yet,
2180 * it's not an option of an explicitly loaded
2181 * match or a target. However, we support
2182 * implicit loading of the protocol match
2183 * extension. '-p tcp' means 'l4 proto 6' and
2184 * at the same time 'load tcp protocol match on
2185 * demand if we specify --dport'.
2186 *
2187 * To make this work, we need to make sure:
2188 * - the parameter has not been parsed by
2189 * a match (m above)
2190 * - a protocol has been specified
2191 * - the protocol extension has not been
2192 * loaded yet, or is loaded and unused
2193 * [think of iptables-restore!]
2194 * - the protocol extension can be successively
2195 * loaded
2196 */
2197 if (m == NULL
2198 && protocol
2199 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002200 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002201 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002202 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002203 && (proto_used == 0))
2204 )
2205 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002206 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002207 /* Try loading protocol */
2208 size_t size;
2209
2210 proto_used = 1;
2211
2212 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2213 + m->size;
2214
2215 m->m = fw_calloc(1, size);
2216 m->m->u.match_size = size;
2217 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002218 set_revision(m->m->u.user.name,
2219 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002220 if (m->init != NULL)
2221 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002222
2223 opts = merge_options(opts,
2224 m->extra_opts, &m->option_offset);
2225
2226 optind--;
2227 continue;
2228 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002229 if (!m)
2230 exit_error(PARAMETER_PROBLEM,
2231 "Unknown arg `%s'",
2232 argv[optind-1]);
2233 }
2234 }
2235 invert = FALSE;
2236 }
2237
Martin Josefsson78cafda2004-02-02 20:01:18 +00002238 for (matchp = matches; matchp; matchp = matchp->next)
2239 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002240
Marc Bouchere6869a82000-03-20 06:03:29 +00002241 if (target)
2242 target->final_check(target->tflags);
2243
2244 /* Fix me: must put inverse options checking here --MN */
2245
2246 if (optind < argc)
2247 exit_error(PARAMETER_PROBLEM,
2248 "unknown arguments found on commandline");
2249 if (!command)
2250 exit_error(PARAMETER_PROBLEM, "no command specified");
2251 if (invert)
2252 exit_error(PARAMETER_PROBLEM,
2253 "nothing appropriate following !");
2254
Harald Welte6336bfd2002-05-07 14:41:43 +00002255 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002256 if (!(options & OPT_DESTINATION))
2257 dhostnetworkmask = "0.0.0.0/0";
2258 if (!(options & OPT_SOURCE))
2259 shostnetworkmask = "0.0.0.0/0";
2260 }
2261
2262 if (shostnetworkmask)
2263 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2264 &(fw.ip.smsk), &nsaddrs);
2265
2266 if (dhostnetworkmask)
2267 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2268 &(fw.ip.dmsk), &ndaddrs);
2269
2270 if ((nsaddrs > 1 || ndaddrs > 1) &&
2271 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2272 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2273 " source or destination IP addresses");
2274
Marc Bouchere6869a82000-03-20 06:03:29 +00002275 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2276 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2277 "specify a unique address");
2278
2279 generic_opt_check(command, options);
2280
2281 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2282 exit_error(PARAMETER_PROBLEM,
2283 "chain name `%s' too long (must be under %i chars)",
2284 chain, IPT_FUNCTION_MAXNAMELEN);
2285
Harald Welteae1ff9f2000-12-01 14:26:20 +00002286 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002287 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002288 *handle = iptc_init(*table);
2289
Rusty Russell8beb0492004-12-22 00:37:10 +00002290 /* try to insmod the module if iptc_init failed */
2291 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002292 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002293
Marc Bouchere6869a82000-03-20 06:03:29 +00002294 if (!*handle)
2295 exit_error(VERSION_PROBLEM,
2296 "can't initialize iptables table `%s': %s",
2297 *table, iptc_strerror(errno));
2298
Harald Welte6336bfd2002-05-07 14:41:43 +00002299 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002300 || command == CMD_DELETE
2301 || command == CMD_INSERT
2302 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002303 if (strcmp(chain, "PREROUTING") == 0
2304 || strcmp(chain, "INPUT") == 0) {
2305 /* -o not valid with incoming packets. */
2306 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002307 exit_error(PARAMETER_PROBLEM,
2308 "Can't use -%c with %s\n",
2309 opt2char(OPT_VIANAMEOUT),
2310 chain);
2311 }
2312
Rusty Russella4860fd2000-06-17 16:13:02 +00002313 if (strcmp(chain, "POSTROUTING") == 0
2314 || strcmp(chain, "OUTPUT") == 0) {
2315 /* -i not valid with outgoing packets */
2316 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002317 exit_error(PARAMETER_PROBLEM,
2318 "Can't use -%c with %s\n",
2319 opt2char(OPT_VIANAMEIN),
2320 chain);
2321 }
2322
2323 if (target && iptc_is_chain(jumpto, *handle)) {
2324 printf("Warning: using chain %s, not extension\n",
2325 jumpto);
2326
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002327 if (target->t)
2328 free(target->t);
2329
Marc Bouchere6869a82000-03-20 06:03:29 +00002330 target = NULL;
2331 }
2332
2333 /* If they didn't specify a target, or it's a chain
2334 name, use standard. */
2335 if (!target
2336 && (strlen(jumpto) == 0
2337 || iptc_is_chain(jumpto, *handle))) {
2338 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002339
Rusty Russell52a51492000-05-02 16:44:29 +00002340 target = find_target(IPT_STANDARD_TARGET,
2341 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002342
2343 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002344 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002345 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002346 target->t->u.target_size = size;
2347 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002348 set_revision(target->t->u.user.name, target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002349 if (target->init != NULL)
2350 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002351 }
2352
Rusty Russell7e53bf92000-03-20 07:03:28 +00002353 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002354 /* it is no chain, and we can't load a plugin.
2355 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002356 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002357 * chain. */
2358 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002359 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002360 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002361 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002362 }
2363 }
2364
2365 switch (command) {
2366 case CMD_APPEND:
2367 ret = append_entry(chain, e,
2368 nsaddrs, saddrs, ndaddrs, daddrs,
2369 options&OPT_VERBOSE,
2370 handle);
2371 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002372 case CMD_DELETE:
2373 ret = delete_entry(chain, e,
2374 nsaddrs, saddrs, ndaddrs, daddrs,
2375 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002376 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002377 break;
2378 case CMD_DELETE_NUM:
2379 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2380 break;
2381 case CMD_REPLACE:
2382 ret = replace_entry(chain, e, rulenum - 1,
2383 saddrs, daddrs, options&OPT_VERBOSE,
2384 handle);
2385 break;
2386 case CMD_INSERT:
2387 ret = insert_entry(chain, e, rulenum - 1,
2388 nsaddrs, saddrs, ndaddrs, daddrs,
2389 options&OPT_VERBOSE,
2390 handle);
2391 break;
2392 case CMD_LIST:
2393 ret = list_entries(chain,
2394 options&OPT_VERBOSE,
2395 options&OPT_NUMERIC,
2396 options&OPT_EXPANDED,
2397 options&OPT_LINENUMBERS,
2398 handle);
2399 break;
2400 case CMD_FLUSH:
2401 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2402 break;
2403 case CMD_ZERO:
2404 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2405 break;
2406 case CMD_LIST|CMD_ZERO:
2407 ret = list_entries(chain,
2408 options&OPT_VERBOSE,
2409 options&OPT_NUMERIC,
2410 options&OPT_EXPANDED,
2411 options&OPT_LINENUMBERS,
2412 handle);
2413 if (ret)
2414 ret = zero_entries(chain,
2415 options&OPT_VERBOSE, handle);
2416 break;
2417 case CMD_NEW_CHAIN:
2418 ret = iptc_create_chain(chain, handle);
2419 break;
2420 case CMD_DELETE_CHAIN:
2421 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2422 break;
2423 case CMD_RENAME_CHAIN:
2424 ret = iptc_rename_chain(chain, newname, handle);
2425 break;
2426 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002427 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002428 break;
2429 default:
2430 /* We should never reach this... */
2431 exit_tryhelp(2);
2432 }
2433
2434 if (verbose > 1)
2435 dump_entries(*handle);
2436
Martin Josefsson78cafda2004-02-02 20:01:18 +00002437 clear_rule_matches(&matches);
2438
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002439 if (e != NULL) {
2440 free(e);
2441 e = NULL;
2442 }
2443
keso6997cdf2004-07-04 15:20:53 +00002444 free(saddrs);
2445 free(daddrs);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002446
2447 if (opts != original_opts) {
2448 free(opts);
2449 opts = original_opts;
2450 global_option_offset = 0;
2451 }
2452
Marc Bouchere6869a82000-03-20 06:03:29 +00002453 return ret;
2454}