blob: ea0eae361ba47026875a1fed9e63562e37db7406 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
41#include <sys/wait.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000043
44#ifndef TRUE
45#define TRUE 1
46#endif
47#ifndef FALSE
48#define FALSE 0
49#endif
50
Harald Welte82dd2ec2000-12-19 05:18:15 +000051#ifndef PROC_SYS_MODPROBE
52#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000083#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000085 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000086
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_FRAGMENT 0x00200U
100#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000101#define OPT_COUNTERS 0x00800U
102#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000103static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000104= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000105
106static struct option original_opts[] = {
107 { "append", 1, 0, 'A' },
108 { "delete", 1, 0, 'D' },
109 { "insert", 1, 0, 'I' },
110 { "replace", 1, 0, 'R' },
111 { "list", 2, 0, 'L' },
112 { "flush", 2, 0, 'F' },
113 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000116 { "rename-chain", 1, 0, 'E' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000122 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "fragments", 0, 0, 'f' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000135 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000136 { "set-counters", 1, 0, 'c' },
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000137 { "goto", 1, 0, 'g' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 { 0 }
139};
140
Illes Marci63e90632003-03-03 08:08:37 +0000141/* we need this for iptables-restore. iptables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. iptables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
146
Marc Bouchere6869a82000-03-20 06:03:29 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162 /* -n -s -d -p -j -v -x -i -o -f --line -c */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x','x'},
175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000176};
177
178static int inverse_for_options[NUMBER_OF_OPT] =
179{
180/* -n */ 0,
181/* -s */ IPT_INV_SRCIP,
182/* -d */ IPT_INV_DSTIP,
183/* -p */ IPT_INV_PROTO,
184/* -j */ 0,
185/* -v */ 0,
186/* -x */ 0,
187/* -i */ IPT_INV_VIA_IN,
188/* -o */ IPT_INV_VIA_OUT,
189/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000190/*--line*/ 0,
191/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000192};
193
194const char *program_version;
195const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000196char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000197
Phil Oester8cf65912005-09-19 15:00:33 +0000198int kernel_version;
199
Rusty Russell2e0a3212000-04-19 11:23:18 +0000200/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000201struct iptables_match *iptables_matches = NULL;
202struct iptables_target *iptables_targets = NULL;
203
204/* Extra debugging from libiptc */
205extern void dump_entries(const iptc_handle_t handle);
206
207/* A few hardcoded protocols for 'all' and in case the user has no
208 /etc/protocols */
209struct pprot {
210 char *name;
211 u_int8_t num;
212};
213
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000215/* defined in netinet/in.h */
216#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000217#ifndef IPPROTO_ESP
218#define IPPROTO_ESP 50
219#endif
220#ifndef IPPROTO_AH
221#define IPPROTO_AH 51
222#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000223#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000224
Marc Bouchere6869a82000-03-20 06:03:29 +0000225static const struct pprot chain_protos[] = {
226 { "tcp", IPPROTO_TCP },
227 { "udp", IPPROTO_UDP },
228 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000231 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000232};
233
234static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000235proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000236{
237 unsigned int i;
238
Rusty Russell28381a42000-05-10 00:19:50 +0000239 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000240 struct protoent *pent = getprotobynumber(proto);
241 if (pent)
242 return pent->p_name;
243 }
244
245 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
246 if (chain_protos[i].num == proto)
247 return chain_protos[i].name;
248
249 return NULL;
250}
251
252struct in_addr *
253dotted_to_addr(const char *dotted)
254{
255 static struct in_addr addr;
256 unsigned char *addrp;
257 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000258 unsigned int onebyte;
259 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000260 char buf[20];
261
262 /* copy dotted string, because we need to modify it */
263 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000264 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000265 addrp = (unsigned char *) &(addr.s_addr);
266
267 p = buf;
268 for (i = 0; i < 3; i++) {
269 if ((q = strchr(p, '.')) == NULL)
270 return (struct in_addr *) NULL;
271
272 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000273 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 return (struct in_addr *) NULL;
275
276 addrp[i] = (unsigned char) onebyte;
277 p = q + 1;
278 }
279
280 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000281 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 return (struct in_addr *) NULL;
283
284 addrp[3] = (unsigned char) onebyte;
285
286 return &addr;
287}
288
289static struct in_addr *
290network_to_addr(const char *name)
291{
292 struct netent *net;
293 static struct in_addr addr;
294
295 if ((net = getnetbyname(name)) != NULL) {
296 if (net->n_addrtype != AF_INET)
297 return (struct in_addr *) NULL;
298 addr.s_addr = htonl((unsigned long) net->n_net);
299 return &addr;
300 }
301
302 return (struct in_addr *) NULL;
303}
304
305static void
306inaddrcpy(struct in_addr *dst, struct in_addr *src)
307{
308 /* memcpy(dst, src, sizeof(struct in_addr)); */
309 dst->s_addr = src->s_addr;
310}
311
Pablo Neiradfdcd642005-05-29 19:05:23 +0000312static void free_opts(int reset_offset)
313{
314 if (opts != original_opts) {
315 free(opts);
316 opts = original_opts;
317 if (reset_offset)
318 global_option_offset = 0;
319 }
320}
321
Marc Bouchere6869a82000-03-20 06:03:29 +0000322void
323exit_error(enum exittype status, char *msg, ...)
324{
325 va_list args;
326
327 va_start(args, msg);
328 fprintf(stderr, "%s v%s: ", program_name, program_version);
329 vfprintf(stderr, msg, args);
330 va_end(args);
331 fprintf(stderr, "\n");
332 if (status == PARAMETER_PROBLEM)
333 exit_tryhelp(status);
334 if (status == VERSION_PROBLEM)
335 fprintf(stderr,
336 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000337 /* On error paths, make sure that we don't leak memory */
338 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 exit(status);
340}
341
342void
343exit_tryhelp(int status)
344{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000345 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000346 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
348 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000349 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000350 exit(status);
351}
352
353void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000355{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000357 struct iptables_target *t = NULL;
358
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000360"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000361" %s -[RI] chain rulenum rule-specification [options]\n"
362" %s -D chain rulenum [options]\n"
363" %s -[LFZ] [chain] [options]\n"
364" %s -[NX] chain\n"
365" %s -E old-chain-name new-chain-name\n"
366" %s -P chain target [options]\n"
367" %s -h (print this help information)\n\n",
368 program_name, program_version, program_name, program_name,
369 program_name, program_name, program_name, program_name,
370 program_name, program_name);
371
372 printf(
373"Commands:\n"
374"Either long or short options are allowed.\n"
375" --append -A chain Append to chain\n"
376" --delete -D chain Delete matching rule from chain\n"
377" --delete -D chain rulenum\n"
378" Delete rule rulenum (1 = first) from chain\n"
379" --insert -I chain [rulenum]\n"
380" Insert in chain as rulenum (default 1=first)\n"
381" --replace -R chain rulenum\n"
382" Replace rule rulenum (1 = first) in chain\n"
383" --list -L [chain] List the rules in a chain or all chains\n"
384" --flush -F [chain] Delete all rules in chain or all chains\n"
385" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000386" --new -N chain Create a new user-defined chain\n"
387" --delete-chain\n"
388" -X [chain] Delete a user-defined chain\n"
389" --policy -P chain target\n"
390" Change policy on chain to target\n"
391" --rename-chain\n"
392" -E old-chain new-chain\n"
393" Change chain name, (moving any references)\n"
394
395"Options:\n"
396" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
397" --source -s [!] address[/mask]\n"
398" source specification\n"
399" --destination -d [!] address[/mask]\n"
400" destination specification\n"
401" --in-interface -i [!] input name[+]\n"
402" network interface name ([+] for wildcard)\n"
403" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000404" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000405#ifdef IPT_F_GOTO
406" --goto -g chain\n"
407" jump to chain with no return\n"
408#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000409" --match -m match\n"
410" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000411" --numeric -n numeric output of addresses and ports\n"
412" --out-interface -o [!] output name[+]\n"
413" network interface name ([+] for wildcard)\n"
414" --table -t table table to manipulate (default: `filter')\n"
415" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000416" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000417" --exact -x expand numbers (display exact values)\n"
418"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000419" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000420" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000421"[!] --version -V print package version.\n");
422
Rusty Russell363112d2000-08-11 13:49:26 +0000423 /* Print out any special helps. A user might like to be able
424 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000425 results. So we call help for all specified matches & targets */
426 for (t = iptables_targets; t ;t = t->next) {
427 if (t->used) {
428 printf("\n");
429 t->help();
430 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000431 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000432 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000434 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000435 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000436 exit(0);
437}
438
439static void
440generic_opt_check(int command, int options)
441{
442 int i, j, legal = 0;
443
444 /* Check that commands are valid with options. Complicated by the
445 * fact that if an option is legal with *any* command given, it is
446 * legal overall (ie. -z and -l).
447 */
448 for (i = 0; i < NUMBER_OF_OPT; i++) {
449 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
450
451 for (j = 0; j < NUMBER_OF_CMD; j++) {
452 if (!(command & (1<<j)))
453 continue;
454
455 if (!(options & (1<<i))) {
456 if (commands_v_options[j][i] == '+')
457 exit_error(PARAMETER_PROBLEM,
458 "You need to supply the `-%c' "
459 "option for this command\n",
460 optflags[i]);
461 } else {
462 if (commands_v_options[j][i] != 'x')
463 legal = 1;
464 else if (legal == 0)
465 legal = -1;
466 }
467 }
468 if (legal == -1)
469 exit_error(PARAMETER_PROBLEM,
470 "Illegal option `-%c' with this command\n",
471 optflags[i]);
472 }
473}
474
475static char
476opt2char(int option)
477{
478 const char *ptr;
479 for (ptr = optflags; option > 1; option >>= 1, ptr++);
480
481 return *ptr;
482}
483
484static char
485cmd2char(int option)
486{
487 const char *ptr;
488 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
489
490 return *ptr;
491}
492
493static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000494add_command(unsigned int *cmd, const int newcmd, const int othercmds,
495 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000496{
497 if (invert)
498 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
499 if (*cmd & (~othercmds))
500 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
501 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
502 *cmd |= newcmd;
503}
504
505int
Harald Welteb77f1da2002-03-14 11:35:58 +0000506check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000507{
508 if (option && strcmp(option, "!") == 0) {
509 if (*invert)
510 exit_error(PARAMETER_PROBLEM,
511 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000512 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000513 if (optind) {
514 *optind = *optind+1;
515 if (argc && *optind > argc)
516 exit_error(PARAMETER_PROBLEM,
517 "no argument following `!'");
518 }
519
Marc Bouchere6869a82000-03-20 06:03:29 +0000520 return TRUE;
521 }
522 return FALSE;
523}
524
525static void *
526fw_calloc(size_t count, size_t size)
527{
528 void *p;
529
530 if ((p = calloc(count, size)) == NULL) {
531 perror("iptables: calloc failed");
532 exit(1);
533 }
534 return p;
535}
536
537static void *
538fw_malloc(size_t size)
539{
540 void *p;
541
542 if ((p = malloc(size)) == NULL) {
543 perror("iptables: malloc failed");
544 exit(1);
545 }
546 return p;
547}
548
549static struct in_addr *
550host_to_addr(const char *name, unsigned int *naddr)
551{
552 struct hostent *host;
553 struct in_addr *addr;
554 unsigned int i;
555
556 *naddr = 0;
557 if ((host = gethostbyname(name)) != NULL) {
558 if (host->h_addrtype != AF_INET ||
559 host->h_length != sizeof(struct in_addr))
560 return (struct in_addr *) NULL;
561
562 while (host->h_addr_list[*naddr] != (char *) NULL)
563 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000564 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000565 for (i = 0; i < *naddr; i++)
566 inaddrcpy(&(addr[i]),
567 (struct in_addr *) host->h_addr_list[i]);
568 return addr;
569 }
570
571 return (struct in_addr *) NULL;
572}
573
574static char *
575addr_to_host(const struct in_addr *addr)
576{
577 struct hostent *host;
578
579 if ((host = gethostbyaddr((char *) addr,
580 sizeof(struct in_addr), AF_INET)) != NULL)
581 return (char *) host->h_name;
582
583 return (char *) NULL;
584}
585
586/*
587 * All functions starting with "parse" should succeed, otherwise
588 * the program fails.
589 * Most routines return pointers to static data that may change
590 * between calls to the same or other routines with a few exceptions:
591 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
592 * return global static data.
593*/
594
595static struct in_addr *
596parse_hostnetwork(const char *name, unsigned int *naddrs)
597{
598 struct in_addr *addrp, *addrptmp;
599
600 if ((addrptmp = dotted_to_addr(name)) != NULL ||
601 (addrptmp = network_to_addr(name)) != NULL) {
602 addrp = fw_malloc(sizeof(struct in_addr));
603 inaddrcpy(addrp, addrptmp);
604 *naddrs = 1;
605 return addrp;
606 }
607 if ((addrp = host_to_addr(name, naddrs)) != NULL)
608 return addrp;
609
610 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
611}
612
613static struct in_addr *
614parse_mask(char *mask)
615{
616 static struct in_addr maskaddr;
617 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000618 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000619
620 if (mask == NULL) {
621 /* no mask at all defaults to 32 bits */
622 maskaddr.s_addr = 0xFFFFFFFF;
623 return &maskaddr;
624 }
625 if ((addrp = dotted_to_addr(mask)) != NULL)
626 /* dotted_to_addr already returns a network byte order addr */
627 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000628 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000629 exit_error(PARAMETER_PROBLEM,
630 "invalid mask `%s' specified", mask);
631 if (bits != 0) {
632 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
633 return &maskaddr;
634 }
635
636 maskaddr.s_addr = 0L;
637 return &maskaddr;
638}
639
Marc Boucherb93c7982001-12-06 14:50:19 +0000640void
Marc Bouchere6869a82000-03-20 06:03:29 +0000641parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
642 struct in_addr *maskp, unsigned int *naddrs)
643{
644 struct in_addr *addrp;
645 char buf[256];
646 char *p;
647 int i, j, k, n;
648
649 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000650 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000651 if ((p = strrchr(buf, '/')) != NULL) {
652 *p = '\0';
653 addrp = parse_mask(p + 1);
654 } else
655 addrp = parse_mask(NULL);
656 inaddrcpy(maskp, addrp);
657
658 /* if a null mask is given, the name is ignored, like in "any/0" */
659 if (maskp->s_addr == 0L)
660 strcpy(buf, "0.0.0.0");
661
662 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
663 n = *naddrs;
664 for (i = 0, j = 0; i < n; i++) {
665 addrp[j++].s_addr &= maskp->s_addr;
666 for (k = 0; k < j - 1; k++) {
667 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
668 (*naddrs)--;
669 j--;
670 break;
671 }
672 }
673 }
674}
675
676struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000677find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000678{
679 struct iptables_match *ptr;
680
681 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000682 if (strcmp(name, ptr->name) == 0) {
683 struct iptables_match *clone;
684
685 /* First match of this type: */
686 if (ptr->m == NULL)
687 break;
688
689 /* Second and subsequent clones */
690 clone = fw_malloc(sizeof(struct iptables_match));
691 memcpy(clone, ptr, sizeof(struct iptables_match));
692 clone->mflags = 0;
693 /* This is a clone: */
694 clone->next = clone;
695
696 ptr = clone;
Marc Bouchere6869a82000-03-20 06:03:29 +0000697 break;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000698 }
699 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000700
Harald Welte3efb6ea2001-08-06 18:50:21 +0000701#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000702 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000703 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000705 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000706 if (dlopen(path, RTLD_NOW)) {
707 /* Found library. If it didn't register itself,
708 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000709 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000710
Rusty Russell9e1d2142000-04-23 09:11:12 +0000711 if (!ptr)
712 exit_error(PARAMETER_PROBLEM,
713 "Couldn't load match `%s'\n",
714 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000715 } else if (tryload == LOAD_MUST_SUCCEED)
716 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000717 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000718 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000719 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000720#else
721 if (ptr && !ptr->loaded) {
722 if (tryload != DONT_LOAD)
723 ptr->loaded = 1;
724 else
725 ptr = NULL;
726 }
Marc Boucher067477b2002-03-24 15:09:31 +0000727 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
728 exit_error(PARAMETER_PROBLEM,
729 "Couldn't find match `%s'\n", name);
730 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000731#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000732
Martin Josefsson78cafda2004-02-02 20:01:18 +0000733 if (ptr && matches) {
734 struct iptables_rule_match **i;
735 struct iptables_rule_match *newentry;
736
737 newentry = fw_malloc(sizeof(struct iptables_rule_match));
738
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000739 for (i = matches; *i; i = &(*i)->next) {
740 if (strcmp(name, (*i)->match->name) == 0)
741 (*i)->completed = 1;
742 }
Martin Josefsson78cafda2004-02-02 20:01:18 +0000743 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000744 newentry->completed = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000745 newentry->next = NULL;
746 *i = newentry;
747 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000748
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 return ptr;
750}
751
Rusty Russell28381a42000-05-10 00:19:50 +0000752/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
753static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000754find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000755{
Harald Welteed498492001-07-23 01:24:22 +0000756 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000757
Harald Welte0b0013a2002-02-18 16:15:31 +0000758 if (string_to_number(pname, 0, 255, &proto) != -1) {
759 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000760
Harald Welte0b0013a2002-02-18 16:15:31 +0000761 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000762 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000763 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000764 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000765
766 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000767}
768
Marc Boucherb93c7982001-12-06 14:50:19 +0000769u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000770parse_protocol(const char *s)
771{
Harald Welteed498492001-07-23 01:24:22 +0000772 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000773
Harald Welteed498492001-07-23 01:24:22 +0000774 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000775 struct protoent *pent;
776
Harald Weltecbe1ec72006-02-11 09:50:11 +0000777 /* first deal with the special case of 'all' to prevent
778 * people from being able to redefine 'all' in nsswitch
779 * and/or provoke expensive [not working] ldap/nis/...
780 * lookups */
781 if (!strcmp(s, "all"))
782 return 0;
783
Marc Bouchere6869a82000-03-20 06:03:29 +0000784 if ((pent = getprotobyname(s)))
785 proto = pent->p_proto;
786 else {
787 unsigned int i;
788 for (i = 0;
789 i < sizeof(chain_protos)/sizeof(struct pprot);
790 i++) {
791 if (strcmp(s, chain_protos[i].name) == 0) {
792 proto = chain_protos[i].num;
793 break;
794 }
795 }
796 if (i == sizeof(chain_protos)/sizeof(struct pprot))
797 exit_error(PARAMETER_PROBLEM,
798 "unknown protocol `%s' specified",
799 s);
800 }
801 }
802
803 return (u_int16_t)proto;
804}
805
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000806void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000807{
808 int vialen = strlen(arg);
809 unsigned int i;
810
811 memset(mask, 0, IFNAMSIZ);
812 memset(vianame, 0, IFNAMSIZ);
813
814 if (vialen + 1 > IFNAMSIZ)
815 exit_error(PARAMETER_PROBLEM,
816 "interface name `%s' must be shorter than IFNAMSIZ"
817 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000818
Marc Bouchere6869a82000-03-20 06:03:29 +0000819 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000820 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000821 memset(mask, 0, IFNAMSIZ);
822 else if (vianame[vialen - 1] == '+') {
823 memset(mask, 0xFF, vialen - 1);
824 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000825 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000826 } else {
827 /* Include nul-terminator in match */
828 memset(mask, 0xFF, vialen + 1);
829 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000830 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000831 if (!isalnum(vianame[i])
832 && vianame[i] != '_'
833 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000834 printf("Warning: wierd character in interface"
835 " `%s' (No aliases, :, ! or *).\n",
836 vianame);
837 break;
838 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000839 }
840 }
841}
842
843/* Can't be zero. */
844static int
845parse_rulenumber(const char *rule)
846{
Harald Welteed498492001-07-23 01:24:22 +0000847 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000848
Harald Welteed498492001-07-23 01:24:22 +0000849 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000850 exit_error(PARAMETER_PROBLEM,
851 "Invalid rule number `%s'", rule);
852
853 return rulenum;
854}
855
856static const char *
857parse_target(const char *targetname)
858{
859 const char *ptr;
860
861 if (strlen(targetname) < 1)
862 exit_error(PARAMETER_PROBLEM,
863 "Invalid target name (too short)");
864
865 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
866 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000867 "Invalid target name `%s' (%u chars max)",
868 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000869
870 for (ptr = targetname; *ptr; ptr++)
871 if (isspace(*ptr))
872 exit_error(PARAMETER_PROBLEM,
873 "Invalid target name `%s'", targetname);
874 return targetname;
875}
876
877static char *
878addr_to_network(const struct in_addr *addr)
879{
880 struct netent *net;
881
882 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
883 return (char *) net->n_name;
884
885 return (char *) NULL;
886}
887
888char *
889addr_to_dotted(const struct in_addr *addrp)
890{
891 static char buf[20];
892 const unsigned char *bytep;
893
894 bytep = (const unsigned char *) &(addrp->s_addr);
895 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
896 return buf;
897}
Marc Boucherb93c7982001-12-06 14:50:19 +0000898
899char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000900addr_to_anyname(const struct in_addr *addr)
901{
902 char *name;
903
904 if ((name = addr_to_host(addr)) != NULL ||
905 (name = addr_to_network(addr)) != NULL)
906 return name;
907
908 return addr_to_dotted(addr);
909}
910
Marc Boucherb93c7982001-12-06 14:50:19 +0000911char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000912mask_to_dotted(const struct in_addr *mask)
913{
914 int i;
915 static char buf[20];
916 u_int32_t maskaddr, bits;
917
918 maskaddr = ntohl(mask->s_addr);
919
920 if (maskaddr == 0xFFFFFFFFL)
921 /* we don't want to see "/32" */
922 return "";
923
924 i = 32;
925 bits = 0xFFFFFFFEL;
926 while (--i >= 0 && maskaddr != bits)
927 bits <<= 1;
928 if (i >= 0)
929 sprintf(buf, "/%d", i);
930 else
931 /* mask was not a decent combination of 1's and 0's */
932 sprintf(buf, "/%s", addr_to_dotted(mask));
933
934 return buf;
935}
936
937int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000938string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
939 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000940{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000941 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000942 char *end;
943
944 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000945 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000946 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000947 if (*end == '\0' && end != s) {
948 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000949 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000950 *ret = number;
951 return 0;
952 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000953 }
954 return -1;
955}
956
Martin Josefssonb105bc92004-05-26 15:54:49 +0000957int
958string_to_number_l(const char *s, unsigned long min, unsigned long max,
959 unsigned long *ret)
960{
961 int result;
962 unsigned long long number;
963
964 result = string_to_number_ll(s, min, max, &number);
965 *ret = (unsigned long)number;
966
967 return result;
968}
969
970int string_to_number(const char *s, unsigned int min, unsigned int max,
971 unsigned int *ret)
972{
973 int result;
974 unsigned long number;
975
976 result = string_to_number_l(s, min, max, &number);
977 *ret = (unsigned int)number;
978
979 return result;
980}
981
Marc Bouchere6869a82000-03-20 06:03:29 +0000982static void
983set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
984 int invert)
985{
986 if (*options & option)
987 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
988 opt2char(option));
989 *options |= option;
990
991 if (invert) {
992 unsigned int i;
993 for (i = 0; 1 << i != option; i++);
994
995 if (!inverse_for_options[i])
996 exit_error(PARAMETER_PROBLEM,
997 "cannot have ! before -%c",
998 opt2char(option));
999 *invflg |= inverse_for_options[i];
1000 }
1001}
1002
1003struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +00001004find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +00001005{
1006 struct iptables_target *ptr;
1007
1008 /* Standard target? */
1009 if (strcmp(name, "") == 0
1010 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
1011 || strcmp(name, IPTC_LABEL_DROP) == 0
1012 || strcmp(name, IPTC_LABEL_QUEUE) == 0
1013 || strcmp(name, IPTC_LABEL_RETURN) == 0)
1014 name = "standard";
1015
1016 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
1017 if (strcmp(name, ptr->name) == 0)
1018 break;
1019 }
1020
Harald Welte3efb6ea2001-08-06 18:50:21 +00001021#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001022 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001023 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001024 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001025 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001026 if (dlopen(path, RTLD_NOW)) {
1027 /* Found library. If it didn't register itself,
1028 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001029 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001030 if (!ptr)
1031 exit_error(PARAMETER_PROBLEM,
1032 "Couldn't load target `%s'\n",
1033 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001034 } else if (tryload == LOAD_MUST_SUCCEED)
1035 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001036 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001037 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001038 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001039#else
1040 if (ptr && !ptr->loaded) {
1041 if (tryload != DONT_LOAD)
1042 ptr->loaded = 1;
1043 else
1044 ptr = NULL;
1045 }
Marc Boucher067477b2002-03-24 15:09:31 +00001046 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1047 exit_error(PARAMETER_PROBLEM,
1048 "Couldn't find target `%s'\n", name);
1049 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001050#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001051
Harald Welteae1ff9f2000-12-01 14:26:20 +00001052 if (ptr)
1053 ptr->used = 1;
1054
Marc Bouchere6869a82000-03-20 06:03:29 +00001055 return ptr;
1056}
1057
1058static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001059merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001060 unsigned int *option_offset)
1061{
1062 unsigned int num_old, num_new, i;
1063 struct option *merge;
1064
1065 for (num_old = 0; oldopts[num_old].name; num_old++);
1066 for (num_new = 0; newopts[num_new].name; num_new++);
1067
1068 global_option_offset += OPTION_OFFSET;
1069 *option_offset = global_option_offset;
1070
1071 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1072 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001073 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001074 for (i = 0; i < num_new; i++) {
1075 merge[num_old + i] = newopts[i];
1076 merge[num_old + i].val += *option_offset;
1077 }
1078 memset(merge + num_old + num_new, 0, sizeof(struct option));
1079
1080 return merge;
1081}
1082
Rusty Russell3aef54d2005-01-03 03:48:40 +00001083static int compatible_revision(const char *name, u_int8_t revision, int opt)
1084{
1085 struct ipt_get_revision rev;
1086 socklen_t s = sizeof(rev);
1087 int max_rev, sockfd;
1088
1089 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1090 if (sockfd < 0) {
1091 fprintf(stderr, "Could not open socket to kernel: %s\n",
1092 strerror(errno));
1093 exit(1);
1094 }
1095
1096 strcpy(rev.name, name);
1097 rev.revision = revision;
1098
1099 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1100 if (max_rev < 0) {
1101 /* Definitely don't support this? */
1102 if (errno == EPROTONOSUPPORT) {
1103 close(sockfd);
1104 return 0;
1105 } else if (errno == ENOPROTOOPT) {
1106 close(sockfd);
1107 /* Assume only revision 0 support (old kernel) */
1108 return (revision == 0);
1109 } else {
1110 fprintf(stderr, "getsockopt failed strangely: %s\n",
1111 strerror(errno));
1112 exit(1);
1113 }
1114 }
1115 close(sockfd);
1116 return 1;
1117}
1118
1119static int compatible_match_revision(const char *name, u_int8_t revision)
1120{
1121 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1122}
1123
1124static int compatible_target_revision(const char *name, u_int8_t revision)
1125{
1126 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1127}
1128
Marc Bouchere6869a82000-03-20 06:03:29 +00001129void
1130register_match(struct iptables_match *me)
1131{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001132 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001133
Marc Bouchere6869a82000-03-20 06:03:29 +00001134 if (strcmp(me->version, program_version) != 0) {
1135 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1136 program_name, me->name, me->version, program_version);
1137 exit(1);
1138 }
1139
Martin Josefsson93911bf2005-01-03 07:46:07 +00001140 /* Revision field stole a char from name. */
1141 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001142 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001143 program_name, me->name);
1144 exit(1);
1145 }
1146
Jones Desougif5b86e62005-12-22 03:33:50 +00001147 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001148 if (old) {
1149 if (old->revision == me->revision) {
1150 fprintf(stderr,
1151 "%s: match `%s' already registered.\n",
1152 program_name, me->name);
1153 exit(1);
1154 }
1155
1156 /* Now we have two (or more) options, check compatibility. */
1157 if (compatible_match_revision(old->name, old->revision)
1158 && old->revision > me->revision)
1159 return;
1160
1161 /* Replace if compatible. */
1162 if (!compatible_match_revision(me->name, me->revision))
1163 return;
1164
1165 /* Delete old one. */
1166 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1167 *i = old->next;
1168 }
1169
Rusty Russell73f72f52000-07-03 10:17:57 +00001170 if (me->size != IPT_ALIGN(me->size)) {
1171 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001172 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001173 exit(1);
1174 }
1175
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001176 /* Append to list. */
1177 for (i = &iptables_matches; *i; i = &(*i)->next);
1178 me->next = NULL;
1179 *i = me;
1180
Marc Bouchere6869a82000-03-20 06:03:29 +00001181 me->m = NULL;
1182 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001183}
1184
1185void
1186register_target(struct iptables_target *me)
1187{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001188 struct iptables_target *old;
1189
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 if (strcmp(me->version, program_version) != 0) {
1191 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1192 program_name, me->name, me->version, program_version);
1193 exit(1);
1194 }
1195
Martin Josefsson93911bf2005-01-03 07:46:07 +00001196 /* Revision field stole a char from name. */
1197 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001198 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001199 program_name, me->name);
1200 exit(1);
1201 }
1202
Jones Desougif5b86e62005-12-22 03:33:50 +00001203 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001204 if (old) {
1205 struct iptables_target **i;
1206
1207 if (old->revision == me->revision) {
1208 fprintf(stderr,
1209 "%s: target `%s' already registered.\n",
1210 program_name, me->name);
1211 exit(1);
1212 }
1213
Rusty Russell3aef54d2005-01-03 03:48:40 +00001214 /* Now we have two (or more) options, check compatibility. */
1215 if (compatible_target_revision(old->name, old->revision)
1216 && old->revision > me->revision)
1217 return;
1218
1219 /* Replace if compatible. */
1220 if (!compatible_target_revision(me->name, me->revision))
1221 return;
1222
1223 /* Delete old one. */
1224 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1225 *i = old->next;
1226 }
1227
Rusty Russell73f72f52000-07-03 10:17:57 +00001228 if (me->size != IPT_ALIGN(me->size)) {
1229 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001230 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001231 exit(1);
1232 }
1233
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 /* Prepend to list. */
1235 me->next = iptables_targets;
1236 iptables_targets = me;
1237 me->t = NULL;
1238 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001239}
1240
1241static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001242print_num(u_int64_t number, unsigned int format)
1243{
1244 if (format & FMT_KILOMEGAGIGA) {
1245 if (number > 99999) {
1246 number = (number + 500) / 1000;
1247 if (number > 9999) {
1248 number = (number + 500) / 1000;
1249 if (number > 9999) {
1250 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001251 if (number > 9999) {
1252 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001253 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001254 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001255 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001256 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001257 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001258 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001259 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001260 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001261 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001262 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001263 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001264}
1265
1266
1267static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001268print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1269{
1270 struct ipt_counters counters;
1271 const char *pol = iptc_get_policy(chain, &counters, handle);
1272 printf("Chain %s", chain);
1273 if (pol) {
1274 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001275 if (!(format & FMT_NOCOUNTS)) {
1276 fputc(' ', stdout);
1277 print_num(counters.pcnt, (format|FMT_NOTABLE));
1278 fputs("packets, ", stdout);
1279 print_num(counters.bcnt, (format|FMT_NOTABLE));
1280 fputs("bytes", stdout);
1281 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001282 printf(")\n");
1283 } else {
1284 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001285 if (!iptc_get_references(&refs, chain, handle))
1286 printf(" (ERROR obtaining refs)\n");
1287 else
1288 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001289 }
1290
1291 if (format & FMT_LINENUMBERS)
1292 printf(FMT("%-4s ", "%s "), "num");
1293 if (!(format & FMT_NOCOUNTS)) {
1294 if (format & FMT_KILOMEGAGIGA) {
1295 printf(FMT("%5s ","%s "), "pkts");
1296 printf(FMT("%5s ","%s "), "bytes");
1297 } else {
1298 printf(FMT("%8s ","%s "), "pkts");
1299 printf(FMT("%10s ","%s "), "bytes");
1300 }
1301 }
1302 if (!(format & FMT_NOTARGET))
1303 printf(FMT("%-9s ","%s "), "target");
1304 fputs(" prot ", stdout);
1305 if (format & FMT_OPTIONS)
1306 fputs("opt", stdout);
1307 if (format & FMT_VIA) {
1308 printf(FMT(" %-6s ","%s "), "in");
1309 printf(FMT("%-6s ","%s "), "out");
1310 }
1311 printf(FMT(" %-19s ","%s "), "source");
1312 printf(FMT(" %-19s "," %s "), "destination");
1313 printf("\n");
1314}
1315
Marc Bouchere6869a82000-03-20 06:03:29 +00001316
1317static int
1318print_match(const struct ipt_entry_match *m,
1319 const struct ipt_ip *ip,
1320 int numeric)
1321{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001322 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001323
1324 if (match) {
1325 if (match->print)
1326 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001327 else
Rusty Russellb039b022000-09-01 06:04:05 +00001328 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001330 if (m->u.user.name[0])
1331 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 }
1333 /* Don't stop iterating. */
1334 return 0;
1335}
1336
1337/* e is called `fw' here for hysterical raisins */
1338static void
1339print_firewall(const struct ipt_entry *fw,
1340 const char *targname,
1341 unsigned int num,
1342 unsigned int format,
1343 const iptc_handle_t handle)
1344{
1345 struct iptables_target *target = NULL;
1346 const struct ipt_entry_target *t;
1347 u_int8_t flags;
1348 char buf[BUFSIZ];
1349
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001351 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001352 else
Rusty Russell52a51492000-05-02 16:44:29 +00001353 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
1355 t = ipt_get_target((struct ipt_entry *)fw);
1356 flags = fw->ip.flags;
1357
1358 if (format & FMT_LINENUMBERS)
1359 printf(FMT("%-4u ", "%u "), num+1);
1360
1361 if (!(format & FMT_NOCOUNTS)) {
1362 print_num(fw->counters.pcnt, format);
1363 print_num(fw->counters.bcnt, format);
1364 }
1365
1366 if (!(format & FMT_NOTARGET))
1367 printf(FMT("%-9s ", "%s "), targname);
1368
1369 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1370 {
Rusty Russell28381a42000-05-10 00:19:50 +00001371 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001372 if (pname)
1373 printf(FMT("%-5s", "%s "), pname);
1374 else
1375 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1376 }
1377
1378 if (format & FMT_OPTIONS) {
1379 if (format & FMT_NOTABLE)
1380 fputs("opt ", stdout);
1381 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1382 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1383 fputc(' ', stdout);
1384 }
1385
1386 if (format & FMT_VIA) {
1387 char iface[IFNAMSIZ+2];
1388
1389 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1390 iface[0] = '!';
1391 iface[1] = '\0';
1392 }
1393 else iface[0] = '\0';
1394
1395 if (fw->ip.iniface[0] != '\0') {
1396 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001397 }
1398 else if (format & FMT_NUMERIC) strcat(iface, "*");
1399 else strcat(iface, "any");
1400 printf(FMT(" %-6s ","in %s "), iface);
1401
1402 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1403 iface[0] = '!';
1404 iface[1] = '\0';
1405 }
1406 else iface[0] = '\0';
1407
1408 if (fw->ip.outiface[0] != '\0') {
1409 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001410 }
1411 else if (format & FMT_NUMERIC) strcat(iface, "*");
1412 else strcat(iface, "any");
1413 printf(FMT("%-6s ","out %s "), iface);
1414 }
1415
1416 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1417 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1418 printf(FMT("%-19s ","%s "), "anywhere");
1419 else {
1420 if (format & FMT_NUMERIC)
1421 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1422 else
1423 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1424 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1425 printf(FMT("%-19s ","%s "), buf);
1426 }
1427
1428 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1429 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001430 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001431 else {
1432 if (format & FMT_NUMERIC)
1433 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1434 else
1435 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1436 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001437 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001438 }
1439
1440 if (format & FMT_NOTABLE)
1441 fputs(" ", stdout);
1442
Harald Welte72bd87e2005-11-24 17:04:05 +00001443#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001444 if(fw->ip.flags & IPT_F_GOTO)
1445 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001446#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001447
Marc Bouchere6869a82000-03-20 06:03:29 +00001448 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1449
1450 if (target) {
1451 if (target->print)
1452 /* Print the target information. */
1453 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001454 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001455 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001456 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001457
1458 if (!(format & FMT_NONEWLINE))
1459 fputc('\n', stdout);
1460}
1461
1462static void
1463print_firewall_line(const struct ipt_entry *fw,
1464 const iptc_handle_t h)
1465{
1466 struct ipt_entry_target *t;
1467
1468 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001469 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001470}
1471
1472static int
1473append_entry(const ipt_chainlabel chain,
1474 struct ipt_entry *fw,
1475 unsigned int nsaddrs,
1476 const struct in_addr saddrs[],
1477 unsigned int ndaddrs,
1478 const struct in_addr daddrs[],
1479 int verbose,
1480 iptc_handle_t *handle)
1481{
1482 unsigned int i, j;
1483 int ret = 1;
1484
1485 for (i = 0; i < nsaddrs; i++) {
1486 fw->ip.src.s_addr = saddrs[i].s_addr;
1487 for (j = 0; j < ndaddrs; j++) {
1488 fw->ip.dst.s_addr = daddrs[j].s_addr;
1489 if (verbose)
1490 print_firewall_line(fw, *handle);
1491 ret &= iptc_append_entry(chain, fw, handle);
1492 }
1493 }
1494
1495 return ret;
1496}
1497
1498static int
1499replace_entry(const ipt_chainlabel chain,
1500 struct ipt_entry *fw,
1501 unsigned int rulenum,
1502 const struct in_addr *saddr,
1503 const struct in_addr *daddr,
1504 int verbose,
1505 iptc_handle_t *handle)
1506{
1507 fw->ip.src.s_addr = saddr->s_addr;
1508 fw->ip.dst.s_addr = daddr->s_addr;
1509
1510 if (verbose)
1511 print_firewall_line(fw, *handle);
1512 return iptc_replace_entry(chain, fw, rulenum, handle);
1513}
1514
1515static int
1516insert_entry(const ipt_chainlabel chain,
1517 struct ipt_entry *fw,
1518 unsigned int rulenum,
1519 unsigned int nsaddrs,
1520 const struct in_addr saddrs[],
1521 unsigned int ndaddrs,
1522 const struct in_addr daddrs[],
1523 int verbose,
1524 iptc_handle_t *handle)
1525{
1526 unsigned int i, j;
1527 int ret = 1;
1528
1529 for (i = 0; i < nsaddrs; i++) {
1530 fw->ip.src.s_addr = saddrs[i].s_addr;
1531 for (j = 0; j < ndaddrs; j++) {
1532 fw->ip.dst.s_addr = daddrs[j].s_addr;
1533 if (verbose)
1534 print_firewall_line(fw, *handle);
1535 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1536 }
1537 }
1538
1539 return ret;
1540}
1541
Rusty Russell2e0a3212000-04-19 11:23:18 +00001542static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001543make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001544{
1545 /* Establish mask for comparison */
1546 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001547 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001548 unsigned char *mask, *mptr;
1549
1550 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001551 for (matchp = matches; matchp; matchp = matchp->next)
1552 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001553
Rusty Russell9e1d2142000-04-23 09:11:12 +00001554 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001555 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001556 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001557
Rusty Russell9e1d2142000-04-23 09:11:12 +00001558 memset(mask, 0xFF, sizeof(struct ipt_entry));
1559 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001560
Martin Josefsson78cafda2004-02-02 20:01:18 +00001561 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001562 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001563 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001564 + matchp->match->userspacesize);
1565 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001566 }
1567
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001568 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001569 IPT_ALIGN(sizeof(struct ipt_entry_target))
1570 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001571
1572 return mask;
1573}
1574
Marc Bouchere6869a82000-03-20 06:03:29 +00001575static int
1576delete_entry(const ipt_chainlabel chain,
1577 struct ipt_entry *fw,
1578 unsigned int nsaddrs,
1579 const struct in_addr saddrs[],
1580 unsigned int ndaddrs,
1581 const struct in_addr daddrs[],
1582 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001583 iptc_handle_t *handle,
1584 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001585{
1586 unsigned int i, j;
1587 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001588 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001589
Martin Josefsson78cafda2004-02-02 20:01:18 +00001590 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001591 for (i = 0; i < nsaddrs; i++) {
1592 fw->ip.src.s_addr = saddrs[i].s_addr;
1593 for (j = 0; j < ndaddrs; j++) {
1594 fw->ip.dst.s_addr = daddrs[j].s_addr;
1595 if (verbose)
1596 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001597 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001598 }
1599 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001600 free(mask);
1601
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 return ret;
1603}
1604
Harald Welteae1ff9f2000-12-01 14:26:20 +00001605int
Marc Bouchere6869a82000-03-20 06:03:29 +00001606for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001607 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001608{
1609 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001610 const char *chain;
1611 char *chains;
1612 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001613
Rusty Russell9e1d2142000-04-23 09:11:12 +00001614 chain = iptc_first_chain(handle);
1615 while (chain) {
1616 chaincount++;
1617 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001618 }
1619
Rusty Russell9e1d2142000-04-23 09:11:12 +00001620 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1621 i = 0;
1622 chain = iptc_first_chain(handle);
1623 while (chain) {
1624 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1625 i++;
1626 chain = iptc_next_chain(handle);
1627 }
1628
1629 for (i = 0; i < chaincount; i++) {
1630 if (!builtinstoo
1631 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001632 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001633 continue;
1634 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1635 }
1636
1637 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001638 return ret;
1639}
1640
Harald Welteae1ff9f2000-12-01 14:26:20 +00001641int
Marc Bouchere6869a82000-03-20 06:03:29 +00001642flush_entries(const ipt_chainlabel chain, int verbose,
1643 iptc_handle_t *handle)
1644{
1645 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001646 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001647
1648 if (verbose)
1649 fprintf(stdout, "Flushing chain `%s'\n", chain);
1650 return iptc_flush_entries(chain, handle);
1651}
Marc Bouchere6869a82000-03-20 06:03:29 +00001652
1653static int
1654zero_entries(const ipt_chainlabel chain, int verbose,
1655 iptc_handle_t *handle)
1656{
1657 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001658 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001659
Marc Bouchere6869a82000-03-20 06:03:29 +00001660 if (verbose)
1661 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1662 return iptc_zero_entries(chain, handle);
1663}
1664
Harald Welteae1ff9f2000-12-01 14:26:20 +00001665int
Marc Bouchere6869a82000-03-20 06:03:29 +00001666delete_chain(const ipt_chainlabel chain, int verbose,
1667 iptc_handle_t *handle)
1668{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001669 if (!chain)
1670 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001671
1672 if (verbose)
1673 fprintf(stdout, "Deleting chain `%s'\n", chain);
1674 return iptc_delete_chain(chain, handle);
1675}
1676
1677static int
1678list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1679 int expanded, int linenumbers, iptc_handle_t *handle)
1680{
1681 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001682 unsigned int format;
1683 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
1685 format = FMT_OPTIONS;
1686 if (!verbose)
1687 format |= FMT_NOCOUNTS;
1688 else
1689 format |= FMT_VIA;
1690
1691 if (numeric)
1692 format |= FMT_NUMERIC;
1693
1694 if (!expanded)
1695 format |= FMT_KILOMEGAGIGA;
1696
1697 if (linenumbers)
1698 format |= FMT_LINENUMBERS;
1699
Rusty Russell9e1d2142000-04-23 09:11:12 +00001700 for (this = iptc_first_chain(handle);
1701 this;
1702 this = iptc_next_chain(handle)) {
1703 const struct ipt_entry *i;
1704 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001705
Marc Bouchere6869a82000-03-20 06:03:29 +00001706 if (chain && strcmp(chain, this) != 0)
1707 continue;
1708
1709 if (found) printf("\n");
1710
1711 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001712 i = iptc_first_rule(this, handle);
1713
1714 num = 0;
1715 while (i) {
1716 print_firewall(i,
1717 iptc_get_target(i, handle),
1718 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001719 format,
1720 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001721 i = iptc_next_rule(i, handle);
1722 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001723 found = 1;
1724 }
1725
1726 errno = ENOENT;
1727 return found;
1728}
1729
Harald Welte82dd2ec2000-12-19 05:18:15 +00001730static char *get_modprobe(void)
1731{
1732 int procfile;
1733 char *ret;
1734
Harald Welte10f7f142004-10-22 08:14:07 +00001735#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001736 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1737 if (procfile < 0)
1738 return NULL;
1739
Harald Welte10f7f142004-10-22 08:14:07 +00001740 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001741 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001742 memset(ret, 0, PROCFILE_BUFSIZ);
1743 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001744 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001745 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001746 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001747 if (ret[strlen(ret)-1]=='\n')
1748 ret[strlen(ret)-1]=0;
1749 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001750 return ret;
1751 }
1752 fail:
1753 free(ret);
1754 close(procfile);
1755 return NULL;
1756}
1757
Harald Welte58918652001-06-16 18:25:25 +00001758int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001759{
1760 char *buf = NULL;
1761 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001762 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001763
1764 /* If they don't explicitly set it, read out of kernel */
1765 if (!modprobe) {
1766 buf = get_modprobe();
1767 if (!buf)
1768 return -1;
1769 modprobe = buf;
1770 }
1771
1772 switch (fork()) {
1773 case 0:
1774 argv[0] = (char *)modprobe;
1775 argv[1] = (char *)modname;
1776 argv[2] = NULL;
1777 execv(argv[0], argv);
1778
1779 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001780 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001781 case -1:
1782 return -1;
1783
1784 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001785 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001786 }
1787
1788 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001789 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1790 return 0;
1791 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001792}
1793
Marc Bouchere6869a82000-03-20 06:03:29 +00001794static struct ipt_entry *
1795generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001796 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 struct ipt_entry_target *target)
1798{
1799 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001800 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001801 struct ipt_entry *e;
1802
1803 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001804 for (matchp = matches; matchp; matchp = matchp->next)
1805 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001806
Rusty Russell228e98d2000-04-27 10:28:06 +00001807 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001808 *e = *fw;
1809 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001810 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001811
1812 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001813 for (matchp = matches; matchp; matchp = matchp->next) {
1814 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1815 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001816 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001817 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001818
1819 return e;
1820}
1821
Martin Josefsson78cafda2004-02-02 20:01:18 +00001822void clear_rule_matches(struct iptables_rule_match **matches)
1823{
1824 struct iptables_rule_match *matchp, *tmp;
1825
1826 for (matchp = *matches; matchp;) {
1827 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001828 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001829 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001830 matchp->match->m = NULL;
1831 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001832 if (matchp->match == matchp->match->next) {
1833 free(matchp->match);
1834 matchp->match = NULL;
1835 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001836 free(matchp);
1837 matchp = tmp;
1838 }
1839
1840 *matches = NULL;
1841}
1842
Rusty Russell3aef54d2005-01-03 03:48:40 +00001843static void set_revision(char *name, u_int8_t revision)
1844{
1845 /* Old kernel sources don't have ".revision" field,
1846 but we stole a byte from name. */
1847 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1848 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1849}
1850
Phil Oester8cf65912005-09-19 15:00:33 +00001851void
1852get_kernel_version(void) {
1853 static struct utsname uts;
1854 int x = 0, y = 0, z = 0;
1855
1856 if (uname(&uts) == -1) {
1857 fprintf(stderr, "Unable to retrieve kernel version.\n");
1858 free_opts(1);
1859 exit(1);
1860 }
1861
1862 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1863 kernel_version = LINUX_VERSION(x, y, z);
1864}
1865
Marc Bouchere6869a82000-03-20 06:03:29 +00001866int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1867{
1868 struct ipt_entry fw, *e = NULL;
1869 int invert = 0;
1870 unsigned int nsaddrs = 0, ndaddrs = 0;
1871 struct in_addr *saddrs = NULL, *daddrs = NULL;
1872
1873 int c, verbose = 0;
1874 const char *chain = NULL;
1875 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1876 const char *policy = NULL, *newname = NULL;
1877 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001878 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 int ret = 1;
1880 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001881 struct iptables_rule_match *matches = NULL;
1882 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001883 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001884 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 const char *jumpto = "";
1886 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001887 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001888 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001889
1890 memset(&fw, 0, sizeof(fw));
1891
Harald Welteae1ff9f2000-12-01 14:26:20 +00001892 /* re-set optind to 0 in case do_command gets called
1893 * a second time */
1894 optind = 0;
1895
1896 /* clear mflags in case do_command gets called a second time
1897 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001898 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001899 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001900
1901 for (t = iptables_targets; t; t = t->next) {
1902 t->tflags = 0;
1903 t->used = 0;
1904 }
1905
Marc Bouchere6869a82000-03-20 06:03:29 +00001906 /* Suppress error messages: we may add new options if we
1907 demand-load a protocol. */
1908 opterr = 0;
1909
1910 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001911 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 opts, NULL)) != -1) {
1913 switch (c) {
1914 /*
1915 * Command selection
1916 */
1917 case 'A':
1918 add_command(&command, CMD_APPEND, CMD_NONE,
1919 invert);
1920 chain = optarg;
1921 break;
1922
1923 case 'D':
1924 add_command(&command, CMD_DELETE, CMD_NONE,
1925 invert);
1926 chain = optarg;
1927 if (optind < argc && argv[optind][0] != '-'
1928 && argv[optind][0] != '!') {
1929 rulenum = parse_rulenumber(argv[optind++]);
1930 command = CMD_DELETE_NUM;
1931 }
1932 break;
1933
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 case 'R':
1935 add_command(&command, CMD_REPLACE, CMD_NONE,
1936 invert);
1937 chain = optarg;
1938 if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 rulenum = parse_rulenumber(argv[optind++]);
1941 else
1942 exit_error(PARAMETER_PROBLEM,
1943 "-%c requires a rule number",
1944 cmd2char(CMD_REPLACE));
1945 break;
1946
1947 case 'I':
1948 add_command(&command, CMD_INSERT, CMD_NONE,
1949 invert);
1950 chain = optarg;
1951 if (optind < argc && argv[optind][0] != '-'
1952 && argv[optind][0] != '!')
1953 rulenum = parse_rulenumber(argv[optind++]);
1954 else rulenum = 1;
1955 break;
1956
1957 case 'L':
1958 add_command(&command, CMD_LIST, CMD_ZERO,
1959 invert);
1960 if (optarg) chain = optarg;
1961 else if (optind < argc && argv[optind][0] != '-'
1962 && argv[optind][0] != '!')
1963 chain = argv[optind++];
1964 break;
1965
1966 case 'F':
1967 add_command(&command, CMD_FLUSH, CMD_NONE,
1968 invert);
1969 if (optarg) chain = optarg;
1970 else if (optind < argc && argv[optind][0] != '-'
1971 && argv[optind][0] != '!')
1972 chain = argv[optind++];
1973 break;
1974
1975 case 'Z':
1976 add_command(&command, CMD_ZERO, CMD_LIST,
1977 invert);
1978 if (optarg) chain = optarg;
1979 else if (optind < argc && argv[optind][0] != '-'
1980 && argv[optind][0] != '!')
1981 chain = argv[optind++];
1982 break;
1983
1984 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001985 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001986 exit_error(PARAMETER_PROBLEM,
1987 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001988 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001989 if (find_target(optarg, TRY_LOAD))
1990 exit_error(PARAMETER_PROBLEM,
1991 "chain name may not clash "
1992 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001993 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1994 invert);
1995 chain = optarg;
1996 break;
1997
1998 case 'X':
1999 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
2000 invert);
2001 if (optarg) chain = optarg;
2002 else if (optind < argc && argv[optind][0] != '-'
2003 && argv[optind][0] != '!')
2004 chain = argv[optind++];
2005 break;
2006
2007 case 'E':
2008 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2009 invert);
2010 chain = optarg;
2011 if (optind < argc && argv[optind][0] != '-'
2012 && argv[optind][0] != '!')
2013 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002014 else
2015 exit_error(PARAMETER_PROBLEM,
2016 "-%c requires old-chain-name and "
2017 "new-chain-name",
2018 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00002019 break;
2020
2021 case 'P':
2022 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2023 invert);
2024 chain = optarg;
2025 if (optind < argc && argv[optind][0] != '-'
2026 && argv[optind][0] != '!')
2027 policy = argv[optind++];
2028 else
2029 exit_error(PARAMETER_PROBLEM,
2030 "-%c requires a chain and a policy",
2031 cmd2char(CMD_SET_POLICY));
2032 break;
2033
2034 case 'h':
2035 if (!optarg)
2036 optarg = argv[optind];
2037
Rusty Russell2e0a3212000-04-19 11:23:18 +00002038 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002039 if (!matches && protocol)
2040 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002041
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002042 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002043
2044 /*
2045 * Option selection
2046 */
2047 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002048 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002049 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2050 invert);
2051
2052 /* Canonicalize into lower case */
2053 for (protocol = argv[optind-1]; *protocol; protocol++)
2054 *protocol = tolower(*protocol);
2055
2056 protocol = argv[optind-1];
2057 fw.ip.proto = parse_protocol(protocol);
2058
2059 if (fw.ip.proto == 0
2060 && (fw.ip.invflags & IPT_INV_PROTO))
2061 exit_error(PARAMETER_PROBLEM,
2062 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 break;
2064
2065 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002066 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2068 invert);
2069 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 break;
2071
2072 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002073 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2075 invert);
2076 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 break;
2078
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002079#ifdef IPT_F_GOTO
2080 case 'g':
2081 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2082 invert);
2083 fw.ip.flags |= IPT_F_GOTO;
2084 jumpto = parse_target(optarg);
2085 break;
2086#endif
2087
Marc Bouchere6869a82000-03-20 06:03:29 +00002088 case 'j':
2089 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2090 invert);
2091 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002092 /* TRY_LOAD (may be chain name) */
2093 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002094
2095 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002096 size_t size;
2097
Rusty Russell73f72f52000-07-03 10:17:57 +00002098 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2099 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002100
Rusty Russell2e0a3212000-04-19 11:23:18 +00002101 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002102 target->t->u.target_size = size;
2103 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002104 set_revision(target->t->u.user.name,
2105 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002106 if (target->init != NULL)
2107 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002108 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002109 }
2110 break;
2111
2112
2113 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002114 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002115 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2116 invert);
2117 parse_interface(argv[optind-1],
2118 fw.ip.iniface,
2119 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002120 break;
2121
2122 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002123 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002124 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2125 invert);
2126 parse_interface(argv[optind-1],
2127 fw.ip.outiface,
2128 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002129 break;
2130
2131 case 'f':
2132 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2133 invert);
2134 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002135 break;
2136
2137 case 'v':
2138 if (!verbose)
2139 set_option(&options, OPT_VERBOSE,
2140 &fw.ip.invflags, invert);
2141 verbose++;
2142 break;
2143
Rusty Russell52a51492000-05-02 16:44:29 +00002144 case 'm': {
2145 size_t size;
2146
Marc Bouchere6869a82000-03-20 06:03:29 +00002147 if (invert)
2148 exit_error(PARAMETER_PROBLEM,
2149 "unexpected ! flag before --match");
2150
Martin Josefsson78cafda2004-02-02 20:01:18 +00002151 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002152 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2153 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002154 m->m = fw_calloc(1, size);
2155 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002156 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002157 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002158 if (m->init != NULL)
2159 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002160 if (m != m->next)
2161 /* Merge options for non-cloned matches */
2162 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002163 }
2164 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002165
2166 case 'n':
2167 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2168 invert);
2169 break;
2170
2171 case 't':
2172 if (invert)
2173 exit_error(PARAMETER_PROBLEM,
2174 "unexpected ! flag before --table");
2175 *table = argv[optind-1];
2176 break;
2177
2178 case 'x':
2179 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2180 invert);
2181 break;
2182
2183 case 'V':
2184 if (invert)
2185 printf("Not %s ;-)\n", program_version);
2186 else
2187 printf("%s v%s\n",
2188 program_name, program_version);
2189 exit(0);
2190
2191 case '0':
2192 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2193 invert);
2194 break;
2195
Harald Welte82dd2ec2000-12-19 05:18:15 +00002196 case 'M':
2197 modprobe = optarg;
2198 break;
2199
Harald Welteccd49e52001-01-23 22:54:34 +00002200 case 'c':
2201
2202 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2203 invert);
2204 pcnt = optarg;
2205 if (optind < argc && argv[optind][0] != '-'
2206 && argv[optind][0] != '!')
2207 bcnt = argv[optind++];
2208 else
2209 exit_error(PARAMETER_PROBLEM,
2210 "-%c requires packet and byte counter",
2211 opt2char(OPT_COUNTERS));
2212
Martin Josefssona28d4952004-05-26 16:04:48 +00002213 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002214 exit_error(PARAMETER_PROBLEM,
2215 "-%c packet counter not numeric",
2216 opt2char(OPT_COUNTERS));
2217
Martin Josefssona28d4952004-05-26 16:04:48 +00002218 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002219 exit_error(PARAMETER_PROBLEM,
2220 "-%c byte counter not numeric",
2221 opt2char(OPT_COUNTERS));
2222
2223 break;
2224
2225
Marc Bouchere6869a82000-03-20 06:03:29 +00002226 case 1: /* non option */
2227 if (optarg[0] == '!' && optarg[1] == '\0') {
2228 if (invert)
2229 exit_error(PARAMETER_PROBLEM,
2230 "multiple consecutive ! not"
2231 " allowed");
2232 invert = TRUE;
2233 optarg[0] = '\0';
2234 continue;
2235 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002236 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002237 exit_tryhelp(2);
2238
2239 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00002240 if (!target
2241 || !(target->parse(c - target->option_offset,
2242 argv, invert,
2243 &target->tflags,
2244 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002245 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002246 if (matchp->completed)
2247 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00002248 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002249 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002250 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002251 &fw,
2252 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002253 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002254 break;
2255 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002256 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002257
2258 /* If you listen carefully, you can
2259 actually hear this code suck. */
2260
2261 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002262 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00002263 * parameter, that has not been parsed yet,
2264 * it's not an option of an explicitly loaded
2265 * match or a target. However, we support
2266 * implicit loading of the protocol match
2267 * extension. '-p tcp' means 'l4 proto 6' and
2268 * at the same time 'load tcp protocol match on
2269 * demand if we specify --dport'.
2270 *
2271 * To make this work, we need to make sure:
2272 * - the parameter has not been parsed by
2273 * a match (m above)
2274 * - a protocol has been specified
2275 * - the protocol extension has not been
2276 * loaded yet, or is loaded and unused
2277 * [think of iptables-restore!]
2278 * - the protocol extension can be successively
2279 * loaded
2280 */
2281 if (m == NULL
2282 && protocol
2283 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002284 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002285 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002286 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002287 && (proto_used == 0))
2288 )
2289 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002290 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002291 /* Try loading protocol */
2292 size_t size;
2293
2294 proto_used = 1;
2295
2296 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2297 + m->size;
2298
2299 m->m = fw_calloc(1, size);
2300 m->m->u.match_size = size;
2301 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002302 set_revision(m->m->u.user.name,
2303 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002304 if (m->init != NULL)
2305 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002306
2307 opts = merge_options(opts,
2308 m->extra_opts, &m->option_offset);
2309
2310 optind--;
2311 continue;
2312 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002313 if (!m)
2314 exit_error(PARAMETER_PROBLEM,
2315 "Unknown arg `%s'",
2316 argv[optind-1]);
2317 }
2318 }
2319 invert = FALSE;
2320 }
2321
Martin Josefsson78cafda2004-02-02 20:01:18 +00002322 for (matchp = matches; matchp; matchp = matchp->next)
2323 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002324
Marc Bouchere6869a82000-03-20 06:03:29 +00002325 if (target)
2326 target->final_check(target->tflags);
2327
2328 /* Fix me: must put inverse options checking here --MN */
2329
2330 if (optind < argc)
2331 exit_error(PARAMETER_PROBLEM,
2332 "unknown arguments found on commandline");
2333 if (!command)
2334 exit_error(PARAMETER_PROBLEM, "no command specified");
2335 if (invert)
2336 exit_error(PARAMETER_PROBLEM,
2337 "nothing appropriate following !");
2338
Harald Welte6336bfd2002-05-07 14:41:43 +00002339 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002340 if (!(options & OPT_DESTINATION))
2341 dhostnetworkmask = "0.0.0.0/0";
2342 if (!(options & OPT_SOURCE))
2343 shostnetworkmask = "0.0.0.0/0";
2344 }
2345
2346 if (shostnetworkmask)
2347 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2348 &(fw.ip.smsk), &nsaddrs);
2349
2350 if (dhostnetworkmask)
2351 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2352 &(fw.ip.dmsk), &ndaddrs);
2353
2354 if ((nsaddrs > 1 || ndaddrs > 1) &&
2355 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2356 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2357 " source or destination IP addresses");
2358
Marc Bouchere6869a82000-03-20 06:03:29 +00002359 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2360 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2361 "specify a unique address");
2362
2363 generic_opt_check(command, options);
2364
2365 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2366 exit_error(PARAMETER_PROBLEM,
2367 "chain name `%s' too long (must be under %i chars)",
2368 chain, IPT_FUNCTION_MAXNAMELEN);
2369
Harald Welteae1ff9f2000-12-01 14:26:20 +00002370 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002371 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002372 *handle = iptc_init(*table);
2373
Rusty Russell8beb0492004-12-22 00:37:10 +00002374 /* try to insmod the module if iptc_init failed */
2375 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002376 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002377
Marc Bouchere6869a82000-03-20 06:03:29 +00002378 if (!*handle)
2379 exit_error(VERSION_PROBLEM,
2380 "can't initialize iptables table `%s': %s",
2381 *table, iptc_strerror(errno));
2382
Harald Welte6336bfd2002-05-07 14:41:43 +00002383 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002384 || command == CMD_DELETE
2385 || command == CMD_INSERT
2386 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002387 if (strcmp(chain, "PREROUTING") == 0
2388 || strcmp(chain, "INPUT") == 0) {
2389 /* -o not valid with incoming packets. */
2390 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002391 exit_error(PARAMETER_PROBLEM,
2392 "Can't use -%c with %s\n",
2393 opt2char(OPT_VIANAMEOUT),
2394 chain);
2395 }
2396
Rusty Russella4860fd2000-06-17 16:13:02 +00002397 if (strcmp(chain, "POSTROUTING") == 0
2398 || strcmp(chain, "OUTPUT") == 0) {
2399 /* -i not valid with outgoing packets */
2400 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002401 exit_error(PARAMETER_PROBLEM,
2402 "Can't use -%c with %s\n",
2403 opt2char(OPT_VIANAMEIN),
2404 chain);
2405 }
2406
2407 if (target && iptc_is_chain(jumpto, *handle)) {
2408 printf("Warning: using chain %s, not extension\n",
2409 jumpto);
2410
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002411 if (target->t)
2412 free(target->t);
2413
Marc Bouchere6869a82000-03-20 06:03:29 +00002414 target = NULL;
2415 }
2416
2417 /* If they didn't specify a target, or it's a chain
2418 name, use standard. */
2419 if (!target
2420 && (strlen(jumpto) == 0
2421 || iptc_is_chain(jumpto, *handle))) {
2422 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002423
Rusty Russell52a51492000-05-02 16:44:29 +00002424 target = find_target(IPT_STANDARD_TARGET,
2425 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002426
2427 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002428 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002429 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002430 target->t->u.target_size = size;
2431 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002432 if (!iptc_is_chain(jumpto, *handle))
2433 set_revision(target->t->u.user.name,
2434 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002435 if (target->init != NULL)
2436 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002437 }
2438
Rusty Russell7e53bf92000-03-20 07:03:28 +00002439 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002440 /* it is no chain, and we can't load a plugin.
2441 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002442 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002443 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002444#ifdef IPT_F_GOTO
2445 if (fw.ip.flags & IPT_F_GOTO)
2446 exit_error(PARAMETER_PROBLEM,
2447 "goto '%s' is not a chain\n", jumpto);
2448#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002449 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002450 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002451 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002452 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002453 }
2454 }
2455
2456 switch (command) {
2457 case CMD_APPEND:
2458 ret = append_entry(chain, e,
2459 nsaddrs, saddrs, ndaddrs, daddrs,
2460 options&OPT_VERBOSE,
2461 handle);
2462 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002463 case CMD_DELETE:
2464 ret = delete_entry(chain, e,
2465 nsaddrs, saddrs, ndaddrs, daddrs,
2466 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002467 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002468 break;
2469 case CMD_DELETE_NUM:
2470 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2471 break;
2472 case CMD_REPLACE:
2473 ret = replace_entry(chain, e, rulenum - 1,
2474 saddrs, daddrs, options&OPT_VERBOSE,
2475 handle);
2476 break;
2477 case CMD_INSERT:
2478 ret = insert_entry(chain, e, rulenum - 1,
2479 nsaddrs, saddrs, ndaddrs, daddrs,
2480 options&OPT_VERBOSE,
2481 handle);
2482 break;
2483 case CMD_LIST:
2484 ret = list_entries(chain,
2485 options&OPT_VERBOSE,
2486 options&OPT_NUMERIC,
2487 options&OPT_EXPANDED,
2488 options&OPT_LINENUMBERS,
2489 handle);
2490 break;
2491 case CMD_FLUSH:
2492 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2493 break;
2494 case CMD_ZERO:
2495 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2496 break;
2497 case CMD_LIST|CMD_ZERO:
2498 ret = list_entries(chain,
2499 options&OPT_VERBOSE,
2500 options&OPT_NUMERIC,
2501 options&OPT_EXPANDED,
2502 options&OPT_LINENUMBERS,
2503 handle);
2504 if (ret)
2505 ret = zero_entries(chain,
2506 options&OPT_VERBOSE, handle);
2507 break;
2508 case CMD_NEW_CHAIN:
2509 ret = iptc_create_chain(chain, handle);
2510 break;
2511 case CMD_DELETE_CHAIN:
2512 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2513 break;
2514 case CMD_RENAME_CHAIN:
2515 ret = iptc_rename_chain(chain, newname, handle);
2516 break;
2517 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002518 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002519 break;
2520 default:
2521 /* We should never reach this... */
2522 exit_tryhelp(2);
2523 }
2524
2525 if (verbose > 1)
2526 dump_entries(*handle);
2527
Martin Josefsson78cafda2004-02-02 20:01:18 +00002528 clear_rule_matches(&matches);
2529
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002530 if (e != NULL) {
2531 free(e);
2532 e = NULL;
2533 }
2534
keso6997cdf2004-07-04 15:20:53 +00002535 free(saddrs);
2536 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002537 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002538
Marc Bouchere6869a82000-03-20 06:03:29 +00002539 return ret;
2540}