blob: 3cedaccf72d5d6fd5e82b0b47608a0fd4772ef64 [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
Rusty Russell3aef54d2005-01-03 03:48:40 +00001093 /* Revision field stole a char: check for 30 char names. */
1094 if (!memchr(me->name, 0, IPT_FUNCTION_MAXNAMELEN-1)) {
1095 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
Rusty Russell3aef54d2005-01-03 03:48:40 +00001149 /* Revision field stole a char: check for 30 char names. */
1150 if (!memchr(me->name, 0, IPT_FUNCTION_MAXNAMELEN)) {
1151 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
1167 fprintf(stderr, "%s v%i vs v%i\n",
1168 me->name, me->revision, old->revision);
1169
1170 /* Now we have two (or more) options, check compatibility. */
1171 if (compatible_target_revision(old->name, old->revision)
1172 && old->revision > me->revision)
1173 return;
1174
1175 /* Replace if compatible. */
1176 if (!compatible_target_revision(me->name, me->revision))
1177 return;
1178
1179 /* Delete old one. */
1180 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1181 *i = old->next;
1182 }
1183
Rusty Russell73f72f52000-07-03 10:17:57 +00001184 if (me->size != IPT_ALIGN(me->size)) {
1185 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001186 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001187 exit(1);
1188 }
1189
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 /* Prepend to list. */
1191 me->next = iptables_targets;
1192 iptables_targets = me;
1193 me->t = NULL;
1194 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001195}
1196
1197static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001198print_num(u_int64_t number, unsigned int format)
1199{
1200 if (format & FMT_KILOMEGAGIGA) {
1201 if (number > 99999) {
1202 number = (number + 500) / 1000;
1203 if (number > 9999) {
1204 number = (number + 500) / 1000;
1205 if (number > 9999) {
1206 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001207 if (number > 9999) {
1208 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001209 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001210 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001211 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001212 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001213 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001214 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001215 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001216 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001217 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001218 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001219 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001220}
1221
1222
1223static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001224print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1225{
1226 struct ipt_counters counters;
1227 const char *pol = iptc_get_policy(chain, &counters, handle);
1228 printf("Chain %s", chain);
1229 if (pol) {
1230 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001231 if (!(format & FMT_NOCOUNTS)) {
1232 fputc(' ', stdout);
1233 print_num(counters.pcnt, (format|FMT_NOTABLE));
1234 fputs("packets, ", stdout);
1235 print_num(counters.bcnt, (format|FMT_NOTABLE));
1236 fputs("bytes", stdout);
1237 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001238 printf(")\n");
1239 } else {
1240 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001241 if (!iptc_get_references(&refs, chain, handle))
1242 printf(" (ERROR obtaining refs)\n");
1243 else
1244 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001245 }
1246
1247 if (format & FMT_LINENUMBERS)
1248 printf(FMT("%-4s ", "%s "), "num");
1249 if (!(format & FMT_NOCOUNTS)) {
1250 if (format & FMT_KILOMEGAGIGA) {
1251 printf(FMT("%5s ","%s "), "pkts");
1252 printf(FMT("%5s ","%s "), "bytes");
1253 } else {
1254 printf(FMT("%8s ","%s "), "pkts");
1255 printf(FMT("%10s ","%s "), "bytes");
1256 }
1257 }
1258 if (!(format & FMT_NOTARGET))
1259 printf(FMT("%-9s ","%s "), "target");
1260 fputs(" prot ", stdout);
1261 if (format & FMT_OPTIONS)
1262 fputs("opt", stdout);
1263 if (format & FMT_VIA) {
1264 printf(FMT(" %-6s ","%s "), "in");
1265 printf(FMT("%-6s ","%s "), "out");
1266 }
1267 printf(FMT(" %-19s ","%s "), "source");
1268 printf(FMT(" %-19s "," %s "), "destination");
1269 printf("\n");
1270}
1271
Marc Bouchere6869a82000-03-20 06:03:29 +00001272
1273static int
1274print_match(const struct ipt_entry_match *m,
1275 const struct ipt_ip *ip,
1276 int numeric)
1277{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001278 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001279
1280 if (match) {
1281 if (match->print)
1282 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001283 else
Rusty Russellb039b022000-09-01 06:04:05 +00001284 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001285 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001286 if (m->u.user.name[0])
1287 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 }
1289 /* Don't stop iterating. */
1290 return 0;
1291}
1292
1293/* e is called `fw' here for hysterical raisins */
1294static void
1295print_firewall(const struct ipt_entry *fw,
1296 const char *targname,
1297 unsigned int num,
1298 unsigned int format,
1299 const iptc_handle_t handle)
1300{
1301 struct iptables_target *target = NULL;
1302 const struct ipt_entry_target *t;
1303 u_int8_t flags;
1304 char buf[BUFSIZ];
1305
Marc Bouchere6869a82000-03-20 06:03:29 +00001306 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001307 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001308 else
Rusty Russell52a51492000-05-02 16:44:29 +00001309 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001310
1311 t = ipt_get_target((struct ipt_entry *)fw);
1312 flags = fw->ip.flags;
1313
1314 if (format & FMT_LINENUMBERS)
1315 printf(FMT("%-4u ", "%u "), num+1);
1316
1317 if (!(format & FMT_NOCOUNTS)) {
1318 print_num(fw->counters.pcnt, format);
1319 print_num(fw->counters.bcnt, format);
1320 }
1321
1322 if (!(format & FMT_NOTARGET))
1323 printf(FMT("%-9s ", "%s "), targname);
1324
1325 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1326 {
Rusty Russell28381a42000-05-10 00:19:50 +00001327 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 if (pname)
1329 printf(FMT("%-5s", "%s "), pname);
1330 else
1331 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1332 }
1333
1334 if (format & FMT_OPTIONS) {
1335 if (format & FMT_NOTABLE)
1336 fputs("opt ", stdout);
1337 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1338 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1339 fputc(' ', stdout);
1340 }
1341
1342 if (format & FMT_VIA) {
1343 char iface[IFNAMSIZ+2];
1344
1345 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1346 iface[0] = '!';
1347 iface[1] = '\0';
1348 }
1349 else iface[0] = '\0';
1350
1351 if (fw->ip.iniface[0] != '\0') {
1352 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001353 }
1354 else if (format & FMT_NUMERIC) strcat(iface, "*");
1355 else strcat(iface, "any");
1356 printf(FMT(" %-6s ","in %s "), iface);
1357
1358 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1359 iface[0] = '!';
1360 iface[1] = '\0';
1361 }
1362 else iface[0] = '\0';
1363
1364 if (fw->ip.outiface[0] != '\0') {
1365 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001366 }
1367 else if (format & FMT_NUMERIC) strcat(iface, "*");
1368 else strcat(iface, "any");
1369 printf(FMT("%-6s ","out %s "), iface);
1370 }
1371
1372 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1373 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1374 printf(FMT("%-19s ","%s "), "anywhere");
1375 else {
1376 if (format & FMT_NUMERIC)
1377 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1378 else
1379 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1380 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1381 printf(FMT("%-19s ","%s "), buf);
1382 }
1383
1384 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1385 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001386 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 else {
1388 if (format & FMT_NUMERIC)
1389 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1390 else
1391 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1392 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001393 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001394 }
1395
1396 if (format & FMT_NOTABLE)
1397 fputs(" ", stdout);
1398
1399 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1400
1401 if (target) {
1402 if (target->print)
1403 /* Print the target information. */
1404 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001405 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001406 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001407 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001408
1409 if (!(format & FMT_NONEWLINE))
1410 fputc('\n', stdout);
1411}
1412
1413static void
1414print_firewall_line(const struct ipt_entry *fw,
1415 const iptc_handle_t h)
1416{
1417 struct ipt_entry_target *t;
1418
1419 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001420 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001421}
1422
1423static int
1424append_entry(const ipt_chainlabel chain,
1425 struct ipt_entry *fw,
1426 unsigned int nsaddrs,
1427 const struct in_addr saddrs[],
1428 unsigned int ndaddrs,
1429 const struct in_addr daddrs[],
1430 int verbose,
1431 iptc_handle_t *handle)
1432{
1433 unsigned int i, j;
1434 int ret = 1;
1435
1436 for (i = 0; i < nsaddrs; i++) {
1437 fw->ip.src.s_addr = saddrs[i].s_addr;
1438 for (j = 0; j < ndaddrs; j++) {
1439 fw->ip.dst.s_addr = daddrs[j].s_addr;
1440 if (verbose)
1441 print_firewall_line(fw, *handle);
1442 ret &= iptc_append_entry(chain, fw, handle);
1443 }
1444 }
1445
1446 return ret;
1447}
1448
1449static int
1450replace_entry(const ipt_chainlabel chain,
1451 struct ipt_entry *fw,
1452 unsigned int rulenum,
1453 const struct in_addr *saddr,
1454 const struct in_addr *daddr,
1455 int verbose,
1456 iptc_handle_t *handle)
1457{
1458 fw->ip.src.s_addr = saddr->s_addr;
1459 fw->ip.dst.s_addr = daddr->s_addr;
1460
1461 if (verbose)
1462 print_firewall_line(fw, *handle);
1463 return iptc_replace_entry(chain, fw, rulenum, handle);
1464}
1465
1466static int
1467insert_entry(const ipt_chainlabel chain,
1468 struct ipt_entry *fw,
1469 unsigned int rulenum,
1470 unsigned int nsaddrs,
1471 const struct in_addr saddrs[],
1472 unsigned int ndaddrs,
1473 const struct in_addr daddrs[],
1474 int verbose,
1475 iptc_handle_t *handle)
1476{
1477 unsigned int i, j;
1478 int ret = 1;
1479
1480 for (i = 0; i < nsaddrs; i++) {
1481 fw->ip.src.s_addr = saddrs[i].s_addr;
1482 for (j = 0; j < ndaddrs; j++) {
1483 fw->ip.dst.s_addr = daddrs[j].s_addr;
1484 if (verbose)
1485 print_firewall_line(fw, *handle);
1486 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1487 }
1488 }
1489
1490 return ret;
1491}
1492
Rusty Russell2e0a3212000-04-19 11:23:18 +00001493static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001494make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001495{
1496 /* Establish mask for comparison */
1497 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001498 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001499 unsigned char *mask, *mptr;
1500
1501 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001502 for (matchp = matches; matchp; matchp = matchp->next)
1503 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001504
Rusty Russell9e1d2142000-04-23 09:11:12 +00001505 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001506 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001507 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001508
Rusty Russell9e1d2142000-04-23 09:11:12 +00001509 memset(mask, 0xFF, sizeof(struct ipt_entry));
1510 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001511
Martin Josefsson78cafda2004-02-02 20:01:18 +00001512 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001513 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001514 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001515 + matchp->match->userspacesize);
1516 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001517 }
1518
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001519 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001520 IPT_ALIGN(sizeof(struct ipt_entry_target))
1521 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001522
1523 return mask;
1524}
1525
Marc Bouchere6869a82000-03-20 06:03:29 +00001526static int
1527delete_entry(const ipt_chainlabel chain,
1528 struct ipt_entry *fw,
1529 unsigned int nsaddrs,
1530 const struct in_addr saddrs[],
1531 unsigned int ndaddrs,
1532 const struct in_addr daddrs[],
1533 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001534 iptc_handle_t *handle,
1535 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001536{
1537 unsigned int i, j;
1538 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001539 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001540
Martin Josefsson78cafda2004-02-02 20:01:18 +00001541 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001542 for (i = 0; i < nsaddrs; i++) {
1543 fw->ip.src.s_addr = saddrs[i].s_addr;
1544 for (j = 0; j < ndaddrs; j++) {
1545 fw->ip.dst.s_addr = daddrs[j].s_addr;
1546 if (verbose)
1547 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001548 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001549 }
1550 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001551 free(mask);
1552
Marc Bouchere6869a82000-03-20 06:03:29 +00001553 return ret;
1554}
1555
Harald Welteae1ff9f2000-12-01 14:26:20 +00001556int
Marc Bouchere6869a82000-03-20 06:03:29 +00001557for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001558 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001559{
1560 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001561 const char *chain;
1562 char *chains;
1563 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001564
Rusty Russell9e1d2142000-04-23 09:11:12 +00001565 chain = iptc_first_chain(handle);
1566 while (chain) {
1567 chaincount++;
1568 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001569 }
1570
Rusty Russell9e1d2142000-04-23 09:11:12 +00001571 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1572 i = 0;
1573 chain = iptc_first_chain(handle);
1574 while (chain) {
1575 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1576 i++;
1577 chain = iptc_next_chain(handle);
1578 }
1579
1580 for (i = 0; i < chaincount; i++) {
1581 if (!builtinstoo
1582 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001583 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001584 continue;
1585 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1586 }
1587
1588 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001589 return ret;
1590}
1591
Harald Welteae1ff9f2000-12-01 14:26:20 +00001592int
Marc Bouchere6869a82000-03-20 06:03:29 +00001593flush_entries(const ipt_chainlabel chain, int verbose,
1594 iptc_handle_t *handle)
1595{
1596 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001597 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001598
1599 if (verbose)
1600 fprintf(stdout, "Flushing chain `%s'\n", chain);
1601 return iptc_flush_entries(chain, handle);
1602}
Marc Bouchere6869a82000-03-20 06:03:29 +00001603
1604static int
1605zero_entries(const ipt_chainlabel chain, int verbose,
1606 iptc_handle_t *handle)
1607{
1608 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001609 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001610
Marc Bouchere6869a82000-03-20 06:03:29 +00001611 if (verbose)
1612 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1613 return iptc_zero_entries(chain, handle);
1614}
1615
Harald Welteae1ff9f2000-12-01 14:26:20 +00001616int
Marc Bouchere6869a82000-03-20 06:03:29 +00001617delete_chain(const ipt_chainlabel chain, int verbose,
1618 iptc_handle_t *handle)
1619{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001620 if (!chain)
1621 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001622
1623 if (verbose)
1624 fprintf(stdout, "Deleting chain `%s'\n", chain);
1625 return iptc_delete_chain(chain, handle);
1626}
1627
1628static int
1629list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1630 int expanded, int linenumbers, iptc_handle_t *handle)
1631{
1632 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001633 unsigned int format;
1634 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001635
1636 format = FMT_OPTIONS;
1637 if (!verbose)
1638 format |= FMT_NOCOUNTS;
1639 else
1640 format |= FMT_VIA;
1641
1642 if (numeric)
1643 format |= FMT_NUMERIC;
1644
1645 if (!expanded)
1646 format |= FMT_KILOMEGAGIGA;
1647
1648 if (linenumbers)
1649 format |= FMT_LINENUMBERS;
1650
Rusty Russell9e1d2142000-04-23 09:11:12 +00001651 for (this = iptc_first_chain(handle);
1652 this;
1653 this = iptc_next_chain(handle)) {
1654 const struct ipt_entry *i;
1655 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001656
Marc Bouchere6869a82000-03-20 06:03:29 +00001657 if (chain && strcmp(chain, this) != 0)
1658 continue;
1659
1660 if (found) printf("\n");
1661
1662 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001663 i = iptc_first_rule(this, handle);
1664
1665 num = 0;
1666 while (i) {
1667 print_firewall(i,
1668 iptc_get_target(i, handle),
1669 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001670 format,
1671 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001672 i = iptc_next_rule(i, handle);
1673 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 found = 1;
1675 }
1676
1677 errno = ENOENT;
1678 return found;
1679}
1680
Harald Welte82dd2ec2000-12-19 05:18:15 +00001681static char *get_modprobe(void)
1682{
1683 int procfile;
1684 char *ret;
1685
Harald Welte10f7f142004-10-22 08:14:07 +00001686#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001687 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1688 if (procfile < 0)
1689 return NULL;
1690
Harald Welte10f7f142004-10-22 08:14:07 +00001691 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001692 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001693 memset(ret, 0, PROCFILE_BUFSIZ);
1694 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001695 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001696 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001697 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001698 if (ret[strlen(ret)-1]=='\n')
1699 ret[strlen(ret)-1]=0;
1700 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001701 return ret;
1702 }
1703 fail:
1704 free(ret);
1705 close(procfile);
1706 return NULL;
1707}
1708
Harald Welte58918652001-06-16 18:25:25 +00001709int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001710{
1711 char *buf = NULL;
1712 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001713 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001714
1715 /* If they don't explicitly set it, read out of kernel */
1716 if (!modprobe) {
1717 buf = get_modprobe();
1718 if (!buf)
1719 return -1;
1720 modprobe = buf;
1721 }
1722
1723 switch (fork()) {
1724 case 0:
1725 argv[0] = (char *)modprobe;
1726 argv[1] = (char *)modname;
1727 argv[2] = NULL;
1728 execv(argv[0], argv);
1729
1730 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001731 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001732 case -1:
1733 return -1;
1734
1735 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001736 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001737 }
1738
1739 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001740 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1741 return 0;
1742 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001743}
1744
Marc Bouchere6869a82000-03-20 06:03:29 +00001745static struct ipt_entry *
1746generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001747 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001748 struct ipt_entry_target *target)
1749{
1750 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001751 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001752 struct ipt_entry *e;
1753
1754 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001755 for (matchp = matches; matchp; matchp = matchp->next)
1756 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001757
Rusty Russell228e98d2000-04-27 10:28:06 +00001758 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001759 *e = *fw;
1760 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001761 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
1763 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001764 for (matchp = matches; matchp; matchp = matchp->next) {
1765 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1766 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001767 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001768 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001769
1770 return e;
1771}
1772
Martin Josefsson78cafda2004-02-02 20:01:18 +00001773void clear_rule_matches(struct iptables_rule_match **matches)
1774{
1775 struct iptables_rule_match *matchp, *tmp;
1776
1777 for (matchp = *matches; matchp;) {
1778 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001779 if (matchp->match->m)
1780 free(matchp->match->m);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001781 free(matchp);
1782 matchp = tmp;
1783 }
1784
1785 *matches = NULL;
1786}
1787
Rusty Russell3aef54d2005-01-03 03:48:40 +00001788static void set_revision(char *name, u_int8_t revision)
1789{
1790 /* Old kernel sources don't have ".revision" field,
1791 but we stole a byte from name. */
1792 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1793 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1794}
1795
Marc Bouchere6869a82000-03-20 06:03:29 +00001796int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1797{
1798 struct ipt_entry fw, *e = NULL;
1799 int invert = 0;
1800 unsigned int nsaddrs = 0, ndaddrs = 0;
1801 struct in_addr *saddrs = NULL, *daddrs = NULL;
1802
1803 int c, verbose = 0;
1804 const char *chain = NULL;
1805 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1806 const char *policy = NULL, *newname = NULL;
1807 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001808 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 int ret = 1;
1810 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001811 struct iptables_rule_match *matches = NULL;
1812 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001813 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001814 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001815 const char *jumpto = "";
1816 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001817 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001818 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001819
1820 memset(&fw, 0, sizeof(fw));
1821
Harald Welteae1ff9f2000-12-01 14:26:20 +00001822 /* re-set optind to 0 in case do_command gets called
1823 * a second time */
1824 optind = 0;
1825
1826 /* clear mflags in case do_command gets called a second time
1827 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001828 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001829 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001830
1831 for (t = iptables_targets; t; t = t->next) {
1832 t->tflags = 0;
1833 t->used = 0;
1834 }
1835
Marc Bouchere6869a82000-03-20 06:03:29 +00001836 /* Suppress error messages: we may add new options if we
1837 demand-load a protocol. */
1838 opterr = 0;
1839
1840 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001841 "-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 +00001842 opts, NULL)) != -1) {
1843 switch (c) {
1844 /*
1845 * Command selection
1846 */
1847 case 'A':
1848 add_command(&command, CMD_APPEND, CMD_NONE,
1849 invert);
1850 chain = optarg;
1851 break;
1852
1853 case 'D':
1854 add_command(&command, CMD_DELETE, CMD_NONE,
1855 invert);
1856 chain = optarg;
1857 if (optind < argc && argv[optind][0] != '-'
1858 && argv[optind][0] != '!') {
1859 rulenum = parse_rulenumber(argv[optind++]);
1860 command = CMD_DELETE_NUM;
1861 }
1862 break;
1863
Marc Bouchere6869a82000-03-20 06:03:29 +00001864 case 'R':
1865 add_command(&command, CMD_REPLACE, 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 else
1872 exit_error(PARAMETER_PROBLEM,
1873 "-%c requires a rule number",
1874 cmd2char(CMD_REPLACE));
1875 break;
1876
1877 case 'I':
1878 add_command(&command, CMD_INSERT, CMD_NONE,
1879 invert);
1880 chain = optarg;
1881 if (optind < argc && argv[optind][0] != '-'
1882 && argv[optind][0] != '!')
1883 rulenum = parse_rulenumber(argv[optind++]);
1884 else rulenum = 1;
1885 break;
1886
1887 case 'L':
1888 add_command(&command, CMD_LIST, CMD_ZERO,
1889 invert);
1890 if (optarg) chain = optarg;
1891 else if (optind < argc && argv[optind][0] != '-'
1892 && argv[optind][0] != '!')
1893 chain = argv[optind++];
1894 break;
1895
1896 case 'F':
1897 add_command(&command, CMD_FLUSH, CMD_NONE,
1898 invert);
1899 if (optarg) chain = optarg;
1900 else if (optind < argc && argv[optind][0] != '-'
1901 && argv[optind][0] != '!')
1902 chain = argv[optind++];
1903 break;
1904
1905 case 'Z':
1906 add_command(&command, CMD_ZERO, CMD_LIST,
1907 invert);
1908 if (optarg) chain = optarg;
1909 else if (optind < argc && argv[optind][0] != '-'
1910 && argv[optind][0] != '!')
1911 chain = argv[optind++];
1912 break;
1913
1914 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001915 if (optarg && *optarg == '-')
1916 exit_error(PARAMETER_PROBLEM,
1917 "chain name not allowed to start "
1918 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001919 if (find_target(optarg, TRY_LOAD))
1920 exit_error(PARAMETER_PROBLEM,
1921 "chain name may not clash "
1922 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001923 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1924 invert);
1925 chain = optarg;
1926 break;
1927
1928 case 'X':
1929 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1930 invert);
1931 if (optarg) chain = optarg;
1932 else if (optind < argc && argv[optind][0] != '-'
1933 && argv[optind][0] != '!')
1934 chain = argv[optind++];
1935 break;
1936
1937 case 'E':
1938 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1939 invert);
1940 chain = optarg;
1941 if (optind < argc && argv[optind][0] != '-'
1942 && argv[optind][0] != '!')
1943 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001944 else
1945 exit_error(PARAMETER_PROBLEM,
1946 "-%c requires old-chain-name and "
1947 "new-chain-name",
1948 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001949 break;
1950
1951 case 'P':
1952 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1953 invert);
1954 chain = optarg;
1955 if (optind < argc && argv[optind][0] != '-'
1956 && argv[optind][0] != '!')
1957 policy = argv[optind++];
1958 else
1959 exit_error(PARAMETER_PROBLEM,
1960 "-%c requires a chain and a policy",
1961 cmd2char(CMD_SET_POLICY));
1962 break;
1963
1964 case 'h':
1965 if (!optarg)
1966 optarg = argv[optind];
1967
Rusty Russell2e0a3212000-04-19 11:23:18 +00001968 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001969 if (!matches && protocol)
1970 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001971
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001972 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001973
1974 /*
1975 * Option selection
1976 */
1977 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001978 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001979 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1980 invert);
1981
1982 /* Canonicalize into lower case */
1983 for (protocol = argv[optind-1]; *protocol; protocol++)
1984 *protocol = tolower(*protocol);
1985
1986 protocol = argv[optind-1];
1987 fw.ip.proto = parse_protocol(protocol);
1988
1989 if (fw.ip.proto == 0
1990 && (fw.ip.invflags & IPT_INV_PROTO))
1991 exit_error(PARAMETER_PROBLEM,
1992 "rule would never match protocol");
1993 fw.nfcache |= NFC_IP_PROTO;
1994 break;
1995
1996 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001997 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1999 invert);
2000 shostnetworkmask = argv[optind-1];
2001 fw.nfcache |= NFC_IP_SRC;
2002 break;
2003
2004 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002005 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2007 invert);
2008 dhostnetworkmask = argv[optind-1];
2009 fw.nfcache |= NFC_IP_DST;
2010 break;
2011
2012 case 'j':
2013 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2014 invert);
2015 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002016 /* TRY_LOAD (may be chain name) */
2017 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002018
2019 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002020 size_t size;
2021
Rusty Russell73f72f52000-07-03 10:17:57 +00002022 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2023 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002024
Rusty Russell2e0a3212000-04-19 11:23:18 +00002025 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002026 target->t->u.target_size = size;
2027 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002028 set_revision(target->t->u.user.name,
2029 target->revision);
Marc Bouchere6869a82000-03-20 06:03:29 +00002030 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002031 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002032 }
2033 break;
2034
2035
2036 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002037 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2039 invert);
2040 parse_interface(argv[optind-1],
2041 fw.ip.iniface,
2042 fw.ip.iniface_mask);
2043 fw.nfcache |= NFC_IP_IF_IN;
2044 break;
2045
2046 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002047 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002048 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2049 invert);
2050 parse_interface(argv[optind-1],
2051 fw.ip.outiface,
2052 fw.ip.outiface_mask);
2053 fw.nfcache |= NFC_IP_IF_OUT;
2054 break;
2055
2056 case 'f':
2057 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2058 invert);
2059 fw.ip.flags |= IPT_F_FRAG;
2060 fw.nfcache |= NFC_IP_FRAG;
2061 break;
2062
2063 case 'v':
2064 if (!verbose)
2065 set_option(&options, OPT_VERBOSE,
2066 &fw.ip.invflags, invert);
2067 verbose++;
2068 break;
2069
Rusty Russell52a51492000-05-02 16:44:29 +00002070 case 'm': {
2071 size_t size;
2072
Marc Bouchere6869a82000-03-20 06:03:29 +00002073 if (invert)
2074 exit_error(PARAMETER_PROBLEM,
2075 "unexpected ! flag before --match");
2076
Martin Josefsson78cafda2004-02-02 20:01:18 +00002077 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002078 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2079 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002080 m->m = fw_calloc(1, size);
2081 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002082 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002083 set_revision(m->m->u.user.name, m->revision);
Rusty Russell52a51492000-05-02 16:44:29 +00002084 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002085 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002086 }
2087 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002088
2089 case 'n':
2090 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2091 invert);
2092 break;
2093
2094 case 't':
2095 if (invert)
2096 exit_error(PARAMETER_PROBLEM,
2097 "unexpected ! flag before --table");
2098 *table = argv[optind-1];
2099 break;
2100
2101 case 'x':
2102 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2103 invert);
2104 break;
2105
2106 case 'V':
2107 if (invert)
2108 printf("Not %s ;-)\n", program_version);
2109 else
2110 printf("%s v%s\n",
2111 program_name, program_version);
2112 exit(0);
2113
2114 case '0':
2115 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2116 invert);
2117 break;
2118
Harald Welte82dd2ec2000-12-19 05:18:15 +00002119 case 'M':
2120 modprobe = optarg;
2121 break;
2122
Harald Welteccd49e52001-01-23 22:54:34 +00002123 case 'c':
2124
2125 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2126 invert);
2127 pcnt = optarg;
2128 if (optind < argc && argv[optind][0] != '-'
2129 && argv[optind][0] != '!')
2130 bcnt = argv[optind++];
2131 else
2132 exit_error(PARAMETER_PROBLEM,
2133 "-%c requires packet and byte counter",
2134 opt2char(OPT_COUNTERS));
2135
Martin Josefssona28d4952004-05-26 16:04:48 +00002136 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002137 exit_error(PARAMETER_PROBLEM,
2138 "-%c packet counter not numeric",
2139 opt2char(OPT_COUNTERS));
2140
Martin Josefssona28d4952004-05-26 16:04:48 +00002141 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002142 exit_error(PARAMETER_PROBLEM,
2143 "-%c byte counter not numeric",
2144 opt2char(OPT_COUNTERS));
2145
2146 break;
2147
2148
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 case 1: /* non option */
2150 if (optarg[0] == '!' && optarg[1] == '\0') {
2151 if (invert)
2152 exit_error(PARAMETER_PROBLEM,
2153 "multiple consecutive ! not"
2154 " allowed");
2155 invert = TRUE;
2156 optarg[0] = '\0';
2157 continue;
2158 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002159 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002160 exit_tryhelp(2);
2161
2162 default:
2163 /* FIXME: This scheme doesn't allow two of the same
2164 matches --RR */
2165 if (!target
2166 || !(target->parse(c - target->option_offset,
2167 argv, invert,
2168 &target->tflags,
2169 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002170 for (matchp = matches; matchp; matchp = matchp->next) {
2171 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002172 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002173 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002174 &fw,
2175 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002176 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002177 break;
2178 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002179 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002180
2181 /* If you listen carefully, you can
2182 actually hear this code suck. */
2183
2184 /* some explanations (after four different bugs
2185 * in 3 different releases): If we encountere a
2186 * parameter, that has not been parsed yet,
2187 * it's not an option of an explicitly loaded
2188 * match or a target. However, we support
2189 * implicit loading of the protocol match
2190 * extension. '-p tcp' means 'l4 proto 6' and
2191 * at the same time 'load tcp protocol match on
2192 * demand if we specify --dport'.
2193 *
2194 * To make this work, we need to make sure:
2195 * - the parameter has not been parsed by
2196 * a match (m above)
2197 * - a protocol has been specified
2198 * - the protocol extension has not been
2199 * loaded yet, or is loaded and unused
2200 * [think of iptables-restore!]
2201 * - the protocol extension can be successively
2202 * loaded
2203 */
2204 if (m == NULL
2205 && protocol
2206 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002207 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002208 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002209 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002210 && (proto_used == 0))
2211 )
2212 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002213 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002214 /* Try loading protocol */
2215 size_t size;
2216
2217 proto_used = 1;
2218
2219 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2220 + m->size;
2221
2222 m->m = fw_calloc(1, size);
2223 m->m->u.match_size = size;
2224 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002225 set_revision(m->m->u.user.name,
2226 m->revision);
Harald Welte2d86b772002-08-26 12:21:44 +00002227 m->init(m->m, &fw.nfcache);
2228
2229 opts = merge_options(opts,
2230 m->extra_opts, &m->option_offset);
2231
2232 optind--;
2233 continue;
2234 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002235 if (!m)
2236 exit_error(PARAMETER_PROBLEM,
2237 "Unknown arg `%s'",
2238 argv[optind-1]);
2239 }
2240 }
2241 invert = FALSE;
2242 }
2243
Martin Josefsson78cafda2004-02-02 20:01:18 +00002244 for (matchp = matches; matchp; matchp = matchp->next)
2245 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002246
Marc Bouchere6869a82000-03-20 06:03:29 +00002247 if (target)
2248 target->final_check(target->tflags);
2249
2250 /* Fix me: must put inverse options checking here --MN */
2251
2252 if (optind < argc)
2253 exit_error(PARAMETER_PROBLEM,
2254 "unknown arguments found on commandline");
2255 if (!command)
2256 exit_error(PARAMETER_PROBLEM, "no command specified");
2257 if (invert)
2258 exit_error(PARAMETER_PROBLEM,
2259 "nothing appropriate following !");
2260
Harald Welte6336bfd2002-05-07 14:41:43 +00002261 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002262 if (!(options & OPT_DESTINATION))
2263 dhostnetworkmask = "0.0.0.0/0";
2264 if (!(options & OPT_SOURCE))
2265 shostnetworkmask = "0.0.0.0/0";
2266 }
2267
2268 if (shostnetworkmask)
2269 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2270 &(fw.ip.smsk), &nsaddrs);
2271
2272 if (dhostnetworkmask)
2273 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2274 &(fw.ip.dmsk), &ndaddrs);
2275
2276 if ((nsaddrs > 1 || ndaddrs > 1) &&
2277 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2278 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2279 " source or destination IP addresses");
2280
Marc Bouchere6869a82000-03-20 06:03:29 +00002281 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2282 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2283 "specify a unique address");
2284
2285 generic_opt_check(command, options);
2286
2287 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2288 exit_error(PARAMETER_PROBLEM,
2289 "chain name `%s' too long (must be under %i chars)",
2290 chain, IPT_FUNCTION_MAXNAMELEN);
2291
Harald Welteae1ff9f2000-12-01 14:26:20 +00002292 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002293 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002294 *handle = iptc_init(*table);
2295
Rusty Russell8beb0492004-12-22 00:37:10 +00002296 /* try to insmod the module if iptc_init failed */
2297 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002298 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002299
Marc Bouchere6869a82000-03-20 06:03:29 +00002300 if (!*handle)
2301 exit_error(VERSION_PROBLEM,
2302 "can't initialize iptables table `%s': %s",
2303 *table, iptc_strerror(errno));
2304
Harald Welte6336bfd2002-05-07 14:41:43 +00002305 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002306 || command == CMD_DELETE
2307 || command == CMD_INSERT
2308 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002309 if (strcmp(chain, "PREROUTING") == 0
2310 || strcmp(chain, "INPUT") == 0) {
2311 /* -o not valid with incoming packets. */
2312 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002313 exit_error(PARAMETER_PROBLEM,
2314 "Can't use -%c with %s\n",
2315 opt2char(OPT_VIANAMEOUT),
2316 chain);
2317 }
2318
Rusty Russella4860fd2000-06-17 16:13:02 +00002319 if (strcmp(chain, "POSTROUTING") == 0
2320 || strcmp(chain, "OUTPUT") == 0) {
2321 /* -i not valid with outgoing packets */
2322 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002323 exit_error(PARAMETER_PROBLEM,
2324 "Can't use -%c with %s\n",
2325 opt2char(OPT_VIANAMEIN),
2326 chain);
2327 }
2328
2329 if (target && iptc_is_chain(jumpto, *handle)) {
2330 printf("Warning: using chain %s, not extension\n",
2331 jumpto);
2332
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002333 if (target->t)
2334 free(target->t);
2335
Marc Bouchere6869a82000-03-20 06:03:29 +00002336 target = NULL;
2337 }
2338
2339 /* If they didn't specify a target, or it's a chain
2340 name, use standard. */
2341 if (!target
2342 && (strlen(jumpto) == 0
2343 || iptc_is_chain(jumpto, *handle))) {
2344 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002345
Rusty Russell52a51492000-05-02 16:44:29 +00002346 target = find_target(IPT_STANDARD_TARGET,
2347 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002348
2349 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002350 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002351 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002352 target->t->u.target_size = size;
2353 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002354 set_revision(target->t->u.user.name, target->revision);
Marc Bouchere6869a82000-03-20 06:03:29 +00002355 target->init(target->t, &fw.nfcache);
2356 }
2357
Rusty Russell7e53bf92000-03-20 07:03:28 +00002358 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002359 /* it is no chain, and we can't load a plugin.
2360 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002361 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002362 * chain. */
2363 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002364 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002365 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002366 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002367 }
2368 }
2369
2370 switch (command) {
2371 case CMD_APPEND:
2372 ret = append_entry(chain, e,
2373 nsaddrs, saddrs, ndaddrs, daddrs,
2374 options&OPT_VERBOSE,
2375 handle);
2376 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002377 case CMD_DELETE:
2378 ret = delete_entry(chain, e,
2379 nsaddrs, saddrs, ndaddrs, daddrs,
2380 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002381 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002382 break;
2383 case CMD_DELETE_NUM:
2384 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2385 break;
2386 case CMD_REPLACE:
2387 ret = replace_entry(chain, e, rulenum - 1,
2388 saddrs, daddrs, options&OPT_VERBOSE,
2389 handle);
2390 break;
2391 case CMD_INSERT:
2392 ret = insert_entry(chain, e, rulenum - 1,
2393 nsaddrs, saddrs, ndaddrs, daddrs,
2394 options&OPT_VERBOSE,
2395 handle);
2396 break;
2397 case CMD_LIST:
2398 ret = list_entries(chain,
2399 options&OPT_VERBOSE,
2400 options&OPT_NUMERIC,
2401 options&OPT_EXPANDED,
2402 options&OPT_LINENUMBERS,
2403 handle);
2404 break;
2405 case CMD_FLUSH:
2406 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2407 break;
2408 case CMD_ZERO:
2409 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2410 break;
2411 case CMD_LIST|CMD_ZERO:
2412 ret = list_entries(chain,
2413 options&OPT_VERBOSE,
2414 options&OPT_NUMERIC,
2415 options&OPT_EXPANDED,
2416 options&OPT_LINENUMBERS,
2417 handle);
2418 if (ret)
2419 ret = zero_entries(chain,
2420 options&OPT_VERBOSE, handle);
2421 break;
2422 case CMD_NEW_CHAIN:
2423 ret = iptc_create_chain(chain, handle);
2424 break;
2425 case CMD_DELETE_CHAIN:
2426 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2427 break;
2428 case CMD_RENAME_CHAIN:
2429 ret = iptc_rename_chain(chain, newname, handle);
2430 break;
2431 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002432 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002433 break;
2434 default:
2435 /* We should never reach this... */
2436 exit_tryhelp(2);
2437 }
2438
2439 if (verbose > 1)
2440 dump_entries(*handle);
2441
Martin Josefsson78cafda2004-02-02 20:01:18 +00002442 clear_rule_matches(&matches);
2443
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002444 if (e != NULL) {
2445 free(e);
2446 e = NULL;
2447 }
2448
keso6997cdf2004-07-04 15:20:53 +00002449 free(saddrs);
2450 free(daddrs);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002451
2452 if (opts != original_opts) {
2453 free(opts);
2454 opts = original_opts;
2455 global_option_offset = 0;
2456 }
2457
Marc Bouchere6869a82000-03-20 06:03:29 +00002458 return ret;
2459}