blob: 55420ab94b1a2dc978d2dfbfdf954136ab9c10c7 [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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000031#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000032#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000033#include <fcntl.h>
34#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035
36#ifndef TRUE
37#define TRUE 1
38#endif
39#ifndef FALSE
40#define FALSE 0
41#endif
42
43#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte82dd2ec2000-12-19 05:18:15 +000047#ifndef PROC_SYS_MODPROBE
48#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
49#endif
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051#define FMT_NUMERIC 0x0001
52#define FMT_NOCOUNTS 0x0002
53#define FMT_KILOMEGAGIGA 0x0004
54#define FMT_OPTIONS 0x0008
55#define FMT_NOTABLE 0x0010
56#define FMT_NOTARGET 0x0020
57#define FMT_VIA 0x0040
58#define FMT_NONEWLINE 0x0080
59#define FMT_LINENUMBERS 0x0100
60
61#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
62 | FMT_NUMERIC | FMT_NOTABLE)
63#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
64
65
66#define CMD_NONE 0x0000U
67#define CMD_INSERT 0x0001U
68#define CMD_DELETE 0x0002U
69#define CMD_DELETE_NUM 0x0004U
70#define CMD_REPLACE 0x0008U
71#define CMD_APPEND 0x0010U
72#define CMD_LIST 0x0020U
73#define CMD_FLUSH 0x0040U
74#define CMD_ZERO 0x0080U
75#define CMD_NEW_CHAIN 0x0100U
76#define CMD_DELETE_CHAIN 0x0200U
77#define CMD_SET_POLICY 0x0400U
78#define CMD_CHECK 0x0800U
79#define CMD_RENAME_CHAIN 0x1000U
80#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
82 'N', 'X', 'P', 'C', 'E' };
83
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
104 { "append", 1, 0, 'A' },
105 { "delete", 1, 0, 'D' },
106 { "insert", 1, 0, 'I' },
107 { "replace", 1, 0, 'R' },
108 { "list", 2, 0, 'L' },
109 { "flush", 2, 0, 'F' },
110 { "zero", 2, 0, 'Z' },
111 { "check", 1, 0, 'C' },
112 { "new-chain", 1, 0, 'N' },
113 { "delete-chain", 2, 0, 'X' },
114 { "rename-chain", 2, 0, 'E' },
115 { "policy", 1, 0, 'P' },
116 { "source", 1, 0, 's' },
117 { "destination", 1, 0, 'd' },
118 { "src", 1, 0, 's' }, /* synonym */
119 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000120 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 { "in-interface", 1, 0, 'i' },
122 { "jump", 1, 0, 'j' },
123 { "table", 1, 0, 't' },
124 { "match", 1, 0, 'm' },
125 { "numeric", 0, 0, 'n' },
126 { "out-interface", 1, 0, 'o' },
127 { "verbose", 0, 0, 'v' },
128 { "exact", 0, 0, 'x' },
129 { "fragments", 0, 0, 'f' },
130 { "version", 0, 0, 'V' },
131 { "help", 2, 0, 'h' },
132 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000133 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000134 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 { 0 }
136};
137
Rusty Russell4e242f82000-05-31 06:33:50 +0000138#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000139struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000140ipt_get_target(struct ipt_entry *e)
141{
142 return (void *)e + e->target_offset;
143}
144#endif
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
Rusty Russell2e0a3212000-04-19 11:23:18 +0000195/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000196struct iptables_match *iptables_matches = NULL;
197struct iptables_target *iptables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries(const iptc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000209/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000210/* defined in netinet/in.h */
211#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
223 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "all", 0 },
227};
228
229static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000230proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int i;
233
Rusty Russell28381a42000-05-10 00:19:50 +0000234 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247struct in_addr *
248dotted_to_addr(const char *dotted)
249{
250 static struct in_addr addr;
251 unsigned char *addrp;
252 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000253 unsigned int onebyte;
254 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000255 char buf[20];
256
257 /* copy dotted string, because we need to modify it */
258 strncpy(buf, dotted, sizeof(buf) - 1);
259 addrp = (unsigned char *) &(addr.s_addr);
260
261 p = buf;
262 for (i = 0; i < 3; i++) {
263 if ((q = strchr(p, '.')) == NULL)
264 return (struct in_addr *) NULL;
265
266 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000267 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000268 return (struct in_addr *) NULL;
269
270 addrp[i] = (unsigned char) onebyte;
271 p = q + 1;
272 }
273
274 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000275 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 return (struct in_addr *) NULL;
277
278 addrp[3] = (unsigned char) onebyte;
279
280 return &addr;
281}
282
283static struct in_addr *
284network_to_addr(const char *name)
285{
286 struct netent *net;
287 static struct in_addr addr;
288
289 if ((net = getnetbyname(name)) != NULL) {
290 if (net->n_addrtype != AF_INET)
291 return (struct in_addr *) NULL;
292 addr.s_addr = htonl((unsigned long) net->n_net);
293 return &addr;
294 }
295
296 return (struct in_addr *) NULL;
297}
298
299static void
300inaddrcpy(struct in_addr *dst, struct in_addr *src)
301{
302 /* memcpy(dst, src, sizeof(struct in_addr)); */
303 dst->s_addr = src->s_addr;
304}
305
306void
307exit_error(enum exittype status, char *msg, ...)
308{
309 va_list args;
310
311 va_start(args, msg);
312 fprintf(stderr, "%s v%s: ", program_name, program_version);
313 vfprintf(stderr, msg, args);
314 va_end(args);
315 fprintf(stderr, "\n");
316 if (status == PARAMETER_PROBLEM)
317 exit_tryhelp(status);
318 if (status == VERSION_PROBLEM)
319 fprintf(stderr,
320 "Perhaps iptables or your kernel needs to be upgraded.\n");
321 exit(status);
322}
323
324void
325exit_tryhelp(int status)
326{
327 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
328 program_name, program_name );
329 exit(status);
330}
331
332void
333exit_printhelp(void)
334{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000335 struct iptables_match *m = NULL;
336 struct iptables_target *t = NULL;
337
Marc Bouchere6869a82000-03-20 06:03:29 +0000338 printf("%s v%s\n\n"
339"Usage: %s -[ADC] chain rule-specification [options]\n"
340" %s -[RI] chain rulenum rule-specification [options]\n"
341" %s -D chain rulenum [options]\n"
342" %s -[LFZ] [chain] [options]\n"
343" %s -[NX] chain\n"
344" %s -E old-chain-name new-chain-name\n"
345" %s -P chain target [options]\n"
346" %s -h (print this help information)\n\n",
347 program_name, program_version, program_name, program_name,
348 program_name, program_name, program_name, program_name,
349 program_name, program_name);
350
351 printf(
352"Commands:\n"
353"Either long or short options are allowed.\n"
354" --append -A chain Append to chain\n"
355" --delete -D chain Delete matching rule from chain\n"
356" --delete -D chain rulenum\n"
357" Delete rule rulenum (1 = first) from chain\n"
358" --insert -I chain [rulenum]\n"
359" Insert in chain as rulenum (default 1=first)\n"
360" --replace -R chain rulenum\n"
361" Replace rule rulenum (1 = first) in chain\n"
362" --list -L [chain] List the rules in a chain or all chains\n"
363" --flush -F [chain] Delete all rules in chain or all chains\n"
364" --zero -Z [chain] Zero counters in chain or all chains\n"
365" --check -C chain Test this packet on chain\n"
366" --new -N chain Create a new user-defined chain\n"
367" --delete-chain\n"
368" -X [chain] Delete a user-defined chain\n"
369" --policy -P chain target\n"
370" Change policy on chain to target\n"
371" --rename-chain\n"
372" -E old-chain new-chain\n"
373" Change chain name, (moving any references)\n"
374
375"Options:\n"
376" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
377" --source -s [!] address[/mask]\n"
378" source specification\n"
379" --destination -d [!] address[/mask]\n"
380" destination specification\n"
381" --in-interface -i [!] input name[+]\n"
382" network interface name ([+] for wildcard)\n"
383" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000384" target for rule (may load target extension)\n"
385" --match -m match\n"
386" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000387" --numeric -n numeric output of addresses and ports\n"
388" --out-interface -o [!] output name[+]\n"
389" network interface name ([+] for wildcard)\n"
390" --table -t table table to manipulate (default: `filter')\n"
391" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000392" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000393" --exact -x expand numbers (display exact values)\n"
394"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000395" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000396" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000397"[!] --version -V print package version.\n");
398
Rusty Russell363112d2000-08-11 13:49:26 +0000399 /* Print out any special helps. A user might like to be able
400 to add a --help to the commandline, and see expected
401 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000402 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000404 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000406 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000408 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000409 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000410 exit(0);
411}
412
413static void
414generic_opt_check(int command, int options)
415{
416 int i, j, legal = 0;
417
418 /* Check that commands are valid with options. Complicated by the
419 * fact that if an option is legal with *any* command given, it is
420 * legal overall (ie. -z and -l).
421 */
422 for (i = 0; i < NUMBER_OF_OPT; i++) {
423 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
424
425 for (j = 0; j < NUMBER_OF_CMD; j++) {
426 if (!(command & (1<<j)))
427 continue;
428
429 if (!(options & (1<<i))) {
430 if (commands_v_options[j][i] == '+')
431 exit_error(PARAMETER_PROBLEM,
432 "You need to supply the `-%c' "
433 "option for this command\n",
434 optflags[i]);
435 } else {
436 if (commands_v_options[j][i] != 'x')
437 legal = 1;
438 else if (legal == 0)
439 legal = -1;
440 }
441 }
442 if (legal == -1)
443 exit_error(PARAMETER_PROBLEM,
444 "Illegal option `-%c' with this command\n",
445 optflags[i]);
446 }
447}
448
449static char
450opt2char(int option)
451{
452 const char *ptr;
453 for (ptr = optflags; option > 1; option >>= 1, ptr++);
454
455 return *ptr;
456}
457
458static char
459cmd2char(int option)
460{
461 const char *ptr;
462 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
463
464 return *ptr;
465}
466
467static void
468add_command(int *cmd, const int newcmd, const int othercmds, int invert)
469{
470 if (invert)
471 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
472 if (*cmd & (~othercmds))
473 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
474 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
475 *cmd |= newcmd;
476}
477
478int
479check_inverse(const char option[], int *invert)
480{
481 if (option && strcmp(option, "!") == 0) {
482 if (*invert)
483 exit_error(PARAMETER_PROBLEM,
484 "Multiple `!' flags not allowed");
485
486 *invert = TRUE;
487 return TRUE;
488 }
489 return FALSE;
490}
491
492static void *
493fw_calloc(size_t count, size_t size)
494{
495 void *p;
496
497 if ((p = calloc(count, size)) == NULL) {
498 perror("iptables: calloc failed");
499 exit(1);
500 }
501 return p;
502}
503
504static void *
505fw_malloc(size_t size)
506{
507 void *p;
508
509 if ((p = malloc(size)) == NULL) {
510 perror("iptables: malloc failed");
511 exit(1);
512 }
513 return p;
514}
515
516static struct in_addr *
517host_to_addr(const char *name, unsigned int *naddr)
518{
519 struct hostent *host;
520 struct in_addr *addr;
521 unsigned int i;
522
523 *naddr = 0;
524 if ((host = gethostbyname(name)) != NULL) {
525 if (host->h_addrtype != AF_INET ||
526 host->h_length != sizeof(struct in_addr))
527 return (struct in_addr *) NULL;
528
529 while (host->h_addr_list[*naddr] != (char *) NULL)
530 (*naddr)++;
531 addr = fw_calloc(*naddr, sizeof(struct in_addr));
532 for (i = 0; i < *naddr; i++)
533 inaddrcpy(&(addr[i]),
534 (struct in_addr *) host->h_addr_list[i]);
535 return addr;
536 }
537
538 return (struct in_addr *) NULL;
539}
540
541static char *
542addr_to_host(const struct in_addr *addr)
543{
544 struct hostent *host;
545
546 if ((host = gethostbyaddr((char *) addr,
547 sizeof(struct in_addr), AF_INET)) != NULL)
548 return (char *) host->h_name;
549
550 return (char *) NULL;
551}
552
553/*
554 * All functions starting with "parse" should succeed, otherwise
555 * the program fails.
556 * Most routines return pointers to static data that may change
557 * between calls to the same or other routines with a few exceptions:
558 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
559 * return global static data.
560*/
561
562static struct in_addr *
563parse_hostnetwork(const char *name, unsigned int *naddrs)
564{
565 struct in_addr *addrp, *addrptmp;
566
567 if ((addrptmp = dotted_to_addr(name)) != NULL ||
568 (addrptmp = network_to_addr(name)) != NULL) {
569 addrp = fw_malloc(sizeof(struct in_addr));
570 inaddrcpy(addrp, addrptmp);
571 *naddrs = 1;
572 return addrp;
573 }
574 if ((addrp = host_to_addr(name, naddrs)) != NULL)
575 return addrp;
576
577 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
578}
579
580static struct in_addr *
581parse_mask(char *mask)
582{
583 static struct in_addr maskaddr;
584 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000585 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000586
587 if (mask == NULL) {
588 /* no mask at all defaults to 32 bits */
589 maskaddr.s_addr = 0xFFFFFFFF;
590 return &maskaddr;
591 }
592 if ((addrp = dotted_to_addr(mask)) != NULL)
593 /* dotted_to_addr already returns a network byte order addr */
594 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000595 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000596 exit_error(PARAMETER_PROBLEM,
597 "invalid mask `%s' specified", mask);
598 if (bits != 0) {
599 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
600 return &maskaddr;
601 }
602
603 maskaddr.s_addr = 0L;
604 return &maskaddr;
605}
606
607static void
608parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
609 struct in_addr *maskp, unsigned int *naddrs)
610{
611 struct in_addr *addrp;
612 char buf[256];
613 char *p;
614 int i, j, k, n;
615
616 strncpy(buf, name, sizeof(buf) - 1);
617 if ((p = strrchr(buf, '/')) != NULL) {
618 *p = '\0';
619 addrp = parse_mask(p + 1);
620 } else
621 addrp = parse_mask(NULL);
622 inaddrcpy(maskp, addrp);
623
624 /* if a null mask is given, the name is ignored, like in "any/0" */
625 if (maskp->s_addr == 0L)
626 strcpy(buf, "0.0.0.0");
627
628 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
629 n = *naddrs;
630 for (i = 0, j = 0; i < n; i++) {
631 addrp[j++].s_addr &= maskp->s_addr;
632 for (k = 0; k < j - 1; k++) {
633 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
634 (*naddrs)--;
635 j--;
636 break;
637 }
638 }
639 }
640}
641
642struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000643find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000644{
645 struct iptables_match *ptr;
646
647 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
648 if (strcmp(name, ptr->name) == 0)
649 break;
650 }
651
Rusty Russell52a51492000-05-02 16:44:29 +0000652 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000653 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
654 + strlen(name)];
655 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000656 if (dlopen(path, RTLD_NOW)) {
657 /* Found library. If it didn't register itself,
658 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000659 ptr = find_match(name, DONT_LOAD);
660
Rusty Russell9e1d2142000-04-23 09:11:12 +0000661 if (!ptr)
662 exit_error(PARAMETER_PROBLEM,
663 "Couldn't load match `%s'\n",
664 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000665 } else if (tryload == LOAD_MUST_SUCCEED)
666 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000667 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000668 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000669 }
670
Harald Welteae1ff9f2000-12-01 14:26:20 +0000671 if (ptr)
672 ptr->used = 1;
673
Marc Bouchere6869a82000-03-20 06:03:29 +0000674 return ptr;
675}
676
Rusty Russell28381a42000-05-10 00:19:50 +0000677/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
678static struct iptables_match *
679find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
680{
Harald Welteed498492001-07-23 01:24:22 +0000681 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000682
Harald Welteed498492001-07-23 01:24:22 +0000683 if (string_to_number(pname, 0, 255, &proto) != -1)
Rusty Russell28381a42000-05-10 00:19:50 +0000684 return find_match(proto_to_name(proto, nolookup), tryload);
685
686 return find_match(pname, tryload);
687}
688
Marc Bouchere6869a82000-03-20 06:03:29 +0000689static u_int16_t
690parse_protocol(const char *s)
691{
Harald Welteed498492001-07-23 01:24:22 +0000692 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000693
Harald Welteed498492001-07-23 01:24:22 +0000694 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000695 struct protoent *pent;
696
697 if ((pent = getprotobyname(s)))
698 proto = pent->p_proto;
699 else {
700 unsigned int i;
701 for (i = 0;
702 i < sizeof(chain_protos)/sizeof(struct pprot);
703 i++) {
704 if (strcmp(s, chain_protos[i].name) == 0) {
705 proto = chain_protos[i].num;
706 break;
707 }
708 }
709 if (i == sizeof(chain_protos)/sizeof(struct pprot))
710 exit_error(PARAMETER_PROBLEM,
711 "unknown protocol `%s' specified",
712 s);
713 }
714 }
715
716 return (u_int16_t)proto;
717}
718
719static void
720parse_interface(const char *arg, char *vianame, unsigned char *mask)
721{
722 int vialen = strlen(arg);
723 unsigned int i;
724
725 memset(mask, 0, IFNAMSIZ);
726 memset(vianame, 0, IFNAMSIZ);
727
728 if (vialen + 1 > IFNAMSIZ)
729 exit_error(PARAMETER_PROBLEM,
730 "interface name `%s' must be shorter than IFNAMSIZ"
731 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000732
Marc Bouchere6869a82000-03-20 06:03:29 +0000733 strcpy(vianame, arg);
734 if (vialen == 0)
735 memset(mask, 0, IFNAMSIZ);
736 else if (vianame[vialen - 1] == '+') {
737 memset(mask, 0xFF, vialen - 1);
738 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000739 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000740 } else {
741 /* Include nul-terminator in match */
742 memset(mask, 0xFF, vialen + 1);
743 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000744 for (i = 0; vianame[i]; i++) {
745 if (!isalnum(vianame[i]) && vianame[i] != '_') {
746 printf("Warning: wierd character in interface"
747 " `%s' (No aliases, :, ! or *).\n",
748 vianame);
749 break;
750 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 }
752 }
753}
754
755/* Can't be zero. */
756static int
757parse_rulenumber(const char *rule)
758{
Harald Welteed498492001-07-23 01:24:22 +0000759 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000760
Harald Welteed498492001-07-23 01:24:22 +0000761 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000762 exit_error(PARAMETER_PROBLEM,
763 "Invalid rule number `%s'", rule);
764
765 return rulenum;
766}
767
768static const char *
769parse_target(const char *targetname)
770{
771 const char *ptr;
772
773 if (strlen(targetname) < 1)
774 exit_error(PARAMETER_PROBLEM,
775 "Invalid target name (too short)");
776
777 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
778 exit_error(PARAMETER_PROBLEM,
779 "Invalid target name `%s' (%i chars max)",
780 targetname, sizeof(ipt_chainlabel)-1);
781
782 for (ptr = targetname; *ptr; ptr++)
783 if (isspace(*ptr))
784 exit_error(PARAMETER_PROBLEM,
785 "Invalid target name `%s'", targetname);
786 return targetname;
787}
788
789static char *
790addr_to_network(const struct in_addr *addr)
791{
792 struct netent *net;
793
794 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
795 return (char *) net->n_name;
796
797 return (char *) NULL;
798}
799
800char *
801addr_to_dotted(const struct in_addr *addrp)
802{
803 static char buf[20];
804 const unsigned char *bytep;
805
806 bytep = (const unsigned char *) &(addrp->s_addr);
807 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
808 return buf;
809}
810static char *
811addr_to_anyname(const struct in_addr *addr)
812{
813 char *name;
814
815 if ((name = addr_to_host(addr)) != NULL ||
816 (name = addr_to_network(addr)) != NULL)
817 return name;
818
819 return addr_to_dotted(addr);
820}
821
822static char *
823mask_to_dotted(const struct in_addr *mask)
824{
825 int i;
826 static char buf[20];
827 u_int32_t maskaddr, bits;
828
829 maskaddr = ntohl(mask->s_addr);
830
831 if (maskaddr == 0xFFFFFFFFL)
832 /* we don't want to see "/32" */
833 return "";
834
835 i = 32;
836 bits = 0xFFFFFFFEL;
837 while (--i >= 0 && maskaddr != bits)
838 bits <<= 1;
839 if (i >= 0)
840 sprintf(buf, "/%d", i);
841 else
842 /* mask was not a decent combination of 1's and 0's */
843 sprintf(buf, "/%s", addr_to_dotted(mask));
844
845 return buf;
846}
847
848int
Harald Welteed498492001-07-23 01:24:22 +0000849string_to_number(const char *s, unsigned int min, unsigned int max,
850 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000851{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000852 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000853 char *end;
854
855 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000856 errno = 0;
857 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000858 if (*end == '\0' && end != s) {
859 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000860 if (errno != ERANGE && min <= number && number <= max) {
861 *ret = number;
862 return 0;
863 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000864 }
865 return -1;
866}
867
868static void
869set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
870 int invert)
871{
872 if (*options & option)
873 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
874 opt2char(option));
875 *options |= option;
876
877 if (invert) {
878 unsigned int i;
879 for (i = 0; 1 << i != option; i++);
880
881 if (!inverse_for_options[i])
882 exit_error(PARAMETER_PROBLEM,
883 "cannot have ! before -%c",
884 opt2char(option));
885 *invflg |= inverse_for_options[i];
886 }
887}
888
889struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000890find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000891{
892 struct iptables_target *ptr;
893
894 /* Standard target? */
895 if (strcmp(name, "") == 0
896 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
897 || strcmp(name, IPTC_LABEL_DROP) == 0
898 || strcmp(name, IPTC_LABEL_QUEUE) == 0
899 || strcmp(name, IPTC_LABEL_RETURN) == 0)
900 name = "standard";
901
902 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
903 if (strcmp(name, ptr->name) == 0)
904 break;
905 }
906
Rusty Russell52a51492000-05-02 16:44:29 +0000907 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000908 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
909 + strlen(name)];
910 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000911 if (dlopen(path, RTLD_NOW)) {
912 /* Found library. If it didn't register itself,
913 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000914 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000915 if (!ptr)
916 exit_error(PARAMETER_PROBLEM,
917 "Couldn't load target `%s'\n",
918 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000919 } else if (tryload == LOAD_MUST_SUCCEED)
920 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000921 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000922 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000923 }
924
Harald Welteae1ff9f2000-12-01 14:26:20 +0000925 if (ptr)
926 ptr->used = 1;
927
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 return ptr;
929}
930
931static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000932merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000933 unsigned int *option_offset)
934{
935 unsigned int num_old, num_new, i;
936 struct option *merge;
937
938 for (num_old = 0; oldopts[num_old].name; num_old++);
939 for (num_new = 0; newopts[num_new].name; num_new++);
940
941 global_option_offset += OPTION_OFFSET;
942 *option_offset = global_option_offset;
943
944 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
945 memcpy(merge, oldopts, num_old * sizeof(struct option));
946 for (i = 0; i < num_new; i++) {
947 merge[num_old + i] = newopts[i];
948 merge[num_old + i].val += *option_offset;
949 }
950 memset(merge + num_old + num_new, 0, sizeof(struct option));
951
952 return merge;
953}
954
955void
956register_match(struct iptables_match *me)
957{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000958 struct iptables_match **i;
959
Marc Bouchere6869a82000-03-20 06:03:29 +0000960 if (strcmp(me->version, program_version) != 0) {
961 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
962 program_name, me->name, me->version, program_version);
963 exit(1);
964 }
965
Rusty Russell52a51492000-05-02 16:44:29 +0000966 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000967 fprintf(stderr, "%s: match `%s' already registered.\n",
968 program_name, me->name);
969 exit(1);
970 }
971
Rusty Russell73f72f52000-07-03 10:17:57 +0000972 if (me->size != IPT_ALIGN(me->size)) {
973 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
974 program_name, me->name, me->size);
975 exit(1);
976 }
977
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000978 /* Append to list. */
979 for (i = &iptables_matches; *i; i = &(*i)->next);
980 me->next = NULL;
981 *i = me;
982
Marc Bouchere6869a82000-03-20 06:03:29 +0000983 me->m = NULL;
984 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000985}
986
987void
988register_target(struct iptables_target *me)
989{
990 if (strcmp(me->version, program_version) != 0) {
991 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
992 program_name, me->name, me->version, program_version);
993 exit(1);
994 }
995
Rusty Russell52a51492000-05-02 16:44:29 +0000996 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000997 fprintf(stderr, "%s: target `%s' already registered.\n",
998 program_name, me->name);
999 exit(1);
1000 }
1001
Rusty Russell73f72f52000-07-03 10:17:57 +00001002 if (me->size != IPT_ALIGN(me->size)) {
1003 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1004 program_name, me->name, me->size);
1005 exit(1);
1006 }
1007
Marc Bouchere6869a82000-03-20 06:03:29 +00001008 /* Prepend to list. */
1009 me->next = iptables_targets;
1010 iptables_targets = me;
1011 me->t = NULL;
1012 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001013}
1014
1015static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001016print_num(u_int64_t number, unsigned int format)
1017{
1018 if (format & FMT_KILOMEGAGIGA) {
1019 if (number > 99999) {
1020 number = (number + 500) / 1000;
1021 if (number > 9999) {
1022 number = (number + 500) / 1000;
1023 if (number > 9999) {
1024 number = (number + 500) / 1000;
1025 printf(FMT("%4lluG ","%lluG "),number);
1026 }
1027 else printf(FMT("%4lluM ","%lluM "), number);
1028 } else
1029 printf(FMT("%4lluK ","%lluK "), number);
1030 } else
1031 printf(FMT("%5llu ","%llu "), number);
1032 } else
1033 printf(FMT("%8llu ","%llu "), number);
1034}
1035
1036
1037static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001038print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1039{
1040 struct ipt_counters counters;
1041 const char *pol = iptc_get_policy(chain, &counters, handle);
1042 printf("Chain %s", chain);
1043 if (pol) {
1044 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001045 if (!(format & FMT_NOCOUNTS)) {
1046 fputc(' ', stdout);
1047 print_num(counters.pcnt, (format|FMT_NOTABLE));
1048 fputs("packets, ", stdout);
1049 print_num(counters.bcnt, (format|FMT_NOTABLE));
1050 fputs("bytes", stdout);
1051 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001052 printf(")\n");
1053 } else {
1054 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001055 if (!iptc_get_references(&refs, chain, handle))
1056 printf(" (ERROR obtaining refs)\n");
1057 else
1058 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001059 }
1060
1061 if (format & FMT_LINENUMBERS)
1062 printf(FMT("%-4s ", "%s "), "num");
1063 if (!(format & FMT_NOCOUNTS)) {
1064 if (format & FMT_KILOMEGAGIGA) {
1065 printf(FMT("%5s ","%s "), "pkts");
1066 printf(FMT("%5s ","%s "), "bytes");
1067 } else {
1068 printf(FMT("%8s ","%s "), "pkts");
1069 printf(FMT("%10s ","%s "), "bytes");
1070 }
1071 }
1072 if (!(format & FMT_NOTARGET))
1073 printf(FMT("%-9s ","%s "), "target");
1074 fputs(" prot ", stdout);
1075 if (format & FMT_OPTIONS)
1076 fputs("opt", stdout);
1077 if (format & FMT_VIA) {
1078 printf(FMT(" %-6s ","%s "), "in");
1079 printf(FMT("%-6s ","%s "), "out");
1080 }
1081 printf(FMT(" %-19s ","%s "), "source");
1082 printf(FMT(" %-19s "," %s "), "destination");
1083 printf("\n");
1084}
1085
Marc Bouchere6869a82000-03-20 06:03:29 +00001086
1087static int
1088print_match(const struct ipt_entry_match *m,
1089 const struct ipt_ip *ip,
1090 int numeric)
1091{
Rusty Russell52a51492000-05-02 16:44:29 +00001092 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001093
1094 if (match) {
1095 if (match->print)
1096 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001097 else
Rusty Russellb039b022000-09-01 06:04:05 +00001098 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001099 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001100 if (m->u.user.name[0])
1101 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001102 }
1103 /* Don't stop iterating. */
1104 return 0;
1105}
1106
1107/* e is called `fw' here for hysterical raisins */
1108static void
1109print_firewall(const struct ipt_entry *fw,
1110 const char *targname,
1111 unsigned int num,
1112 unsigned int format,
1113 const iptc_handle_t handle)
1114{
1115 struct iptables_target *target = NULL;
1116 const struct ipt_entry_target *t;
1117 u_int8_t flags;
1118 char buf[BUFSIZ];
1119
1120 /* User creates a chain called "REJECT": this overrides the
1121 `REJECT' target module. Keep feeding them rope until the
1122 revolution... Bwahahahahah */
1123 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001124 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001125 else
Rusty Russell52a51492000-05-02 16:44:29 +00001126 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001127
1128 t = ipt_get_target((struct ipt_entry *)fw);
1129 flags = fw->ip.flags;
1130
1131 if (format & FMT_LINENUMBERS)
1132 printf(FMT("%-4u ", "%u "), num+1);
1133
1134 if (!(format & FMT_NOCOUNTS)) {
1135 print_num(fw->counters.pcnt, format);
1136 print_num(fw->counters.bcnt, format);
1137 }
1138
1139 if (!(format & FMT_NOTARGET))
1140 printf(FMT("%-9s ", "%s "), targname);
1141
1142 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1143 {
Rusty Russell28381a42000-05-10 00:19:50 +00001144 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001145 if (pname)
1146 printf(FMT("%-5s", "%s "), pname);
1147 else
1148 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1149 }
1150
1151 if (format & FMT_OPTIONS) {
1152 if (format & FMT_NOTABLE)
1153 fputs("opt ", stdout);
1154 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1155 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1156 fputc(' ', stdout);
1157 }
1158
1159 if (format & FMT_VIA) {
1160 char iface[IFNAMSIZ+2];
1161
1162 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1163 iface[0] = '!';
1164 iface[1] = '\0';
1165 }
1166 else iface[0] = '\0';
1167
1168 if (fw->ip.iniface[0] != '\0') {
1169 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001170 }
1171 else if (format & FMT_NUMERIC) strcat(iface, "*");
1172 else strcat(iface, "any");
1173 printf(FMT(" %-6s ","in %s "), iface);
1174
1175 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1176 iface[0] = '!';
1177 iface[1] = '\0';
1178 }
1179 else iface[0] = '\0';
1180
1181 if (fw->ip.outiface[0] != '\0') {
1182 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001183 }
1184 else if (format & FMT_NUMERIC) strcat(iface, "*");
1185 else strcat(iface, "any");
1186 printf(FMT("%-6s ","out %s "), iface);
1187 }
1188
1189 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1190 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1191 printf(FMT("%-19s ","%s "), "anywhere");
1192 else {
1193 if (format & FMT_NUMERIC)
1194 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1195 else
1196 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1197 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1198 printf(FMT("%-19s ","%s "), buf);
1199 }
1200
1201 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1202 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1203 printf(FMT("%-19s","-> %s"), "anywhere");
1204 else {
1205 if (format & FMT_NUMERIC)
1206 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1207 else
1208 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1209 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1210 printf(FMT("%-19s","-> %s"), buf);
1211 }
1212
1213 if (format & FMT_NOTABLE)
1214 fputs(" ", stdout);
1215
1216 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1217
1218 if (target) {
1219 if (target->print)
1220 /* Print the target information. */
1221 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001222 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001223 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001224 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001225
1226 if (!(format & FMT_NONEWLINE))
1227 fputc('\n', stdout);
1228}
1229
1230static void
1231print_firewall_line(const struct ipt_entry *fw,
1232 const iptc_handle_t h)
1233{
1234 struct ipt_entry_target *t;
1235
1236 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001237 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001238}
1239
1240static int
1241append_entry(const ipt_chainlabel chain,
1242 struct ipt_entry *fw,
1243 unsigned int nsaddrs,
1244 const struct in_addr saddrs[],
1245 unsigned int ndaddrs,
1246 const struct in_addr daddrs[],
1247 int verbose,
1248 iptc_handle_t *handle)
1249{
1250 unsigned int i, j;
1251 int ret = 1;
1252
1253 for (i = 0; i < nsaddrs; i++) {
1254 fw->ip.src.s_addr = saddrs[i].s_addr;
1255 for (j = 0; j < ndaddrs; j++) {
1256 fw->ip.dst.s_addr = daddrs[j].s_addr;
1257 if (verbose)
1258 print_firewall_line(fw, *handle);
1259 ret &= iptc_append_entry(chain, fw, handle);
1260 }
1261 }
1262
1263 return ret;
1264}
1265
1266static int
1267replace_entry(const ipt_chainlabel chain,
1268 struct ipt_entry *fw,
1269 unsigned int rulenum,
1270 const struct in_addr *saddr,
1271 const struct in_addr *daddr,
1272 int verbose,
1273 iptc_handle_t *handle)
1274{
1275 fw->ip.src.s_addr = saddr->s_addr;
1276 fw->ip.dst.s_addr = daddr->s_addr;
1277
1278 if (verbose)
1279 print_firewall_line(fw, *handle);
1280 return iptc_replace_entry(chain, fw, rulenum, handle);
1281}
1282
1283static int
1284insert_entry(const ipt_chainlabel chain,
1285 struct ipt_entry *fw,
1286 unsigned int rulenum,
1287 unsigned int nsaddrs,
1288 const struct in_addr saddrs[],
1289 unsigned int ndaddrs,
1290 const struct in_addr daddrs[],
1291 int verbose,
1292 iptc_handle_t *handle)
1293{
1294 unsigned int i, j;
1295 int ret = 1;
1296
1297 for (i = 0; i < nsaddrs; i++) {
1298 fw->ip.src.s_addr = saddrs[i].s_addr;
1299 for (j = 0; j < ndaddrs; j++) {
1300 fw->ip.dst.s_addr = daddrs[j].s_addr;
1301 if (verbose)
1302 print_firewall_line(fw, *handle);
1303 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1304 }
1305 }
1306
1307 return ret;
1308}
1309
Rusty Russell2e0a3212000-04-19 11:23:18 +00001310static unsigned char *
1311make_delete_mask(struct ipt_entry *fw)
1312{
1313 /* Establish mask for comparison */
1314 unsigned int size;
1315 struct iptables_match *m;
1316 unsigned char *mask, *mptr;
1317
1318 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001319 for (m = iptables_matches; m; m = m->next) {
1320 if (!m->used)
1321 continue;
1322
Rusty Russell73f72f52000-07-03 10:17:57 +00001323 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001324 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001325
Rusty Russell9e1d2142000-04-23 09:11:12 +00001326 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001327 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001328 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001329
Rusty Russell9e1d2142000-04-23 09:11:12 +00001330 memset(mask, 0xFF, sizeof(struct ipt_entry));
1331 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001332
Sven Kochfb1279a2001-02-27 12:25:12 +00001333 for (m = iptables_matches; m; m = m->next) {
1334 if (!m->used)
1335 continue;
1336
Rusty Russell2e0a3212000-04-19 11:23:18 +00001337 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001338 IPT_ALIGN(sizeof(struct ipt_entry_match))
1339 + m->userspacesize);
1340 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001341 }
1342
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001343 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001344 IPT_ALIGN(sizeof(struct ipt_entry_target))
1345 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001346
1347 return mask;
1348}
1349
Marc Bouchere6869a82000-03-20 06:03:29 +00001350static int
1351delete_entry(const ipt_chainlabel chain,
1352 struct ipt_entry *fw,
1353 unsigned int nsaddrs,
1354 const struct in_addr saddrs[],
1355 unsigned int ndaddrs,
1356 const struct in_addr daddrs[],
1357 int verbose,
1358 iptc_handle_t *handle)
1359{
1360 unsigned int i, j;
1361 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001362 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001363
Rusty Russell2e0a3212000-04-19 11:23:18 +00001364 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001365 for (i = 0; i < nsaddrs; i++) {
1366 fw->ip.src.s_addr = saddrs[i].s_addr;
1367 for (j = 0; j < ndaddrs; j++) {
1368 fw->ip.dst.s_addr = daddrs[j].s_addr;
1369 if (verbose)
1370 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001371 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001372 }
1373 }
1374 return ret;
1375}
1376
1377static int
1378check_packet(const ipt_chainlabel chain,
1379 struct ipt_entry *fw,
1380 unsigned int nsaddrs,
1381 const struct in_addr saddrs[],
1382 unsigned int ndaddrs,
1383 const struct in_addr daddrs[],
1384 int verbose,
1385 iptc_handle_t *handle)
1386{
1387 int ret = 1;
1388 unsigned int i, j;
1389 const char *msg;
1390
1391 for (i = 0; i < nsaddrs; i++) {
1392 fw->ip.src.s_addr = saddrs[i].s_addr;
1393 for (j = 0; j < ndaddrs; j++) {
1394 fw->ip.dst.s_addr = daddrs[j].s_addr;
1395 if (verbose)
1396 print_firewall_line(fw, *handle);
1397 msg = iptc_check_packet(chain, fw, handle);
1398 if (!msg) ret = 0;
1399 else printf("%s\n", msg);
1400 }
1401 }
1402
1403 return ret;
1404}
1405
Harald Welteae1ff9f2000-12-01 14:26:20 +00001406int
Marc Bouchere6869a82000-03-20 06:03:29 +00001407for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001408 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001409{
1410 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001411 const char *chain;
1412 char *chains;
1413 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001414
Rusty Russell9e1d2142000-04-23 09:11:12 +00001415 chain = iptc_first_chain(handle);
1416 while (chain) {
1417 chaincount++;
1418 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001419 }
1420
Rusty Russell9e1d2142000-04-23 09:11:12 +00001421 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1422 i = 0;
1423 chain = iptc_first_chain(handle);
1424 while (chain) {
1425 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1426 i++;
1427 chain = iptc_next_chain(handle);
1428 }
1429
1430 for (i = 0; i < chaincount; i++) {
1431 if (!builtinstoo
1432 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1433 *handle))
1434 continue;
1435 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1436 }
1437
1438 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001439 return ret;
1440}
1441
Harald Welteae1ff9f2000-12-01 14:26:20 +00001442int
Marc Bouchere6869a82000-03-20 06:03:29 +00001443flush_entries(const ipt_chainlabel chain, int verbose,
1444 iptc_handle_t *handle)
1445{
1446 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001447 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001448
1449 if (verbose)
1450 fprintf(stdout, "Flushing chain `%s'\n", chain);
1451 return iptc_flush_entries(chain, handle);
1452}
Marc Bouchere6869a82000-03-20 06:03:29 +00001453
1454static int
1455zero_entries(const ipt_chainlabel chain, int verbose,
1456 iptc_handle_t *handle)
1457{
1458 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001459 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001460
Marc Bouchere6869a82000-03-20 06:03:29 +00001461 if (verbose)
1462 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1463 return iptc_zero_entries(chain, handle);
1464}
1465
Harald Welteae1ff9f2000-12-01 14:26:20 +00001466int
Marc Bouchere6869a82000-03-20 06:03:29 +00001467delete_chain(const ipt_chainlabel chain, int verbose,
1468 iptc_handle_t *handle)
1469{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001470 if (!chain)
1471 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001472
1473 if (verbose)
1474 fprintf(stdout, "Deleting chain `%s'\n", chain);
1475 return iptc_delete_chain(chain, handle);
1476}
1477
1478static int
1479list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1480 int expanded, int linenumbers, iptc_handle_t *handle)
1481{
1482 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001483 unsigned int format;
1484 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001485
1486 format = FMT_OPTIONS;
1487 if (!verbose)
1488 format |= FMT_NOCOUNTS;
1489 else
1490 format |= FMT_VIA;
1491
1492 if (numeric)
1493 format |= FMT_NUMERIC;
1494
1495 if (!expanded)
1496 format |= FMT_KILOMEGAGIGA;
1497
1498 if (linenumbers)
1499 format |= FMT_LINENUMBERS;
1500
Rusty Russell9e1d2142000-04-23 09:11:12 +00001501 for (this = iptc_first_chain(handle);
1502 this;
1503 this = iptc_next_chain(handle)) {
1504 const struct ipt_entry *i;
1505 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001506
Marc Bouchere6869a82000-03-20 06:03:29 +00001507 if (chain && strcmp(chain, this) != 0)
1508 continue;
1509
1510 if (found) printf("\n");
1511
1512 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001513 i = iptc_first_rule(this, handle);
1514
1515 num = 0;
1516 while (i) {
1517 print_firewall(i,
1518 iptc_get_target(i, handle),
1519 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001520 format,
1521 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001522 i = iptc_next_rule(i, handle);
1523 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001524 found = 1;
1525 }
1526
1527 errno = ENOENT;
1528 return found;
1529}
1530
Harald Welte82dd2ec2000-12-19 05:18:15 +00001531static char *get_modprobe(void)
1532{
1533 int procfile;
1534 char *ret;
1535
1536 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1537 if (procfile < 0)
1538 return NULL;
1539
1540 ret = malloc(1024);
1541 if (ret) {
1542 switch (read(procfile, ret, 1024)) {
1543 case -1: goto fail;
1544 case 1024: goto fail; /* Partial read. Wierd */
1545 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001546 if (ret[strlen(ret)-1]=='\n')
1547 ret[strlen(ret)-1]=0;
1548 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001549 return ret;
1550 }
1551 fail:
1552 free(ret);
1553 close(procfile);
1554 return NULL;
1555}
1556
Harald Welte58918652001-06-16 18:25:25 +00001557int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001558{
1559 char *buf = NULL;
1560 char *argv[3];
1561
1562 /* If they don't explicitly set it, read out of kernel */
1563 if (!modprobe) {
1564 buf = get_modprobe();
1565 if (!buf)
1566 return -1;
1567 modprobe = buf;
1568 }
1569
1570 switch (fork()) {
1571 case 0:
1572 argv[0] = (char *)modprobe;
1573 argv[1] = (char *)modname;
1574 argv[2] = NULL;
1575 execv(argv[0], argv);
1576
1577 /* not usually reached */
1578 exit(0);
1579 case -1:
1580 return -1;
1581
1582 default: /* parent */
1583 wait(NULL);
1584 }
1585
1586 free(buf);
1587 return 0;
1588}
1589
Marc Bouchere6869a82000-03-20 06:03:29 +00001590static struct ipt_entry *
1591generate_entry(const struct ipt_entry *fw,
1592 struct iptables_match *matches,
1593 struct ipt_entry_target *target)
1594{
1595 unsigned int size;
1596 struct iptables_match *m;
1597 struct ipt_entry *e;
1598
1599 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001600 for (m = matches; m; m = m->next) {
1601 if (!m->used)
1602 continue;
1603
Rusty Russell228e98d2000-04-27 10:28:06 +00001604 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001605 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001606
Rusty Russell228e98d2000-04-27 10:28:06 +00001607 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001608 *e = *fw;
1609 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001610 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001611
1612 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001613 for (m = matches; m; m = m->next) {
1614 if (!m->used)
1615 continue;
1616
Rusty Russell228e98d2000-04-27 10:28:06 +00001617 memcpy(e->elems + size, m->m, m->m->u.match_size);
1618 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001619 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001620 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001621
1622 return e;
1623}
1624
1625int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1626{
1627 struct ipt_entry fw, *e = NULL;
1628 int invert = 0;
1629 unsigned int nsaddrs = 0, ndaddrs = 0;
1630 struct in_addr *saddrs = NULL, *daddrs = NULL;
1631
1632 int c, verbose = 0;
1633 const char *chain = NULL;
1634 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1635 const char *policy = NULL, *newname = NULL;
1636 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001637 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001638 int ret = 1;
1639 struct iptables_match *m;
1640 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001641 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001642 const char *jumpto = "";
1643 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001644 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001645
1646 memset(&fw, 0, sizeof(fw));
1647
Sven Kochfb1279a2001-02-27 12:25:12 +00001648 opts = original_opts;
1649 global_option_offset = 0;
1650
Harald Welteae1ff9f2000-12-01 14:26:20 +00001651 /* re-set optind to 0 in case do_command gets called
1652 * a second time */
1653 optind = 0;
1654
1655 /* clear mflags in case do_command gets called a second time
1656 * (we clear the global list of all matches for security)*/
1657 for (m = iptables_matches; m; m = m->next) {
1658 m->mflags = 0;
1659 m->used = 0;
1660 }
1661
1662 for (t = iptables_targets; t; t = t->next) {
1663 t->tflags = 0;
1664 t->used = 0;
1665 }
1666
Marc Bouchere6869a82000-03-20 06:03:29 +00001667 /* Suppress error messages: we may add new options if we
1668 demand-load a protocol. */
1669 opterr = 0;
1670
1671 while ((c = getopt_long(argc, argv,
Harald Welteccd49e52001-01-23 22:54:34 +00001672 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 opts, NULL)) != -1) {
1674 switch (c) {
1675 /*
1676 * Command selection
1677 */
1678 case 'A':
1679 add_command(&command, CMD_APPEND, CMD_NONE,
1680 invert);
1681 chain = optarg;
1682 break;
1683
1684 case 'D':
1685 add_command(&command, CMD_DELETE, CMD_NONE,
1686 invert);
1687 chain = optarg;
1688 if (optind < argc && argv[optind][0] != '-'
1689 && argv[optind][0] != '!') {
1690 rulenum = parse_rulenumber(argv[optind++]);
1691 command = CMD_DELETE_NUM;
1692 }
1693 break;
1694
1695 case 'C':
1696 add_command(&command, CMD_CHECK, CMD_NONE,
1697 invert);
1698 chain = optarg;
1699 break;
1700
1701 case 'R':
1702 add_command(&command, CMD_REPLACE, CMD_NONE,
1703 invert);
1704 chain = optarg;
1705 if (optind < argc && argv[optind][0] != '-'
1706 && argv[optind][0] != '!')
1707 rulenum = parse_rulenumber(argv[optind++]);
1708 else
1709 exit_error(PARAMETER_PROBLEM,
1710 "-%c requires a rule number",
1711 cmd2char(CMD_REPLACE));
1712 break;
1713
1714 case 'I':
1715 add_command(&command, CMD_INSERT, CMD_NONE,
1716 invert);
1717 chain = optarg;
1718 if (optind < argc && argv[optind][0] != '-'
1719 && argv[optind][0] != '!')
1720 rulenum = parse_rulenumber(argv[optind++]);
1721 else rulenum = 1;
1722 break;
1723
1724 case 'L':
1725 add_command(&command, CMD_LIST, CMD_ZERO,
1726 invert);
1727 if (optarg) chain = optarg;
1728 else if (optind < argc && argv[optind][0] != '-'
1729 && argv[optind][0] != '!')
1730 chain = argv[optind++];
1731 break;
1732
1733 case 'F':
1734 add_command(&command, CMD_FLUSH, CMD_NONE,
1735 invert);
1736 if (optarg) chain = optarg;
1737 else if (optind < argc && argv[optind][0] != '-'
1738 && argv[optind][0] != '!')
1739 chain = argv[optind++];
1740 break;
1741
1742 case 'Z':
1743 add_command(&command, CMD_ZERO, CMD_LIST,
1744 invert);
1745 if (optarg) chain = optarg;
1746 else if (optind < argc && argv[optind][0] != '-'
1747 && argv[optind][0] != '!')
1748 chain = argv[optind++];
1749 break;
1750
1751 case 'N':
1752 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1753 invert);
1754 chain = optarg;
1755 break;
1756
1757 case 'X':
1758 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1759 invert);
1760 if (optarg) chain = optarg;
1761 else if (optind < argc && argv[optind][0] != '-'
1762 && argv[optind][0] != '!')
1763 chain = argv[optind++];
1764 break;
1765
1766 case 'E':
1767 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1768 invert);
1769 chain = optarg;
1770 if (optind < argc && argv[optind][0] != '-'
1771 && argv[optind][0] != '!')
1772 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001773 else
1774 exit_error(PARAMETER_PROBLEM,
1775 "-%c requires old-chain-name and "
1776 "new-chain-name",
1777 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 break;
1779
1780 case 'P':
1781 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1782 invert);
1783 chain = optarg;
1784 if (optind < argc && argv[optind][0] != '-'
1785 && argv[optind][0] != '!')
1786 policy = argv[optind++];
1787 else
1788 exit_error(PARAMETER_PROBLEM,
1789 "-%c requires a chain and a policy",
1790 cmd2char(CMD_SET_POLICY));
1791 break;
1792
1793 case 'h':
1794 if (!optarg)
1795 optarg = argv[optind];
1796
Rusty Russell2e0a3212000-04-19 11:23:18 +00001797 /* iptables -p icmp -h */
1798 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001799 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001800
Marc Bouchere6869a82000-03-20 06:03:29 +00001801 exit_printhelp();
1802
1803 /*
1804 * Option selection
1805 */
1806 case 'p':
1807 if (check_inverse(optarg, &invert))
1808 optind++;
1809 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1810 invert);
1811
1812 /* Canonicalize into lower case */
1813 for (protocol = argv[optind-1]; *protocol; protocol++)
1814 *protocol = tolower(*protocol);
1815
1816 protocol = argv[optind-1];
1817 fw.ip.proto = parse_protocol(protocol);
1818
1819 if (fw.ip.proto == 0
1820 && (fw.ip.invflags & IPT_INV_PROTO))
1821 exit_error(PARAMETER_PROBLEM,
1822 "rule would never match protocol");
1823 fw.nfcache |= NFC_IP_PROTO;
1824 break;
1825
1826 case 's':
1827 if (check_inverse(optarg, &invert))
1828 optind++;
1829 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1830 invert);
1831 shostnetworkmask = argv[optind-1];
1832 fw.nfcache |= NFC_IP_SRC;
1833 break;
1834
1835 case 'd':
1836 if (check_inverse(optarg, &invert))
1837 optind++;
1838 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1839 invert);
1840 dhostnetworkmask = argv[optind-1];
1841 fw.nfcache |= NFC_IP_DST;
1842 break;
1843
1844 case 'j':
1845 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1846 invert);
1847 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001848 /* TRY_LOAD (may be chain name) */
1849 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001850
1851 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001852 size_t size;
1853
Rusty Russell73f72f52000-07-03 10:17:57 +00001854 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1855 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856
Rusty Russell2e0a3212000-04-19 11:23:18 +00001857 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001858 target->t->u.target_size = size;
1859 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001861 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 }
1863 break;
1864
1865
1866 case 'i':
1867 if (check_inverse(optarg, &invert))
1868 optind++;
1869 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1870 invert);
1871 parse_interface(argv[optind-1],
1872 fw.ip.iniface,
1873 fw.ip.iniface_mask);
1874 fw.nfcache |= NFC_IP_IF_IN;
1875 break;
1876
1877 case 'o':
1878 if (check_inverse(optarg, &invert))
1879 optind++;
1880 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1881 invert);
1882 parse_interface(argv[optind-1],
1883 fw.ip.outiface,
1884 fw.ip.outiface_mask);
1885 fw.nfcache |= NFC_IP_IF_OUT;
1886 break;
1887
1888 case 'f':
1889 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1890 invert);
1891 fw.ip.flags |= IPT_F_FRAG;
1892 fw.nfcache |= NFC_IP_FRAG;
1893 break;
1894
1895 case 'v':
1896 if (!verbose)
1897 set_option(&options, OPT_VERBOSE,
1898 &fw.ip.invflags, invert);
1899 verbose++;
1900 break;
1901
Rusty Russell52a51492000-05-02 16:44:29 +00001902 case 'm': {
1903 size_t size;
1904
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 if (invert)
1906 exit_error(PARAMETER_PROBLEM,
1907 "unexpected ! flag before --match");
1908
Rusty Russell52a51492000-05-02 16:44:29 +00001909 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001910 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1911 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001912 m->m = fw_calloc(1, size);
1913 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001914 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001915 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001916 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001917 }
1918 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001919
1920 case 'n':
1921 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1922 invert);
1923 break;
1924
1925 case 't':
1926 if (invert)
1927 exit_error(PARAMETER_PROBLEM,
1928 "unexpected ! flag before --table");
1929 *table = argv[optind-1];
1930 break;
1931
1932 case 'x':
1933 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1934 invert);
1935 break;
1936
1937 case 'V':
1938 if (invert)
1939 printf("Not %s ;-)\n", program_version);
1940 else
1941 printf("%s v%s\n",
1942 program_name, program_version);
1943 exit(0);
1944
1945 case '0':
1946 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1947 invert);
1948 break;
1949
Harald Welte82dd2ec2000-12-19 05:18:15 +00001950 case 'M':
1951 modprobe = optarg;
1952 break;
1953
Harald Welteccd49e52001-01-23 22:54:34 +00001954 case 'c':
1955
1956 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1957 invert);
1958 pcnt = optarg;
1959 if (optind < argc && argv[optind][0] != '-'
1960 && argv[optind][0] != '!')
1961 bcnt = argv[optind++];
1962 else
1963 exit_error(PARAMETER_PROBLEM,
1964 "-%c requires packet and byte counter",
1965 opt2char(OPT_COUNTERS));
1966
1967 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1968 exit_error(PARAMETER_PROBLEM,
1969 "-%c packet counter not numeric",
1970 opt2char(OPT_COUNTERS));
1971
1972 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1973 exit_error(PARAMETER_PROBLEM,
1974 "-%c byte counter not numeric",
1975 opt2char(OPT_COUNTERS));
1976
1977 break;
1978
1979
Marc Bouchere6869a82000-03-20 06:03:29 +00001980 case 1: /* non option */
1981 if (optarg[0] == '!' && optarg[1] == '\0') {
1982 if (invert)
1983 exit_error(PARAMETER_PROBLEM,
1984 "multiple consecutive ! not"
1985 " allowed");
1986 invert = TRUE;
1987 optarg[0] = '\0';
1988 continue;
1989 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001990 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001991 exit_tryhelp(2);
1992
1993 default:
1994 /* FIXME: This scheme doesn't allow two of the same
1995 matches --RR */
1996 if (!target
1997 || !(target->parse(c - target->option_offset,
1998 argv, invert,
1999 &target->tflags,
2000 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002001 for (m = iptables_matches; m; m = m->next) {
2002 if (!m->used)
2003 continue;
2004
Marc Bouchere6869a82000-03-20 06:03:29 +00002005 if (m->parse(c - m->option_offset,
2006 argv, invert,
2007 &m->mflags,
2008 &fw,
2009 &fw.nfcache,
2010 &m->m))
2011 break;
2012 }
2013
2014 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00002015 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00002016 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00002017 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00002018 && !find_proto(protocol, DONT_LOAD,
2019 options&OPT_NUMERIC)
2020 && (m = find_proto(protocol, TRY_LOAD,
2021 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002022 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00002023 size_t size;
2024
Rusty Russell73f72f52000-07-03 10:17:57 +00002025 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2026 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002027
Rusty Russell2e0a3212000-04-19 11:23:18 +00002028 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002029 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002030 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002031 m->init(m->m, &fw.nfcache);
2032
Sven Kochdbe857a2001-03-02 09:41:08 +00002033 opts = merge_options(opts,
2034 m->extra_opts, &m->option_offset);
2035
Marc Bouchere6869a82000-03-20 06:03:29 +00002036 optind--;
2037 continue;
2038 }
2039 if (!m)
2040 exit_error(PARAMETER_PROBLEM,
2041 "Unknown arg `%s'",
2042 argv[optind-1]);
2043 }
2044 }
2045 invert = FALSE;
2046 }
2047
Sven Kochfb1279a2001-02-27 12:25:12 +00002048 for (m = iptables_matches; m; m = m->next) {
2049 if (!m->used)
2050 continue;
2051
Marc Bouchere6869a82000-03-20 06:03:29 +00002052 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002053 }
2054
Marc Bouchere6869a82000-03-20 06:03:29 +00002055 if (target)
2056 target->final_check(target->tflags);
2057
2058 /* Fix me: must put inverse options checking here --MN */
2059
2060 if (optind < argc)
2061 exit_error(PARAMETER_PROBLEM,
2062 "unknown arguments found on commandline");
2063 if (!command)
2064 exit_error(PARAMETER_PROBLEM, "no command specified");
2065 if (invert)
2066 exit_error(PARAMETER_PROBLEM,
2067 "nothing appropriate following !");
2068
Marc Boucher744bd022000-04-22 22:36:10 +00002069 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2070 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002071 if (!(options & OPT_DESTINATION))
2072 dhostnetworkmask = "0.0.0.0/0";
2073 if (!(options & OPT_SOURCE))
2074 shostnetworkmask = "0.0.0.0/0";
2075 }
2076
2077 if (shostnetworkmask)
2078 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2079 &(fw.ip.smsk), &nsaddrs);
2080
2081 if (dhostnetworkmask)
2082 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2083 &(fw.ip.dmsk), &ndaddrs);
2084
2085 if ((nsaddrs > 1 || ndaddrs > 1) &&
2086 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2087 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2088 " source or destination IP addresses");
2089
2090 if (command == CMD_CHECK && fw.ip.invflags != 0)
2091 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2092 cmd2char(CMD_CHECK));
2093
2094 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2095 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2096 "specify a unique address");
2097
2098 generic_opt_check(command, options);
2099
2100 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2101 exit_error(PARAMETER_PROBLEM,
2102 "chain name `%s' too long (must be under %i chars)",
2103 chain, IPT_FUNCTION_MAXNAMELEN);
2104
Harald Welteae1ff9f2000-12-01 14:26:20 +00002105 /* only allocate handle if we weren't called with a handle */
2106 if (!*handle)
2107 *handle = iptc_init(*table);
2108
Harald Welte82dd2ec2000-12-19 05:18:15 +00002109 if (!*handle) {
2110 /* try to insmod the module if iptc_init failed */
2111 iptables_insmod("ip_tables", modprobe);
2112 *handle = iptc_init(*table);
2113 }
2114
Marc Bouchere6869a82000-03-20 06:03:29 +00002115 if (!*handle)
2116 exit_error(VERSION_PROBLEM,
2117 "can't initialize iptables table `%s': %s",
2118 *table, iptc_strerror(errno));
2119
Marc Boucher744bd022000-04-22 22:36:10 +00002120 if (command == CMD_CHECK
2121 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002122 || command == CMD_DELETE
2123 || command == CMD_INSERT
2124 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002125 if (strcmp(chain, "PREROUTING") == 0
2126 || strcmp(chain, "INPUT") == 0) {
2127 /* -o not valid with incoming packets. */
2128 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002129 exit_error(PARAMETER_PROBLEM,
2130 "Can't use -%c with %s\n",
2131 opt2char(OPT_VIANAMEOUT),
2132 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002133 /* -i required with -C */
2134 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2135 exit_error(PARAMETER_PROBLEM,
2136 "Need -%c with %s\n",
2137 opt2char(OPT_VIANAMEIN),
2138 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002139 }
2140
Rusty Russella4860fd2000-06-17 16:13:02 +00002141 if (strcmp(chain, "POSTROUTING") == 0
2142 || strcmp(chain, "OUTPUT") == 0) {
2143 /* -i not valid with outgoing packets */
2144 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002145 exit_error(PARAMETER_PROBLEM,
2146 "Can't use -%c with %s\n",
2147 opt2char(OPT_VIANAMEIN),
2148 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002149 /* -o required with -C */
2150 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2151 exit_error(PARAMETER_PROBLEM,
2152 "Need -%c with %s\n",
2153 opt2char(OPT_VIANAMEOUT),
2154 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002155 }
2156
2157 if (target && iptc_is_chain(jumpto, *handle)) {
2158 printf("Warning: using chain %s, not extension\n",
2159 jumpto);
2160
2161 target = NULL;
2162 }
2163
2164 /* If they didn't specify a target, or it's a chain
2165 name, use standard. */
2166 if (!target
2167 && (strlen(jumpto) == 0
2168 || iptc_is_chain(jumpto, *handle))) {
2169 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002170
Rusty Russell52a51492000-05-02 16:44:29 +00002171 target = find_target(IPT_STANDARD_TARGET,
2172 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002173
2174 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002175 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002176 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002177 target->t->u.target_size = size;
2178 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002179 target->init(target->t, &fw.nfcache);
2180 }
2181
Rusty Russell7e53bf92000-03-20 07:03:28 +00002182 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002183 /* it is no chain, and we can't load a plugin.
2184 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002185 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002186 * chain. */
2187 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002188 } else {
2189 e = generate_entry(&fw, iptables_matches, target->t);
2190 }
2191 }
2192
2193 switch (command) {
2194 case CMD_APPEND:
2195 ret = append_entry(chain, e,
2196 nsaddrs, saddrs, ndaddrs, daddrs,
2197 options&OPT_VERBOSE,
2198 handle);
2199 break;
2200 case CMD_CHECK:
2201 ret = check_packet(chain, e,
2202 nsaddrs, saddrs, ndaddrs, daddrs,
2203 options&OPT_VERBOSE, handle);
2204 break;
2205 case CMD_DELETE:
2206 ret = delete_entry(chain, e,
2207 nsaddrs, saddrs, ndaddrs, daddrs,
2208 options&OPT_VERBOSE,
2209 handle);
2210 break;
2211 case CMD_DELETE_NUM:
2212 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2213 break;
2214 case CMD_REPLACE:
2215 ret = replace_entry(chain, e, rulenum - 1,
2216 saddrs, daddrs, options&OPT_VERBOSE,
2217 handle);
2218 break;
2219 case CMD_INSERT:
2220 ret = insert_entry(chain, e, rulenum - 1,
2221 nsaddrs, saddrs, ndaddrs, daddrs,
2222 options&OPT_VERBOSE,
2223 handle);
2224 break;
2225 case CMD_LIST:
2226 ret = list_entries(chain,
2227 options&OPT_VERBOSE,
2228 options&OPT_NUMERIC,
2229 options&OPT_EXPANDED,
2230 options&OPT_LINENUMBERS,
2231 handle);
2232 break;
2233 case CMD_FLUSH:
2234 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2235 break;
2236 case CMD_ZERO:
2237 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2238 break;
2239 case CMD_LIST|CMD_ZERO:
2240 ret = list_entries(chain,
2241 options&OPT_VERBOSE,
2242 options&OPT_NUMERIC,
2243 options&OPT_EXPANDED,
2244 options&OPT_LINENUMBERS,
2245 handle);
2246 if (ret)
2247 ret = zero_entries(chain,
2248 options&OPT_VERBOSE, handle);
2249 break;
2250 case CMD_NEW_CHAIN:
2251 ret = iptc_create_chain(chain, handle);
2252 break;
2253 case CMD_DELETE_CHAIN:
2254 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2255 break;
2256 case CMD_RENAME_CHAIN:
2257 ret = iptc_rename_chain(chain, newname, handle);
2258 break;
2259 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002260 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002261 break;
2262 default:
2263 /* We should never reach this... */
2264 exit_tryhelp(2);
2265 }
2266
2267 if (verbose > 1)
2268 dump_entries(*handle);
2269
2270 return ret;
2271}