blob: 4cb9ae4d167268d7ae85e35bf4ea324b50b5da38 [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{
162 /* -n -s -d -p -j -v -x -i -o -f --line */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
165/*DELETE_NUM*/{'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',' '},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
176};
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,
190/*--line*/ 0
191};
192
193const char *program_version;
194const char *program_name;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000195char *lib_dir;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196
Phil Oester8cf65912005-09-19 15:00:33 +0000197int kernel_version;
198
Rusty Russell2e0a3212000-04-19 11:23:18 +0000199/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000200struct iptables_match *iptables_matches = NULL;
201struct iptables_target *iptables_targets = NULL;
202
203/* Extra debugging from libiptc */
204extern void dump_entries(const iptc_handle_t handle);
205
206/* A few hardcoded protocols for 'all' and in case the user has no
207 /etc/protocols */
208struct pprot {
209 char *name;
210 u_int8_t num;
211};
212
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000213/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000214/* defined in netinet/in.h */
215#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000216#ifndef IPPROTO_ESP
217#define IPPROTO_ESP 50
218#endif
219#ifndef IPPROTO_AH
220#define IPPROTO_AH 51
221#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000222#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000223
Marc Bouchere6869a82000-03-20 06:03:29 +0000224static const struct pprot chain_protos[] = {
225 { "tcp", IPPROTO_TCP },
226 { "udp", IPPROTO_UDP },
227 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000228 { "esp", IPPROTO_ESP },
229 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000230 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231};
232
233static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000234proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000235{
236 unsigned int i;
237
Rusty Russell28381a42000-05-10 00:19:50 +0000238 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
251struct in_addr *
252dotted_to_addr(const char *dotted)
253{
254 static struct in_addr addr;
255 unsigned char *addrp;
256 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000257 unsigned int onebyte;
258 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000259 char buf[20];
260
261 /* copy dotted string, because we need to modify it */
262 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000263 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000264 addrp = (unsigned char *) &(addr.s_addr);
265
266 p = buf;
267 for (i = 0; i < 3; i++) {
268 if ((q = strchr(p, '.')) == NULL)
269 return (struct in_addr *) NULL;
270
271 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000272 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000273 return (struct in_addr *) NULL;
274
275 addrp[i] = (unsigned char) onebyte;
276 p = q + 1;
277 }
278
279 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000280 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000281 return (struct in_addr *) NULL;
282
283 addrp[3] = (unsigned char) onebyte;
284
285 return &addr;
286}
287
288static struct in_addr *
289network_to_addr(const char *name)
290{
291 struct netent *net;
292 static struct in_addr addr;
293
294 if ((net = getnetbyname(name)) != NULL) {
295 if (net->n_addrtype != AF_INET)
296 return (struct in_addr *) NULL;
297 addr.s_addr = htonl((unsigned long) net->n_net);
298 return &addr;
299 }
300
301 return (struct in_addr *) NULL;
302}
303
304static void
305inaddrcpy(struct in_addr *dst, struct in_addr *src)
306{
307 /* memcpy(dst, src, sizeof(struct in_addr)); */
308 dst->s_addr = src->s_addr;
309}
310
Pablo Neiradfdcd642005-05-29 19:05:23 +0000311static void free_opts(int reset_offset)
312{
313 if (opts != original_opts) {
314 free(opts);
315 opts = original_opts;
316 if (reset_offset)
317 global_option_offset = 0;
318 }
319}
320
Marc Bouchere6869a82000-03-20 06:03:29 +0000321void
322exit_error(enum exittype status, char *msg, ...)
323{
324 va_list args;
325
326 va_start(args, msg);
327 fprintf(stderr, "%s v%s: ", program_name, program_version);
328 vfprintf(stderr, msg, args);
329 va_end(args);
330 fprintf(stderr, "\n");
331 if (status == PARAMETER_PROBLEM)
332 exit_tryhelp(status);
333 if (status == VERSION_PROBLEM)
334 fprintf(stderr,
335 "Perhaps iptables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000336 /* On error paths, make sure that we don't leak memory */
337 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000338 exit(status);
339}
340
341void
342exit_tryhelp(int status)
343{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000344 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000345 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000346 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
347 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000348 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000349 exit(status);
350}
351
352void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000353exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000354{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000355 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000356 struct iptables_target *t = NULL;
357
Marc Bouchere6869a82000-03-20 06:03:29 +0000358 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000359"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000360" %s -[RI] chain rulenum rule-specification [options]\n"
361" %s -D chain rulenum [options]\n"
362" %s -[LFZ] [chain] [options]\n"
363" %s -[NX] chain\n"
364" %s -E old-chain-name new-chain-name\n"
365" %s -P chain target [options]\n"
366" %s -h (print this help information)\n\n",
367 program_name, program_version, program_name, program_name,
368 program_name, program_name, program_name, program_name,
369 program_name, program_name);
370
371 printf(
372"Commands:\n"
373"Either long or short options are allowed.\n"
374" --append -A chain Append to chain\n"
375" --delete -D chain Delete matching rule from chain\n"
376" --delete -D chain rulenum\n"
377" Delete rule rulenum (1 = first) from chain\n"
378" --insert -I chain [rulenum]\n"
379" Insert in chain as rulenum (default 1=first)\n"
380" --replace -R chain rulenum\n"
381" Replace rule rulenum (1 = first) in chain\n"
382" --list -L [chain] List the rules in a chain or all chains\n"
383" --flush -F [chain] Delete all rules in chain or all chains\n"
384" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000385" --new -N chain Create a new user-defined chain\n"
386" --delete-chain\n"
387" -X [chain] Delete a user-defined chain\n"
388" --policy -P chain target\n"
389" Change policy on chain to target\n"
390" --rename-chain\n"
391" -E old-chain new-chain\n"
392" Change chain name, (moving any references)\n"
393
394"Options:\n"
395" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
396" --source -s [!] address[/mask]\n"
397" source specification\n"
398" --destination -d [!] address[/mask]\n"
399" destination specification\n"
400" --in-interface -i [!] input name[+]\n"
401" network interface name ([+] for wildcard)\n"
402" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000403" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000404#ifdef IPT_F_GOTO
405" --goto -g chain\n"
406" jump to chain with no return\n"
407#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000408" --match -m match\n"
409" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000410" --numeric -n numeric output of addresses and ports\n"
411" --out-interface -o [!] output name[+]\n"
412" network interface name ([+] for wildcard)\n"
413" --table -t table table to manipulate (default: `filter')\n"
414" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000415" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000416" --exact -x expand numbers (display exact values)\n"
417"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000418" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000419" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000420"[!] --version -V print package version.\n");
421
Rusty Russell363112d2000-08-11 13:49:26 +0000422 /* Print out any special helps. A user might like to be able
423 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000424 results. So we call help for all specified matches & targets */
425 for (t = iptables_targets; t ;t = t->next) {
426 if (t->used) {
427 printf("\n");
428 t->help();
429 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000430 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000431 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000432 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000433 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000434 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000435 exit(0);
436}
437
438static void
439generic_opt_check(int command, int options)
440{
441 int i, j, legal = 0;
442
443 /* Check that commands are valid with options. Complicated by the
444 * fact that if an option is legal with *any* command given, it is
445 * legal overall (ie. -z and -l).
446 */
447 for (i = 0; i < NUMBER_OF_OPT; i++) {
448 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
449
450 for (j = 0; j < NUMBER_OF_CMD; j++) {
451 if (!(command & (1<<j)))
452 continue;
453
454 if (!(options & (1<<i))) {
455 if (commands_v_options[j][i] == '+')
456 exit_error(PARAMETER_PROBLEM,
457 "You need to supply the `-%c' "
458 "option for this command\n",
459 optflags[i]);
460 } else {
461 if (commands_v_options[j][i] != 'x')
462 legal = 1;
463 else if (legal == 0)
464 legal = -1;
465 }
466 }
467 if (legal == -1)
468 exit_error(PARAMETER_PROBLEM,
469 "Illegal option `-%c' with this command\n",
470 optflags[i]);
471 }
472}
473
474static char
475opt2char(int option)
476{
477 const char *ptr;
478 for (ptr = optflags; option > 1; option >>= 1, ptr++);
479
480 return *ptr;
481}
482
483static char
484cmd2char(int option)
485{
486 const char *ptr;
487 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
488
489 return *ptr;
490}
491
492static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000493add_command(unsigned int *cmd, const int newcmd, const int othercmds,
494 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000495{
496 if (invert)
497 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
498 if (*cmd & (~othercmds))
499 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
500 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
501 *cmd |= newcmd;
502}
503
504int
Harald Welteb77f1da2002-03-14 11:35:58 +0000505check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000506{
507 if (option && strcmp(option, "!") == 0) {
508 if (*invert)
509 exit_error(PARAMETER_PROBLEM,
510 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000511 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000512 if (optind) {
513 *optind = *optind+1;
514 if (argc && *optind > argc)
515 exit_error(PARAMETER_PROBLEM,
516 "no argument following `!'");
517 }
518
Marc Bouchere6869a82000-03-20 06:03:29 +0000519 return TRUE;
520 }
521 return FALSE;
522}
523
524static void *
525fw_calloc(size_t count, size_t size)
526{
527 void *p;
528
529 if ((p = calloc(count, size)) == NULL) {
530 perror("iptables: calloc failed");
531 exit(1);
532 }
533 return p;
534}
535
536static void *
537fw_malloc(size_t size)
538{
539 void *p;
540
541 if ((p = malloc(size)) == NULL) {
542 perror("iptables: malloc failed");
543 exit(1);
544 }
545 return p;
546}
547
548static struct in_addr *
549host_to_addr(const char *name, unsigned int *naddr)
550{
551 struct hostent *host;
552 struct in_addr *addr;
553 unsigned int i;
554
555 *naddr = 0;
556 if ((host = gethostbyname(name)) != NULL) {
557 if (host->h_addrtype != AF_INET ||
558 host->h_length != sizeof(struct in_addr))
559 return (struct in_addr *) NULL;
560
561 while (host->h_addr_list[*naddr] != (char *) NULL)
562 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000563 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000564 for (i = 0; i < *naddr; i++)
565 inaddrcpy(&(addr[i]),
566 (struct in_addr *) host->h_addr_list[i]);
567 return addr;
568 }
569
570 return (struct in_addr *) NULL;
571}
572
573static char *
574addr_to_host(const struct in_addr *addr)
575{
576 struct hostent *host;
577
578 if ((host = gethostbyaddr((char *) addr,
579 sizeof(struct in_addr), AF_INET)) != NULL)
580 return (char *) host->h_name;
581
582 return (char *) NULL;
583}
584
585/*
586 * All functions starting with "parse" should succeed, otherwise
587 * the program fails.
588 * Most routines return pointers to static data that may change
589 * between calls to the same or other routines with a few exceptions:
590 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
591 * return global static data.
592*/
593
594static struct in_addr *
595parse_hostnetwork(const char *name, unsigned int *naddrs)
596{
597 struct in_addr *addrp, *addrptmp;
598
599 if ((addrptmp = dotted_to_addr(name)) != NULL ||
600 (addrptmp = network_to_addr(name)) != NULL) {
601 addrp = fw_malloc(sizeof(struct in_addr));
602 inaddrcpy(addrp, addrptmp);
603 *naddrs = 1;
604 return addrp;
605 }
606 if ((addrp = host_to_addr(name, naddrs)) != NULL)
607 return addrp;
608
609 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
610}
611
612static struct in_addr *
613parse_mask(char *mask)
614{
615 static struct in_addr maskaddr;
616 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000617 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000618
619 if (mask == NULL) {
620 /* no mask at all defaults to 32 bits */
621 maskaddr.s_addr = 0xFFFFFFFF;
622 return &maskaddr;
623 }
624 if ((addrp = dotted_to_addr(mask)) != NULL)
625 /* dotted_to_addr already returns a network byte order addr */
626 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000627 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000628 exit_error(PARAMETER_PROBLEM,
629 "invalid mask `%s' specified", mask);
630 if (bits != 0) {
631 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
632 return &maskaddr;
633 }
634
635 maskaddr.s_addr = 0L;
636 return &maskaddr;
637}
638
Marc Boucherb93c7982001-12-06 14:50:19 +0000639void
Marc Bouchere6869a82000-03-20 06:03:29 +0000640parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
641 struct in_addr *maskp, unsigned int *naddrs)
642{
643 struct in_addr *addrp;
644 char buf[256];
645 char *p;
646 int i, j, k, n;
647
648 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000649 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000650 if ((p = strrchr(buf, '/')) != NULL) {
651 *p = '\0';
652 addrp = parse_mask(p + 1);
653 } else
654 addrp = parse_mask(NULL);
655 inaddrcpy(maskp, addrp);
656
657 /* if a null mask is given, the name is ignored, like in "any/0" */
658 if (maskp->s_addr == 0L)
659 strcpy(buf, "0.0.0.0");
660
661 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
662 n = *naddrs;
663 for (i = 0, j = 0; i < n; i++) {
664 addrp[j++].s_addr &= maskp->s_addr;
665 for (k = 0; k < j - 1; k++) {
666 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
667 (*naddrs)--;
668 j--;
669 break;
670 }
671 }
672 }
673}
674
675struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000676find_match(const char *name, enum ipt_tryload tryload, struct iptables_rule_match **matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000677{
678 struct iptables_match *ptr;
679
680 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000681 if (strcmp(name, ptr->name) == 0) {
682 struct iptables_match *clone;
683
684 /* First match of this type: */
685 if (ptr->m == NULL)
686 break;
687
688 /* Second and subsequent clones */
689 clone = fw_malloc(sizeof(struct iptables_match));
690 memcpy(clone, ptr, sizeof(struct iptables_match));
691 clone->mflags = 0;
692 /* This is a clone: */
693 clone->next = clone;
694
695 ptr = clone;
Marc Bouchere6869a82000-03-20 06:03:29 +0000696 break;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000697 }
698 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000699
Harald Welte3efb6ea2001-08-06 18:50:21 +0000700#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000701 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000702 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000704 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000705 if (dlopen(path, RTLD_NOW)) {
706 /* Found library. If it didn't register itself,
707 maybe they specified target as match. */
Martin Josefsson78cafda2004-02-02 20:01:18 +0000708 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell52a51492000-05-02 16:44:29 +0000709
Rusty Russell9e1d2142000-04-23 09:11:12 +0000710 if (!ptr)
711 exit_error(PARAMETER_PROBLEM,
712 "Couldn't load match `%s'\n",
713 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000714 } else if (tryload == LOAD_MUST_SUCCEED)
715 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000716 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000717 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000718 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000719#else
720 if (ptr && !ptr->loaded) {
721 if (tryload != DONT_LOAD)
722 ptr->loaded = 1;
723 else
724 ptr = NULL;
725 }
Marc Boucher067477b2002-03-24 15:09:31 +0000726 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
727 exit_error(PARAMETER_PROBLEM,
728 "Couldn't find match `%s'\n", name);
729 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000730#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000731
Martin Josefsson78cafda2004-02-02 20:01:18 +0000732 if (ptr && matches) {
733 struct iptables_rule_match **i;
734 struct iptables_rule_match *newentry;
735
736 newentry = fw_malloc(sizeof(struct iptables_rule_match));
737
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000738 for (i = matches; *i; i = &(*i)->next) {
739 if (strcmp(name, (*i)->match->name) == 0)
740 (*i)->completed = 1;
741 }
Martin Josefsson78cafda2004-02-02 20:01:18 +0000742 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000743 newentry->completed = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000744 newentry->next = NULL;
745 *i = newentry;
746 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000747
Marc Bouchere6869a82000-03-20 06:03:29 +0000748 return ptr;
749}
750
Rusty Russell28381a42000-05-10 00:19:50 +0000751/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
752static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000753find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000754{
Harald Welteed498492001-07-23 01:24:22 +0000755 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000756
Harald Welte0b0013a2002-02-18 16:15:31 +0000757 if (string_to_number(pname, 0, 255, &proto) != -1) {
758 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000759
Harald Welte0b0013a2002-02-18 16:15:31 +0000760 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000761 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000762 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000763 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000764
765 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000766}
767
Marc Boucherb93c7982001-12-06 14:50:19 +0000768u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000769parse_protocol(const char *s)
770{
Harald Welteed498492001-07-23 01:24:22 +0000771 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000772
Harald Welteed498492001-07-23 01:24:22 +0000773 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000774 struct protoent *pent;
775
Harald Weltecbe1ec72006-02-11 09:50:11 +0000776 /* first deal with the special case of 'all' to prevent
777 * people from being able to redefine 'all' in nsswitch
778 * and/or provoke expensive [not working] ldap/nis/...
779 * lookups */
780 if (!strcmp(s, "all"))
781 return 0;
782
Marc Bouchere6869a82000-03-20 06:03:29 +0000783 if ((pent = getprotobyname(s)))
784 proto = pent->p_proto;
785 else {
786 unsigned int i;
787 for (i = 0;
788 i < sizeof(chain_protos)/sizeof(struct pprot);
789 i++) {
790 if (strcmp(s, chain_protos[i].name) == 0) {
791 proto = chain_protos[i].num;
792 break;
793 }
794 }
795 if (i == sizeof(chain_protos)/sizeof(struct pprot))
796 exit_error(PARAMETER_PROBLEM,
797 "unknown protocol `%s' specified",
798 s);
799 }
800 }
801
802 return (u_int16_t)proto;
803}
804
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000805void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Marc Bouchere6869a82000-03-20 06:03:29 +0000806{
807 int vialen = strlen(arg);
808 unsigned int i;
809
810 memset(mask, 0, IFNAMSIZ);
811 memset(vianame, 0, IFNAMSIZ);
812
813 if (vialen + 1 > IFNAMSIZ)
814 exit_error(PARAMETER_PROBLEM,
815 "interface name `%s' must be shorter than IFNAMSIZ"
816 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000817
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000819 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 memset(mask, 0, IFNAMSIZ);
821 else if (vianame[vialen - 1] == '+') {
822 memset(mask, 0xFF, vialen - 1);
823 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000824 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000825 } else {
826 /* Include nul-terminator in match */
827 memset(mask, 0xFF, vialen + 1);
828 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000829 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000830 if (!isalnum(vianame[i])
831 && vianame[i] != '_'
832 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000833 printf("Warning: wierd character in interface"
834 " `%s' (No aliases, :, ! or *).\n",
835 vianame);
836 break;
837 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000838 }
839 }
840}
841
842/* Can't be zero. */
843static int
844parse_rulenumber(const char *rule)
845{
Harald Welteed498492001-07-23 01:24:22 +0000846 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000847
Harald Welteed498492001-07-23 01:24:22 +0000848 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000849 exit_error(PARAMETER_PROBLEM,
850 "Invalid rule number `%s'", rule);
851
852 return rulenum;
853}
854
855static const char *
856parse_target(const char *targetname)
857{
858 const char *ptr;
859
860 if (strlen(targetname) < 1)
861 exit_error(PARAMETER_PROBLEM,
862 "Invalid target name (too short)");
863
864 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
865 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000866 "Invalid target name `%s' (%u chars max)",
867 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000868
869 for (ptr = targetname; *ptr; ptr++)
870 if (isspace(*ptr))
871 exit_error(PARAMETER_PROBLEM,
872 "Invalid target name `%s'", targetname);
873 return targetname;
874}
875
876static char *
877addr_to_network(const struct in_addr *addr)
878{
879 struct netent *net;
880
881 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
882 return (char *) net->n_name;
883
884 return (char *) NULL;
885}
886
887char *
888addr_to_dotted(const struct in_addr *addrp)
889{
890 static char buf[20];
891 const unsigned char *bytep;
892
893 bytep = (const unsigned char *) &(addrp->s_addr);
894 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
895 return buf;
896}
Marc Boucherb93c7982001-12-06 14:50:19 +0000897
898char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000899addr_to_anyname(const struct in_addr *addr)
900{
901 char *name;
902
903 if ((name = addr_to_host(addr)) != NULL ||
904 (name = addr_to_network(addr)) != NULL)
905 return name;
906
907 return addr_to_dotted(addr);
908}
909
Marc Boucherb93c7982001-12-06 14:50:19 +0000910char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000911mask_to_dotted(const struct in_addr *mask)
912{
913 int i;
914 static char buf[20];
915 u_int32_t maskaddr, bits;
916
917 maskaddr = ntohl(mask->s_addr);
918
919 if (maskaddr == 0xFFFFFFFFL)
920 /* we don't want to see "/32" */
921 return "";
922
923 i = 32;
924 bits = 0xFFFFFFFEL;
925 while (--i >= 0 && maskaddr != bits)
926 bits <<= 1;
927 if (i >= 0)
928 sprintf(buf, "/%d", i);
929 else
930 /* mask was not a decent combination of 1's and 0's */
931 sprintf(buf, "/%s", addr_to_dotted(mask));
932
933 return buf;
934}
935
936int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000937string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
938 unsigned long long *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000939{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000940 unsigned long long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000941 char *end;
942
943 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000944 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000945 number = strtoull(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000946 if (*end == '\0' && end != s) {
947 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000948 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000949 *ret = number;
950 return 0;
951 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000952 }
953 return -1;
954}
955
Martin Josefssonb105bc92004-05-26 15:54:49 +0000956int
957string_to_number_l(const char *s, unsigned long min, unsigned long max,
958 unsigned long *ret)
959{
960 int result;
961 unsigned long long number;
962
963 result = string_to_number_ll(s, min, max, &number);
964 *ret = (unsigned long)number;
965
966 return result;
967}
968
969int string_to_number(const char *s, unsigned int min, unsigned int max,
970 unsigned int *ret)
971{
972 int result;
973 unsigned long number;
974
975 result = string_to_number_l(s, min, max, &number);
976 *ret = (unsigned int)number;
977
978 return result;
979}
980
Marc Bouchere6869a82000-03-20 06:03:29 +0000981static void
982set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
983 int invert)
984{
985 if (*options & option)
986 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
987 opt2char(option));
988 *options |= option;
989
990 if (invert) {
991 unsigned int i;
992 for (i = 0; 1 << i != option; i++);
993
994 if (!inverse_for_options[i])
995 exit_error(PARAMETER_PROBLEM,
996 "cannot have ! before -%c",
997 opt2char(option));
998 *invflg |= inverse_for_options[i];
999 }
1000}
1001
1002struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +00001003find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +00001004{
1005 struct iptables_target *ptr;
1006
1007 /* Standard target? */
1008 if (strcmp(name, "") == 0
1009 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
1010 || strcmp(name, IPTC_LABEL_DROP) == 0
1011 || strcmp(name, IPTC_LABEL_QUEUE) == 0
1012 || strcmp(name, IPTC_LABEL_RETURN) == 0)
1013 name = "standard";
1014
1015 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
1016 if (strcmp(name, ptr->name) == 0)
1017 break;
1018 }
1019
Harald Welte3efb6ea2001-08-06 18:50:21 +00001020#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001021 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001022 char path[strlen(lib_dir) + sizeof("/libipt_.so")
Marc Bouchere6869a82000-03-20 06:03:29 +00001023 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001024 sprintf(path, "%s/libipt_%s.so", lib_dir, name);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001025 if (dlopen(path, RTLD_NOW)) {
1026 /* Found library. If it didn't register itself,
1027 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +00001028 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001029 if (!ptr)
1030 exit_error(PARAMETER_PROBLEM,
1031 "Couldn't load target `%s'\n",
1032 name);
Rusty Russell52a51492000-05-02 16:44:29 +00001033 } else if (tryload == LOAD_MUST_SUCCEED)
1034 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001035 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +00001036 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +00001037 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001038#else
1039 if (ptr && !ptr->loaded) {
1040 if (tryload != DONT_LOAD)
1041 ptr->loaded = 1;
1042 else
1043 ptr = NULL;
1044 }
Marc Boucher067477b2002-03-24 15:09:31 +00001045 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1046 exit_error(PARAMETER_PROBLEM,
1047 "Couldn't find target `%s'\n", name);
1048 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001049#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001050
Harald Welteae1ff9f2000-12-01 14:26:20 +00001051 if (ptr)
1052 ptr->used = 1;
1053
Marc Bouchere6869a82000-03-20 06:03:29 +00001054 return ptr;
1055}
1056
1057static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +00001058merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +00001059 unsigned int *option_offset)
1060{
1061 unsigned int num_old, num_new, i;
1062 struct option *merge;
1063
1064 for (num_old = 0; oldopts[num_old].name; num_old++);
1065 for (num_new = 0; newopts[num_new].name; num_new++);
1066
1067 global_option_offset += OPTION_OFFSET;
1068 *option_offset = global_option_offset;
1069
1070 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1071 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001072 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +00001073 for (i = 0; i < num_new; i++) {
1074 merge[num_old + i] = newopts[i];
1075 merge[num_old + i].val += *option_offset;
1076 }
1077 memset(merge + num_old + num_new, 0, sizeof(struct option));
1078
1079 return merge;
1080}
1081
Rusty Russell3aef54d2005-01-03 03:48:40 +00001082static int compatible_revision(const char *name, u_int8_t revision, int opt)
1083{
1084 struct ipt_get_revision rev;
1085 socklen_t s = sizeof(rev);
1086 int max_rev, sockfd;
1087
1088 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1089 if (sockfd < 0) {
1090 fprintf(stderr, "Could not open socket to kernel: %s\n",
1091 strerror(errno));
1092 exit(1);
1093 }
1094
1095 strcpy(rev.name, name);
1096 rev.revision = revision;
1097
1098 max_rev = getsockopt(sockfd, IPPROTO_IP, opt, &rev, &s);
1099 if (max_rev < 0) {
1100 /* Definitely don't support this? */
1101 if (errno == EPROTONOSUPPORT) {
1102 close(sockfd);
1103 return 0;
1104 } else if (errno == ENOPROTOOPT) {
1105 close(sockfd);
1106 /* Assume only revision 0 support (old kernel) */
1107 return (revision == 0);
1108 } else {
1109 fprintf(stderr, "getsockopt failed strangely: %s\n",
1110 strerror(errno));
1111 exit(1);
1112 }
1113 }
1114 close(sockfd);
1115 return 1;
1116}
1117
1118static int compatible_match_revision(const char *name, u_int8_t revision)
1119{
1120 return compatible_revision(name, revision, IPT_SO_GET_REVISION_MATCH);
1121}
1122
1123static int compatible_target_revision(const char *name, u_int8_t revision)
1124{
1125 return compatible_revision(name, revision, IPT_SO_GET_REVISION_TARGET);
1126}
1127
Marc Bouchere6869a82000-03-20 06:03:29 +00001128void
1129register_match(struct iptables_match *me)
1130{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001131 struct iptables_match **i, *old;
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001132
Marc Bouchere6869a82000-03-20 06:03:29 +00001133 if (strcmp(me->version, program_version) != 0) {
1134 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1135 program_name, me->name, me->version, program_version);
1136 exit(1);
1137 }
1138
Martin Josefsson93911bf2005-01-03 07:46:07 +00001139 /* Revision field stole a char from name. */
1140 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001141 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001142 program_name, me->name);
1143 exit(1);
1144 }
1145
Jones Desougif5b86e62005-12-22 03:33:50 +00001146 old = find_match(me->name, DURING_LOAD, NULL);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001147 if (old) {
1148 if (old->revision == me->revision) {
1149 fprintf(stderr,
1150 "%s: match `%s' already registered.\n",
1151 program_name, me->name);
1152 exit(1);
1153 }
1154
1155 /* Now we have two (or more) options, check compatibility. */
1156 if (compatible_match_revision(old->name, old->revision)
1157 && old->revision > me->revision)
1158 return;
1159
1160 /* Replace if compatible. */
1161 if (!compatible_match_revision(me->name, me->revision))
1162 return;
1163
1164 /* Delete old one. */
1165 for (i = &iptables_matches; *i!=old; i = &(*i)->next);
1166 *i = old->next;
1167 }
1168
Rusty Russell73f72f52000-07-03 10:17:57 +00001169 if (me->size != IPT_ALIGN(me->size)) {
1170 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001171 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001172 exit(1);
1173 }
1174
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001175 /* Append to list. */
1176 for (i = &iptables_matches; *i; i = &(*i)->next);
1177 me->next = NULL;
1178 *i = me;
1179
Marc Bouchere6869a82000-03-20 06:03:29 +00001180 me->m = NULL;
1181 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001182}
1183
1184void
1185register_target(struct iptables_target *me)
1186{
Rusty Russell3aef54d2005-01-03 03:48:40 +00001187 struct iptables_target *old;
1188
Marc Bouchere6869a82000-03-20 06:03:29 +00001189 if (strcmp(me->version, program_version) != 0) {
1190 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1191 program_name, me->name, me->version, program_version);
1192 exit(1);
1193 }
1194
Martin Josefsson93911bf2005-01-03 07:46:07 +00001195 /* Revision field stole a char from name. */
1196 if (strlen(me->name) >= IPT_FUNCTION_MAXNAMELEN-1) {
Rusty Russell3aef54d2005-01-03 03:48:40 +00001197 fprintf(stderr, "%s: target `%s' has invalid name\n",
Marc Bouchere6869a82000-03-20 06:03:29 +00001198 program_name, me->name);
1199 exit(1);
1200 }
1201
Jones Desougif5b86e62005-12-22 03:33:50 +00001202 old = find_target(me->name, DURING_LOAD);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001203 if (old) {
1204 struct iptables_target **i;
1205
1206 if (old->revision == me->revision) {
1207 fprintf(stderr,
1208 "%s: target `%s' already registered.\n",
1209 program_name, me->name);
1210 exit(1);
1211 }
1212
Rusty Russell3aef54d2005-01-03 03:48:40 +00001213 /* Now we have two (or more) options, check compatibility. */
1214 if (compatible_target_revision(old->name, old->revision)
1215 && old->revision > me->revision)
1216 return;
1217
1218 /* Replace if compatible. */
1219 if (!compatible_target_revision(me->name, me->revision))
1220 return;
1221
1222 /* Delete old one. */
1223 for (i = &iptables_targets; *i!=old; i = &(*i)->next);
1224 *i = old->next;
1225 }
1226
Rusty Russell73f72f52000-07-03 10:17:57 +00001227 if (me->size != IPT_ALIGN(me->size)) {
1228 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001229 program_name, me->name, (unsigned int)me->size);
Rusty Russell73f72f52000-07-03 10:17:57 +00001230 exit(1);
1231 }
1232
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 /* Prepend to list. */
1234 me->next = iptables_targets;
1235 iptables_targets = me;
1236 me->t = NULL;
1237 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001238}
1239
1240static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001241print_num(u_int64_t number, unsigned int format)
1242{
1243 if (format & FMT_KILOMEGAGIGA) {
1244 if (number > 99999) {
1245 number = (number + 500) / 1000;
1246 if (number > 9999) {
1247 number = (number + 500) / 1000;
1248 if (number > 9999) {
1249 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001250 if (number > 9999) {
1251 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001252 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +00001253 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001254 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001255 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001256 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001257 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001258 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001259 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001260 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001261 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001262 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001263}
1264
1265
1266static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001267print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1268{
1269 struct ipt_counters counters;
1270 const char *pol = iptc_get_policy(chain, &counters, handle);
1271 printf("Chain %s", chain);
1272 if (pol) {
1273 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001274 if (!(format & FMT_NOCOUNTS)) {
1275 fputc(' ', stdout);
1276 print_num(counters.pcnt, (format|FMT_NOTABLE));
1277 fputs("packets, ", stdout);
1278 print_num(counters.bcnt, (format|FMT_NOTABLE));
1279 fputs("bytes", stdout);
1280 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001281 printf(")\n");
1282 } else {
1283 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001284 if (!iptc_get_references(&refs, chain, handle))
1285 printf(" (ERROR obtaining refs)\n");
1286 else
1287 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 }
1289
1290 if (format & FMT_LINENUMBERS)
1291 printf(FMT("%-4s ", "%s "), "num");
1292 if (!(format & FMT_NOCOUNTS)) {
1293 if (format & FMT_KILOMEGAGIGA) {
1294 printf(FMT("%5s ","%s "), "pkts");
1295 printf(FMT("%5s ","%s "), "bytes");
1296 } else {
1297 printf(FMT("%8s ","%s "), "pkts");
1298 printf(FMT("%10s ","%s "), "bytes");
1299 }
1300 }
1301 if (!(format & FMT_NOTARGET))
1302 printf(FMT("%-9s ","%s "), "target");
1303 fputs(" prot ", stdout);
1304 if (format & FMT_OPTIONS)
1305 fputs("opt", stdout);
1306 if (format & FMT_VIA) {
1307 printf(FMT(" %-6s ","%s "), "in");
1308 printf(FMT("%-6s ","%s "), "out");
1309 }
1310 printf(FMT(" %-19s ","%s "), "source");
1311 printf(FMT(" %-19s "," %s "), "destination");
1312 printf("\n");
1313}
1314
Marc Bouchere6869a82000-03-20 06:03:29 +00001315
1316static int
1317print_match(const struct ipt_entry_match *m,
1318 const struct ipt_ip *ip,
1319 int numeric)
1320{
Martin Josefsson78cafda2004-02-02 20:01:18 +00001321 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +00001322
1323 if (match) {
1324 if (match->print)
1325 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001326 else
Rusty Russellb039b022000-09-01 06:04:05 +00001327 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001329 if (m->u.user.name[0])
1330 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001331 }
1332 /* Don't stop iterating. */
1333 return 0;
1334}
1335
1336/* e is called `fw' here for hysterical raisins */
1337static void
1338print_firewall(const struct ipt_entry *fw,
1339 const char *targname,
1340 unsigned int num,
1341 unsigned int format,
1342 const iptc_handle_t handle)
1343{
1344 struct iptables_target *target = NULL;
1345 const struct ipt_entry_target *t;
1346 u_int8_t flags;
1347 char buf[BUFSIZ];
1348
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001350 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001351 else
Rusty Russell52a51492000-05-02 16:44:29 +00001352 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001353
1354 t = ipt_get_target((struct ipt_entry *)fw);
1355 flags = fw->ip.flags;
1356
1357 if (format & FMT_LINENUMBERS)
1358 printf(FMT("%-4u ", "%u "), num+1);
1359
1360 if (!(format & FMT_NOCOUNTS)) {
1361 print_num(fw->counters.pcnt, format);
1362 print_num(fw->counters.bcnt, format);
1363 }
1364
1365 if (!(format & FMT_NOTARGET))
1366 printf(FMT("%-9s ", "%s "), targname);
1367
1368 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1369 {
Rusty Russell28381a42000-05-10 00:19:50 +00001370 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001371 if (pname)
1372 printf(FMT("%-5s", "%s "), pname);
1373 else
1374 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1375 }
1376
1377 if (format & FMT_OPTIONS) {
1378 if (format & FMT_NOTABLE)
1379 fputs("opt ", stdout);
1380 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1381 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1382 fputc(' ', stdout);
1383 }
1384
1385 if (format & FMT_VIA) {
1386 char iface[IFNAMSIZ+2];
1387
1388 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1389 iface[0] = '!';
1390 iface[1] = '\0';
1391 }
1392 else iface[0] = '\0';
1393
1394 if (fw->ip.iniface[0] != '\0') {
1395 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001396 }
1397 else if (format & FMT_NUMERIC) strcat(iface, "*");
1398 else strcat(iface, "any");
1399 printf(FMT(" %-6s ","in %s "), iface);
1400
1401 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1402 iface[0] = '!';
1403 iface[1] = '\0';
1404 }
1405 else iface[0] = '\0';
1406
1407 if (fw->ip.outiface[0] != '\0') {
1408 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001409 }
1410 else if (format & FMT_NUMERIC) strcat(iface, "*");
1411 else strcat(iface, "any");
1412 printf(FMT("%-6s ","out %s "), iface);
1413 }
1414
1415 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1416 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1417 printf(FMT("%-19s ","%s "), "anywhere");
1418 else {
1419 if (format & FMT_NUMERIC)
1420 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1421 else
1422 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1423 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1424 printf(FMT("%-19s ","%s "), buf);
1425 }
1426
1427 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1428 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001429 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 else {
1431 if (format & FMT_NUMERIC)
1432 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1433 else
1434 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1435 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001436 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001437 }
1438
1439 if (format & FMT_NOTABLE)
1440 fputs(" ", stdout);
1441
Harald Welte72bd87e2005-11-24 17:04:05 +00001442#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001443 if(fw->ip.flags & IPT_F_GOTO)
1444 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001445#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001446
Marc Bouchere6869a82000-03-20 06:03:29 +00001447 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1448
1449 if (target) {
1450 if (target->print)
1451 /* Print the target information. */
1452 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001453 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001454 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001455 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001456
1457 if (!(format & FMT_NONEWLINE))
1458 fputc('\n', stdout);
1459}
1460
1461static void
1462print_firewall_line(const struct ipt_entry *fw,
1463 const iptc_handle_t h)
1464{
1465 struct ipt_entry_target *t;
1466
1467 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001468 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001469}
1470
1471static int
1472append_entry(const ipt_chainlabel chain,
1473 struct ipt_entry *fw,
1474 unsigned int nsaddrs,
1475 const struct in_addr saddrs[],
1476 unsigned int ndaddrs,
1477 const struct in_addr daddrs[],
1478 int verbose,
1479 iptc_handle_t *handle)
1480{
1481 unsigned int i, j;
1482 int ret = 1;
1483
1484 for (i = 0; i < nsaddrs; i++) {
1485 fw->ip.src.s_addr = saddrs[i].s_addr;
1486 for (j = 0; j < ndaddrs; j++) {
1487 fw->ip.dst.s_addr = daddrs[j].s_addr;
1488 if (verbose)
1489 print_firewall_line(fw, *handle);
1490 ret &= iptc_append_entry(chain, fw, handle);
1491 }
1492 }
1493
1494 return ret;
1495}
1496
1497static int
1498replace_entry(const ipt_chainlabel chain,
1499 struct ipt_entry *fw,
1500 unsigned int rulenum,
1501 const struct in_addr *saddr,
1502 const struct in_addr *daddr,
1503 int verbose,
1504 iptc_handle_t *handle)
1505{
1506 fw->ip.src.s_addr = saddr->s_addr;
1507 fw->ip.dst.s_addr = daddr->s_addr;
1508
1509 if (verbose)
1510 print_firewall_line(fw, *handle);
1511 return iptc_replace_entry(chain, fw, rulenum, handle);
1512}
1513
1514static int
1515insert_entry(const ipt_chainlabel chain,
1516 struct ipt_entry *fw,
1517 unsigned int rulenum,
1518 unsigned int nsaddrs,
1519 const struct in_addr saddrs[],
1520 unsigned int ndaddrs,
1521 const struct in_addr daddrs[],
1522 int verbose,
1523 iptc_handle_t *handle)
1524{
1525 unsigned int i, j;
1526 int ret = 1;
1527
1528 for (i = 0; i < nsaddrs; i++) {
1529 fw->ip.src.s_addr = saddrs[i].s_addr;
1530 for (j = 0; j < ndaddrs; j++) {
1531 fw->ip.dst.s_addr = daddrs[j].s_addr;
1532 if (verbose)
1533 print_firewall_line(fw, *handle);
1534 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1535 }
1536 }
1537
1538 return ret;
1539}
1540
Rusty Russell2e0a3212000-04-19 11:23:18 +00001541static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001542make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001543{
1544 /* Establish mask for comparison */
1545 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001546 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001547 unsigned char *mask, *mptr;
1548
1549 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001550 for (matchp = matches; matchp; matchp = matchp->next)
1551 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001552
Rusty Russell9e1d2142000-04-23 09:11:12 +00001553 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001554 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001555 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001556
Rusty Russell9e1d2142000-04-23 09:11:12 +00001557 memset(mask, 0xFF, sizeof(struct ipt_entry));
1558 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001559
Martin Josefsson78cafda2004-02-02 20:01:18 +00001560 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001561 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001562 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001563 + matchp->match->userspacesize);
1564 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001565 }
1566
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001567 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001568 IPT_ALIGN(sizeof(struct ipt_entry_target))
1569 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001570
1571 return mask;
1572}
1573
Marc Bouchere6869a82000-03-20 06:03:29 +00001574static int
1575delete_entry(const ipt_chainlabel chain,
1576 struct ipt_entry *fw,
1577 unsigned int nsaddrs,
1578 const struct in_addr saddrs[],
1579 unsigned int ndaddrs,
1580 const struct in_addr daddrs[],
1581 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001582 iptc_handle_t *handle,
1583 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001584{
1585 unsigned int i, j;
1586 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001587 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001588
Martin Josefsson78cafda2004-02-02 20:01:18 +00001589 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001590 for (i = 0; i < nsaddrs; i++) {
1591 fw->ip.src.s_addr = saddrs[i].s_addr;
1592 for (j = 0; j < ndaddrs; j++) {
1593 fw->ip.dst.s_addr = daddrs[j].s_addr;
1594 if (verbose)
1595 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001596 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001597 }
1598 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001599 free(mask);
1600
Marc Bouchere6869a82000-03-20 06:03:29 +00001601 return ret;
1602}
1603
Harald Welteae1ff9f2000-12-01 14:26:20 +00001604int
Marc Bouchere6869a82000-03-20 06:03:29 +00001605for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001606 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001607{
1608 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001609 const char *chain;
1610 char *chains;
1611 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001612
Rusty Russell9e1d2142000-04-23 09:11:12 +00001613 chain = iptc_first_chain(handle);
1614 while (chain) {
1615 chaincount++;
1616 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001617 }
1618
Rusty Russell9e1d2142000-04-23 09:11:12 +00001619 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1620 i = 0;
1621 chain = iptc_first_chain(handle);
1622 while (chain) {
1623 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1624 i++;
1625 chain = iptc_next_chain(handle);
1626 }
1627
1628 for (i = 0; i < chaincount; i++) {
1629 if (!builtinstoo
1630 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001631 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001632 continue;
1633 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1634 }
1635
1636 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001637 return ret;
1638}
1639
Harald Welteae1ff9f2000-12-01 14:26:20 +00001640int
Marc Bouchere6869a82000-03-20 06:03:29 +00001641flush_entries(const ipt_chainlabel chain, int verbose,
1642 iptc_handle_t *handle)
1643{
1644 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001645 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001646
1647 if (verbose)
1648 fprintf(stdout, "Flushing chain `%s'\n", chain);
1649 return iptc_flush_entries(chain, handle);
1650}
Marc Bouchere6869a82000-03-20 06:03:29 +00001651
1652static int
1653zero_entries(const ipt_chainlabel chain, int verbose,
1654 iptc_handle_t *handle)
1655{
1656 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001657 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001658
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 if (verbose)
1660 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1661 return iptc_zero_entries(chain, handle);
1662}
1663
Harald Welteae1ff9f2000-12-01 14:26:20 +00001664int
Marc Bouchere6869a82000-03-20 06:03:29 +00001665delete_chain(const ipt_chainlabel chain, int verbose,
1666 iptc_handle_t *handle)
1667{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001668 if (!chain)
1669 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001670
1671 if (verbose)
1672 fprintf(stdout, "Deleting chain `%s'\n", chain);
1673 return iptc_delete_chain(chain, handle);
1674}
1675
1676static int
1677list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1678 int expanded, int linenumbers, iptc_handle_t *handle)
1679{
1680 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001681 unsigned int format;
1682 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001683
1684 format = FMT_OPTIONS;
1685 if (!verbose)
1686 format |= FMT_NOCOUNTS;
1687 else
1688 format |= FMT_VIA;
1689
1690 if (numeric)
1691 format |= FMT_NUMERIC;
1692
1693 if (!expanded)
1694 format |= FMT_KILOMEGAGIGA;
1695
1696 if (linenumbers)
1697 format |= FMT_LINENUMBERS;
1698
Rusty Russell9e1d2142000-04-23 09:11:12 +00001699 for (this = iptc_first_chain(handle);
1700 this;
1701 this = iptc_next_chain(handle)) {
1702 const struct ipt_entry *i;
1703 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001704
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 if (chain && strcmp(chain, this) != 0)
1706 continue;
1707
1708 if (found) printf("\n");
1709
1710 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001711 i = iptc_first_rule(this, handle);
1712
1713 num = 0;
1714 while (i) {
1715 print_firewall(i,
1716 iptc_get_target(i, handle),
1717 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001718 format,
1719 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001720 i = iptc_next_rule(i, handle);
1721 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001722 found = 1;
1723 }
1724
1725 errno = ENOENT;
1726 return found;
1727}
1728
Harald Welte82dd2ec2000-12-19 05:18:15 +00001729static char *get_modprobe(void)
1730{
1731 int procfile;
1732 char *ret;
1733
Harald Welte10f7f142004-10-22 08:14:07 +00001734#define PROCFILE_BUFSIZ 1024
Harald Welte82dd2ec2000-12-19 05:18:15 +00001735 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1736 if (procfile < 0)
1737 return NULL;
1738
Harald Welte10f7f142004-10-22 08:14:07 +00001739 ret = (char *) malloc(PROCFILE_BUFSIZ);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001740 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001741 memset(ret, 0, PROCFILE_BUFSIZ);
1742 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte82dd2ec2000-12-19 05:18:15 +00001743 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001744 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte82dd2ec2000-12-19 05:18:15 +00001745 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001746 if (ret[strlen(ret)-1]=='\n')
1747 ret[strlen(ret)-1]=0;
1748 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001749 return ret;
1750 }
1751 fail:
1752 free(ret);
1753 close(procfile);
1754 return NULL;
1755}
1756
Harald Welte58918652001-06-16 18:25:25 +00001757int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001758{
1759 char *buf = NULL;
1760 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001761 int status;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001762
1763 /* If they don't explicitly set it, read out of kernel */
1764 if (!modprobe) {
1765 buf = get_modprobe();
1766 if (!buf)
1767 return -1;
1768 modprobe = buf;
1769 }
1770
1771 switch (fork()) {
1772 case 0:
1773 argv[0] = (char *)modprobe;
1774 argv[1] = (char *)modname;
1775 argv[2] = NULL;
1776 execv(argv[0], argv);
1777
1778 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001779 exit(1);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001780 case -1:
1781 return -1;
1782
1783 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001784 wait(&status);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001785 }
1786
1787 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001788 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1789 return 0;
1790 return -1;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001791}
1792
Marc Bouchere6869a82000-03-20 06:03:29 +00001793static struct ipt_entry *
1794generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001795 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001796 struct ipt_entry_target *target)
1797{
1798 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001799 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001800 struct ipt_entry *e;
1801
1802 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001803 for (matchp = matches; matchp; matchp = matchp->next)
1804 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001805
Rusty Russell228e98d2000-04-27 10:28:06 +00001806 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001807 *e = *fw;
1808 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001809 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
1811 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001812 for (matchp = matches; matchp; matchp = matchp->next) {
1813 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1814 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001815 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001816 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001817
1818 return e;
1819}
1820
Martin Josefsson78cafda2004-02-02 20:01:18 +00001821void clear_rule_matches(struct iptables_rule_match **matches)
1822{
1823 struct iptables_rule_match *matchp, *tmp;
1824
1825 for (matchp = *matches; matchp;) {
1826 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001827 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001828 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001829 matchp->match->m = NULL;
1830 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001831 if (matchp->match == matchp->match->next) {
1832 free(matchp->match);
1833 matchp->match = NULL;
1834 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001835 free(matchp);
1836 matchp = tmp;
1837 }
1838
1839 *matches = NULL;
1840}
1841
Rusty Russell3aef54d2005-01-03 03:48:40 +00001842static void set_revision(char *name, u_int8_t revision)
1843{
1844 /* Old kernel sources don't have ".revision" field,
1845 but we stole a byte from name. */
1846 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1847 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1848}
1849
Phil Oester8cf65912005-09-19 15:00:33 +00001850void
1851get_kernel_version(void) {
1852 static struct utsname uts;
1853 int x = 0, y = 0, z = 0;
1854
1855 if (uname(&uts) == -1) {
1856 fprintf(stderr, "Unable to retrieve kernel version.\n");
1857 free_opts(1);
1858 exit(1);
1859 }
1860
1861 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1862 kernel_version = LINUX_VERSION(x, y, z);
1863}
1864
Marc Bouchere6869a82000-03-20 06:03:29 +00001865int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1866{
1867 struct ipt_entry fw, *e = NULL;
1868 int invert = 0;
1869 unsigned int nsaddrs = 0, ndaddrs = 0;
1870 struct in_addr *saddrs = NULL, *daddrs = NULL;
1871
1872 int c, verbose = 0;
1873 const char *chain = NULL;
1874 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1875 const char *policy = NULL, *newname = NULL;
1876 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001877 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001878 int ret = 1;
1879 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001880 struct iptables_rule_match *matches = NULL;
1881 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001882 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001883 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001884 const char *jumpto = "";
1885 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001886 const char *modprobe = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001887 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001888
1889 memset(&fw, 0, sizeof(fw));
1890
Harald Welteae1ff9f2000-12-01 14:26:20 +00001891 /* re-set optind to 0 in case do_command gets called
1892 * a second time */
1893 optind = 0;
1894
1895 /* clear mflags in case do_command gets called a second time
1896 * (we clear the global list of all matches for security)*/
Martin Josefsson78cafda2004-02-02 20:01:18 +00001897 for (m = iptables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001898 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001899
1900 for (t = iptables_targets; t; t = t->next) {
1901 t->tflags = 0;
1902 t->used = 0;
1903 }
1904
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 /* Suppress error messages: we may add new options if we
1906 demand-load a protocol. */
1907 opterr = 0;
1908
1909 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001910 "-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 +00001911 opts, NULL)) != -1) {
1912 switch (c) {
1913 /*
1914 * Command selection
1915 */
1916 case 'A':
1917 add_command(&command, CMD_APPEND, CMD_NONE,
1918 invert);
1919 chain = optarg;
1920 break;
1921
1922 case 'D':
1923 add_command(&command, CMD_DELETE, CMD_NONE,
1924 invert);
1925 chain = optarg;
1926 if (optind < argc && argv[optind][0] != '-'
1927 && argv[optind][0] != '!') {
1928 rulenum = parse_rulenumber(argv[optind++]);
1929 command = CMD_DELETE_NUM;
1930 }
1931 break;
1932
Marc Bouchere6869a82000-03-20 06:03:29 +00001933 case 'R':
1934 add_command(&command, CMD_REPLACE, CMD_NONE,
1935 invert);
1936 chain = optarg;
1937 if (optind < argc && argv[optind][0] != '-'
1938 && argv[optind][0] != '!')
1939 rulenum = parse_rulenumber(argv[optind++]);
1940 else
1941 exit_error(PARAMETER_PROBLEM,
1942 "-%c requires a rule number",
1943 cmd2char(CMD_REPLACE));
1944 break;
1945
1946 case 'I':
1947 add_command(&command, CMD_INSERT, CMD_NONE,
1948 invert);
1949 chain = optarg;
1950 if (optind < argc && argv[optind][0] != '-'
1951 && argv[optind][0] != '!')
1952 rulenum = parse_rulenumber(argv[optind++]);
1953 else rulenum = 1;
1954 break;
1955
1956 case 'L':
1957 add_command(&command, CMD_LIST, CMD_ZERO,
1958 invert);
1959 if (optarg) chain = optarg;
1960 else if (optind < argc && argv[optind][0] != '-'
1961 && argv[optind][0] != '!')
1962 chain = argv[optind++];
1963 break;
1964
1965 case 'F':
1966 add_command(&command, CMD_FLUSH, CMD_NONE,
1967 invert);
1968 if (optarg) chain = optarg;
1969 else if (optind < argc && argv[optind][0] != '-'
1970 && argv[optind][0] != '!')
1971 chain = argv[optind++];
1972 break;
1973
1974 case 'Z':
1975 add_command(&command, CMD_ZERO, CMD_LIST,
1976 invert);
1977 if (optarg) chain = optarg;
1978 else if (optind < argc && argv[optind][0] != '-'
1979 && argv[optind][0] != '!')
1980 chain = argv[optind++];
1981 break;
1982
1983 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001984 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001985 exit_error(PARAMETER_PROBLEM,
1986 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001987 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001988 if (find_target(optarg, TRY_LOAD))
1989 exit_error(PARAMETER_PROBLEM,
1990 "chain name may not clash "
1991 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001992 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1993 invert);
1994 chain = optarg;
1995 break;
1996
1997 case 'X':
1998 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1999 invert);
2000 if (optarg) chain = optarg;
2001 else if (optind < argc && argv[optind][0] != '-'
2002 && argv[optind][0] != '!')
2003 chain = argv[optind++];
2004 break;
2005
2006 case 'E':
2007 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2008 invert);
2009 chain = optarg;
2010 if (optind < argc && argv[optind][0] != '-'
2011 && argv[optind][0] != '!')
2012 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00002013 else
2014 exit_error(PARAMETER_PROBLEM,
2015 "-%c requires old-chain-name and "
2016 "new-chain-name",
2017 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00002018 break;
2019
2020 case 'P':
2021 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2022 invert);
2023 chain = optarg;
2024 if (optind < argc && argv[optind][0] != '-'
2025 && argv[optind][0] != '!')
2026 policy = argv[optind++];
2027 else
2028 exit_error(PARAMETER_PROBLEM,
2029 "-%c requires a chain and a policy",
2030 cmd2char(CMD_SET_POLICY));
2031 break;
2032
2033 case 'h':
2034 if (!optarg)
2035 optarg = argv[optind];
2036
Rusty Russell2e0a3212000-04-19 11:23:18 +00002037 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002038 if (!matches && protocol)
2039 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00002040
Martin Josefsson66aea6f2004-05-26 15:41:54 +00002041 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002042
2043 /*
2044 * Option selection
2045 */
2046 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00002047 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002048 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
2049 invert);
2050
2051 /* Canonicalize into lower case */
2052 for (protocol = argv[optind-1]; *protocol; protocol++)
2053 *protocol = tolower(*protocol);
2054
2055 protocol = argv[optind-1];
2056 fw.ip.proto = parse_protocol(protocol);
2057
2058 if (fw.ip.proto == 0
2059 && (fw.ip.invflags & IPT_INV_PROTO))
2060 exit_error(PARAMETER_PROBLEM,
2061 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00002062 break;
2063
2064 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00002065 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002066 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
2067 invert);
2068 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002069 break;
2070
2071 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00002072 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002073 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
2074 invert);
2075 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 break;
2077
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002078#ifdef IPT_F_GOTO
2079 case 'g':
2080 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2081 invert);
2082 fw.ip.flags |= IPT_F_GOTO;
2083 jumpto = parse_target(optarg);
2084 break;
2085#endif
2086
Marc Bouchere6869a82000-03-20 06:03:29 +00002087 case 'j':
2088 set_option(&options, OPT_JUMP, &fw.ip.invflags,
2089 invert);
2090 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00002091 /* TRY_LOAD (may be chain name) */
2092 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00002093
2094 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00002095 size_t size;
2096
Rusty Russell73f72f52000-07-03 10:17:57 +00002097 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
2098 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002099
Rusty Russell2e0a3212000-04-19 11:23:18 +00002100 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002101 target->t->u.target_size = size;
2102 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002103 set_revision(target->t->u.user.name,
2104 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002105 if (target->init != NULL)
2106 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00002107 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00002108 }
2109 break;
2110
2111
2112 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002113 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002114 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
2115 invert);
2116 parse_interface(argv[optind-1],
2117 fw.ip.iniface,
2118 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002119 break;
2120
2121 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002122 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00002123 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
2124 invert);
2125 parse_interface(argv[optind-1],
2126 fw.ip.outiface,
2127 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002128 break;
2129
2130 case 'f':
2131 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
2132 invert);
2133 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00002134 break;
2135
2136 case 'v':
2137 if (!verbose)
2138 set_option(&options, OPT_VERBOSE,
2139 &fw.ip.invflags, invert);
2140 verbose++;
2141 break;
2142
Rusty Russell52a51492000-05-02 16:44:29 +00002143 case 'm': {
2144 size_t size;
2145
Marc Bouchere6869a82000-03-20 06:03:29 +00002146 if (invert)
2147 exit_error(PARAMETER_PROBLEM,
2148 "unexpected ! flag before --match");
2149
Martin Josefsson78cafda2004-02-02 20:01:18 +00002150 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00002151 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2152 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00002153 m->m = fw_calloc(1, size);
2154 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002155 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002156 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002157 if (m->init != NULL)
2158 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002159 if (m != m->next)
2160 /* Merge options for non-cloned matches */
2161 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00002162 }
2163 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002164
2165 case 'n':
2166 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
2167 invert);
2168 break;
2169
2170 case 't':
2171 if (invert)
2172 exit_error(PARAMETER_PROBLEM,
2173 "unexpected ! flag before --table");
2174 *table = argv[optind-1];
2175 break;
2176
2177 case 'x':
2178 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
2179 invert);
2180 break;
2181
2182 case 'V':
2183 if (invert)
2184 printf("Not %s ;-)\n", program_version);
2185 else
2186 printf("%s v%s\n",
2187 program_name, program_version);
2188 exit(0);
2189
2190 case '0':
2191 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
2192 invert);
2193 break;
2194
Harald Welte82dd2ec2000-12-19 05:18:15 +00002195 case 'M':
2196 modprobe = optarg;
2197 break;
2198
Harald Welteccd49e52001-01-23 22:54:34 +00002199 case 'c':
2200
2201 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
2202 invert);
2203 pcnt = optarg;
2204 if (optind < argc && argv[optind][0] != '-'
2205 && argv[optind][0] != '!')
2206 bcnt = argv[optind++];
2207 else
2208 exit_error(PARAMETER_PROBLEM,
2209 "-%c requires packet and byte counter",
2210 opt2char(OPT_COUNTERS));
2211
Martin Josefssona28d4952004-05-26 16:04:48 +00002212 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002213 exit_error(PARAMETER_PROBLEM,
2214 "-%c packet counter not numeric",
2215 opt2char(OPT_COUNTERS));
2216
Martin Josefssona28d4952004-05-26 16:04:48 +00002217 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00002218 exit_error(PARAMETER_PROBLEM,
2219 "-%c byte counter not numeric",
2220 opt2char(OPT_COUNTERS));
2221
2222 break;
2223
2224
Marc Bouchere6869a82000-03-20 06:03:29 +00002225 case 1: /* non option */
2226 if (optarg[0] == '!' && optarg[1] == '\0') {
2227 if (invert)
2228 exit_error(PARAMETER_PROBLEM,
2229 "multiple consecutive ! not"
2230 " allowed");
2231 invert = TRUE;
2232 optarg[0] = '\0';
2233 continue;
2234 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002235 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002236 exit_tryhelp(2);
2237
2238 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00002239 if (!target
2240 || !(target->parse(c - target->option_offset,
2241 argv, invert,
2242 &target->tflags,
2243 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002244 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002245 if (matchp->completed)
2246 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00002247 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00002248 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002249 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00002250 &fw,
2251 &fw.nfcache,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002252 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00002253 break;
2254 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00002255 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00002256
2257 /* If you listen carefully, you can
2258 actually hear this code suck. */
2259
2260 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002261 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00002262 * parameter, that has not been parsed yet,
2263 * it's not an option of an explicitly loaded
2264 * match or a target. However, we support
2265 * implicit loading of the protocol match
2266 * extension. '-p tcp' means 'l4 proto 6' and
2267 * at the same time 'load tcp protocol match on
2268 * demand if we specify --dport'.
2269 *
2270 * To make this work, we need to make sure:
2271 * - the parameter has not been parsed by
2272 * a match (m above)
2273 * - a protocol has been specified
2274 * - the protocol extension has not been
2275 * loaded yet, or is loaded and unused
2276 * [think of iptables-restore!]
2277 * - the protocol extension can be successively
2278 * loaded
2279 */
2280 if (m == NULL
2281 && protocol
2282 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002283 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002284 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002285 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00002286 && (proto_used == 0))
2287 )
2288 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002289 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00002290 /* Try loading protocol */
2291 size_t size;
2292
2293 proto_used = 1;
2294
2295 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2296 + m->size;
2297
2298 m->m = fw_calloc(1, size);
2299 m->m->u.match_size = size;
2300 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00002301 set_revision(m->m->u.user.name,
2302 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002303 if (m->init != NULL)
2304 m->init(m->m, &fw.nfcache);
Harald Welte2d86b772002-08-26 12:21:44 +00002305
2306 opts = merge_options(opts,
2307 m->extra_opts, &m->option_offset);
2308
2309 optind--;
2310 continue;
2311 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002312 if (!m)
2313 exit_error(PARAMETER_PROBLEM,
2314 "Unknown arg `%s'",
2315 argv[optind-1]);
2316 }
2317 }
2318 invert = FALSE;
2319 }
2320
Martin Josefsson78cafda2004-02-02 20:01:18 +00002321 for (matchp = matches; matchp; matchp = matchp->next)
2322 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002323
Marc Bouchere6869a82000-03-20 06:03:29 +00002324 if (target)
2325 target->final_check(target->tflags);
2326
2327 /* Fix me: must put inverse options checking here --MN */
2328
2329 if (optind < argc)
2330 exit_error(PARAMETER_PROBLEM,
2331 "unknown arguments found on commandline");
2332 if (!command)
2333 exit_error(PARAMETER_PROBLEM, "no command specified");
2334 if (invert)
2335 exit_error(PARAMETER_PROBLEM,
2336 "nothing appropriate following !");
2337
Harald Welte6336bfd2002-05-07 14:41:43 +00002338 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002339 if (!(options & OPT_DESTINATION))
2340 dhostnetworkmask = "0.0.0.0/0";
2341 if (!(options & OPT_SOURCE))
2342 shostnetworkmask = "0.0.0.0/0";
2343 }
2344
2345 if (shostnetworkmask)
2346 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2347 &(fw.ip.smsk), &nsaddrs);
2348
2349 if (dhostnetworkmask)
2350 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2351 &(fw.ip.dmsk), &ndaddrs);
2352
2353 if ((nsaddrs > 1 || ndaddrs > 1) &&
2354 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2355 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2356 " source or destination IP addresses");
2357
Marc Bouchere6869a82000-03-20 06:03:29 +00002358 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2359 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2360 "specify a unique address");
2361
2362 generic_opt_check(command, options);
2363
2364 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2365 exit_error(PARAMETER_PROBLEM,
2366 "chain name `%s' too long (must be under %i chars)",
2367 chain, IPT_FUNCTION_MAXNAMELEN);
2368
Harald Welteae1ff9f2000-12-01 14:26:20 +00002369 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002370 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00002371 *handle = iptc_init(*table);
2372
Rusty Russell8beb0492004-12-22 00:37:10 +00002373 /* try to insmod the module if iptc_init failed */
2374 if (!*handle && iptables_insmod("ip_tables", modprobe) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002375 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002376
Marc Bouchere6869a82000-03-20 06:03:29 +00002377 if (!*handle)
2378 exit_error(VERSION_PROBLEM,
2379 "can't initialize iptables table `%s': %s",
2380 *table, iptc_strerror(errno));
2381
Harald Welte6336bfd2002-05-07 14:41:43 +00002382 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002383 || command == CMD_DELETE
2384 || command == CMD_INSERT
2385 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002386 if (strcmp(chain, "PREROUTING") == 0
2387 || strcmp(chain, "INPUT") == 0) {
2388 /* -o not valid with incoming packets. */
2389 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002390 exit_error(PARAMETER_PROBLEM,
2391 "Can't use -%c with %s\n",
2392 opt2char(OPT_VIANAMEOUT),
2393 chain);
2394 }
2395
Rusty Russella4860fd2000-06-17 16:13:02 +00002396 if (strcmp(chain, "POSTROUTING") == 0
2397 || strcmp(chain, "OUTPUT") == 0) {
2398 /* -i not valid with outgoing packets */
2399 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002400 exit_error(PARAMETER_PROBLEM,
2401 "Can't use -%c with %s\n",
2402 opt2char(OPT_VIANAMEIN),
2403 chain);
2404 }
2405
2406 if (target && iptc_is_chain(jumpto, *handle)) {
2407 printf("Warning: using chain %s, not extension\n",
2408 jumpto);
2409
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002410 if (target->t)
2411 free(target->t);
2412
Marc Bouchere6869a82000-03-20 06:03:29 +00002413 target = NULL;
2414 }
2415
2416 /* If they didn't specify a target, or it's a chain
2417 name, use standard. */
2418 if (!target
2419 && (strlen(jumpto) == 0
2420 || iptc_is_chain(jumpto, *handle))) {
2421 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002422
Rusty Russell52a51492000-05-02 16:44:29 +00002423 target = find_target(IPT_STANDARD_TARGET,
2424 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002425
2426 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002427 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002428 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002429 target->t->u.target_size = size;
2430 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002431 if (!iptc_is_chain(jumpto, *handle))
2432 set_revision(target->t->u.user.name,
2433 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002434 if (target->init != NULL)
2435 target->init(target->t, &fw.nfcache);
Marc Bouchere6869a82000-03-20 06:03:29 +00002436 }
2437
Rusty Russell7e53bf92000-03-20 07:03:28 +00002438 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002439 /* it is no chain, and we can't load a plugin.
2440 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002441 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002442 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002443#ifdef IPT_F_GOTO
2444 if (fw.ip.flags & IPT_F_GOTO)
2445 exit_error(PARAMETER_PROBLEM,
2446 "goto '%s' is not a chain\n", jumpto);
2447#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002448 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002449 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002450 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002451 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002452 }
2453 }
2454
2455 switch (command) {
2456 case CMD_APPEND:
2457 ret = append_entry(chain, e,
2458 nsaddrs, saddrs, ndaddrs, daddrs,
2459 options&OPT_VERBOSE,
2460 handle);
2461 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002462 case CMD_DELETE:
2463 ret = delete_entry(chain, e,
2464 nsaddrs, saddrs, ndaddrs, daddrs,
2465 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002466 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002467 break;
2468 case CMD_DELETE_NUM:
2469 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2470 break;
2471 case CMD_REPLACE:
2472 ret = replace_entry(chain, e, rulenum - 1,
2473 saddrs, daddrs, options&OPT_VERBOSE,
2474 handle);
2475 break;
2476 case CMD_INSERT:
2477 ret = insert_entry(chain, e, rulenum - 1,
2478 nsaddrs, saddrs, ndaddrs, daddrs,
2479 options&OPT_VERBOSE,
2480 handle);
2481 break;
2482 case CMD_LIST:
2483 ret = list_entries(chain,
2484 options&OPT_VERBOSE,
2485 options&OPT_NUMERIC,
2486 options&OPT_EXPANDED,
2487 options&OPT_LINENUMBERS,
2488 handle);
2489 break;
2490 case CMD_FLUSH:
2491 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2492 break;
2493 case CMD_ZERO:
2494 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2495 break;
2496 case CMD_LIST|CMD_ZERO:
2497 ret = list_entries(chain,
2498 options&OPT_VERBOSE,
2499 options&OPT_NUMERIC,
2500 options&OPT_EXPANDED,
2501 options&OPT_LINENUMBERS,
2502 handle);
2503 if (ret)
2504 ret = zero_entries(chain,
2505 options&OPT_VERBOSE, handle);
2506 break;
2507 case CMD_NEW_CHAIN:
2508 ret = iptc_create_chain(chain, handle);
2509 break;
2510 case CMD_DELETE_CHAIN:
2511 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2512 break;
2513 case CMD_RENAME_CHAIN:
2514 ret = iptc_rename_chain(chain, newname, handle);
2515 break;
2516 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002517 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002518 break;
2519 default:
2520 /* We should never reach this... */
2521 exit_tryhelp(2);
2522 }
2523
2524 if (verbose > 1)
2525 dump_entries(*handle);
2526
Martin Josefsson78cafda2004-02-02 20:01:18 +00002527 clear_rule_matches(&matches);
2528
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002529 if (e != NULL) {
2530 free(e);
2531 e = NULL;
2532 }
2533
keso6997cdf2004-07-04 15:20:53 +00002534 free(saddrs);
2535 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002536 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002537
Marc Bouchere6869a82000-03-20 06:03:29 +00002538 return ret;
2539}