blob: c570d3afa26da2a398ba6fd8b244c12a4740fe30 [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... */
210#ifndef IPPROTO_ESP
211#define IPPROTO_ESP 50
212#endif
213#ifndef IPPROTO_AH
214#define IPPROTO_AH 51
215#endif
216
Marc Bouchere6869a82000-03-20 06:03:29 +0000217static const struct pprot chain_protos[] = {
218 { "tcp", IPPROTO_TCP },
219 { "udp", IPPROTO_UDP },
220 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000221 { "esp", IPPROTO_ESP },
222 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000223 { "all", 0 },
224};
225
226static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000227proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000228{
229 unsigned int i;
230
Rusty Russell28381a42000-05-10 00:19:50 +0000231 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000232 struct protoent *pent = getprotobynumber(proto);
233 if (pent)
234 return pent->p_name;
235 }
236
237 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
238 if (chain_protos[i].num == proto)
239 return chain_protos[i].name;
240
241 return NULL;
242}
243
244struct in_addr *
245dotted_to_addr(const char *dotted)
246{
247 static struct in_addr addr;
248 unsigned char *addrp;
249 char *p, *q;
250 int onebyte, i;
251 char buf[20];
252
253 /* copy dotted string, because we need to modify it */
254 strncpy(buf, dotted, sizeof(buf) - 1);
255 addrp = (unsigned char *) &(addr.s_addr);
256
257 p = buf;
258 for (i = 0; i < 3; i++) {
259 if ((q = strchr(p, '.')) == NULL)
260 return (struct in_addr *) NULL;
261
262 *q = '\0';
263 if ((onebyte = string_to_number(p, 0, 255)) == -1)
264 return (struct in_addr *) NULL;
265
266 addrp[i] = (unsigned char) onebyte;
267 p = q + 1;
268 }
269
270 /* we've checked 3 bytes, now we check the last one */
271 if ((onebyte = string_to_number(p, 0, 255)) == -1)
272 return (struct in_addr *) NULL;
273
274 addrp[3] = (unsigned char) onebyte;
275
276 return &addr;
277}
278
279static struct in_addr *
280network_to_addr(const char *name)
281{
282 struct netent *net;
283 static struct in_addr addr;
284
285 if ((net = getnetbyname(name)) != NULL) {
286 if (net->n_addrtype != AF_INET)
287 return (struct in_addr *) NULL;
288 addr.s_addr = htonl((unsigned long) net->n_net);
289 return &addr;
290 }
291
292 return (struct in_addr *) NULL;
293}
294
295static void
296inaddrcpy(struct in_addr *dst, struct in_addr *src)
297{
298 /* memcpy(dst, src, sizeof(struct in_addr)); */
299 dst->s_addr = src->s_addr;
300}
301
302void
303exit_error(enum exittype status, char *msg, ...)
304{
305 va_list args;
306
307 va_start(args, msg);
308 fprintf(stderr, "%s v%s: ", program_name, program_version);
309 vfprintf(stderr, msg, args);
310 va_end(args);
311 fprintf(stderr, "\n");
312 if (status == PARAMETER_PROBLEM)
313 exit_tryhelp(status);
314 if (status == VERSION_PROBLEM)
315 fprintf(stderr,
316 "Perhaps iptables or your kernel needs to be upgraded.\n");
317 exit(status);
318}
319
320void
321exit_tryhelp(int status)
322{
323 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
324 program_name, program_name );
325 exit(status);
326}
327
328void
329exit_printhelp(void)
330{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000331 struct iptables_match *m = NULL;
332 struct iptables_target *t = NULL;
333
Marc Bouchere6869a82000-03-20 06:03:29 +0000334 printf("%s v%s\n\n"
335"Usage: %s -[ADC] chain rule-specification [options]\n"
336" %s -[RI] chain rulenum rule-specification [options]\n"
337" %s -D chain rulenum [options]\n"
338" %s -[LFZ] [chain] [options]\n"
339" %s -[NX] chain\n"
340" %s -E old-chain-name new-chain-name\n"
341" %s -P chain target [options]\n"
342" %s -h (print this help information)\n\n",
343 program_name, program_version, program_name, program_name,
344 program_name, program_name, program_name, program_name,
345 program_name, program_name);
346
347 printf(
348"Commands:\n"
349"Either long or short options are allowed.\n"
350" --append -A chain Append to chain\n"
351" --delete -D chain Delete matching rule from chain\n"
352" --delete -D chain rulenum\n"
353" Delete rule rulenum (1 = first) from chain\n"
354" --insert -I chain [rulenum]\n"
355" Insert in chain as rulenum (default 1=first)\n"
356" --replace -R chain rulenum\n"
357" Replace rule rulenum (1 = first) in chain\n"
358" --list -L [chain] List the rules in a chain or all chains\n"
359" --flush -F [chain] Delete all rules in chain or all chains\n"
360" --zero -Z [chain] Zero counters in chain or all chains\n"
361" --check -C chain Test this packet on chain\n"
362" --new -N chain Create a new user-defined chain\n"
363" --delete-chain\n"
364" -X [chain] Delete a user-defined chain\n"
365" --policy -P chain target\n"
366" Change policy on chain to target\n"
367" --rename-chain\n"
368" -E old-chain new-chain\n"
369" Change chain name, (moving any references)\n"
370
371"Options:\n"
372" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
373" --source -s [!] address[/mask]\n"
374" source specification\n"
375" --destination -d [!] address[/mask]\n"
376" destination specification\n"
377" --in-interface -i [!] input name[+]\n"
378" network interface name ([+] for wildcard)\n"
379" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000380" target for rule (may load target extension)\n"
381" --match -m match\n"
382" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000383" --numeric -n numeric output of addresses and ports\n"
384" --out-interface -o [!] output name[+]\n"
385" network interface name ([+] for wildcard)\n"
386" --table -t table table to manipulate (default: `filter')\n"
387" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000388" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000389" --exact -x expand numbers (display exact values)\n"
390"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000391" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000392" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000393"[!] --version -V print package version.\n");
394
Rusty Russell363112d2000-08-11 13:49:26 +0000395 /* Print out any special helps. A user might like to be able
396 to add a --help to the commandline, and see expected
397 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000398 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000399 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000400 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000401 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000402 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000404 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000406 exit(0);
407}
408
409static void
410generic_opt_check(int command, int options)
411{
412 int i, j, legal = 0;
413
414 /* Check that commands are valid with options. Complicated by the
415 * fact that if an option is legal with *any* command given, it is
416 * legal overall (ie. -z and -l).
417 */
418 for (i = 0; i < NUMBER_OF_OPT; i++) {
419 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
420
421 for (j = 0; j < NUMBER_OF_CMD; j++) {
422 if (!(command & (1<<j)))
423 continue;
424
425 if (!(options & (1<<i))) {
426 if (commands_v_options[j][i] == '+')
427 exit_error(PARAMETER_PROBLEM,
428 "You need to supply the `-%c' "
429 "option for this command\n",
430 optflags[i]);
431 } else {
432 if (commands_v_options[j][i] != 'x')
433 legal = 1;
434 else if (legal == 0)
435 legal = -1;
436 }
437 }
438 if (legal == -1)
439 exit_error(PARAMETER_PROBLEM,
440 "Illegal option `-%c' with this command\n",
441 optflags[i]);
442 }
443}
444
445static char
446opt2char(int option)
447{
448 const char *ptr;
449 for (ptr = optflags; option > 1; option >>= 1, ptr++);
450
451 return *ptr;
452}
453
454static char
455cmd2char(int option)
456{
457 const char *ptr;
458 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
459
460 return *ptr;
461}
462
463static void
464add_command(int *cmd, const int newcmd, const int othercmds, int invert)
465{
466 if (invert)
467 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
468 if (*cmd & (~othercmds))
469 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
470 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
471 *cmd |= newcmd;
472}
473
474int
475check_inverse(const char option[], int *invert)
476{
477 if (option && strcmp(option, "!") == 0) {
478 if (*invert)
479 exit_error(PARAMETER_PROBLEM,
480 "Multiple `!' flags not allowed");
481
482 *invert = TRUE;
483 return TRUE;
484 }
485 return FALSE;
486}
487
488static void *
489fw_calloc(size_t count, size_t size)
490{
491 void *p;
492
493 if ((p = calloc(count, size)) == NULL) {
494 perror("iptables: calloc failed");
495 exit(1);
496 }
497 return p;
498}
499
500static void *
501fw_malloc(size_t size)
502{
503 void *p;
504
505 if ((p = malloc(size)) == NULL) {
506 perror("iptables: malloc failed");
507 exit(1);
508 }
509 return p;
510}
511
512static struct in_addr *
513host_to_addr(const char *name, unsigned int *naddr)
514{
515 struct hostent *host;
516 struct in_addr *addr;
517 unsigned int i;
518
519 *naddr = 0;
520 if ((host = gethostbyname(name)) != NULL) {
521 if (host->h_addrtype != AF_INET ||
522 host->h_length != sizeof(struct in_addr))
523 return (struct in_addr *) NULL;
524
525 while (host->h_addr_list[*naddr] != (char *) NULL)
526 (*naddr)++;
527 addr = fw_calloc(*naddr, sizeof(struct in_addr));
528 for (i = 0; i < *naddr; i++)
529 inaddrcpy(&(addr[i]),
530 (struct in_addr *) host->h_addr_list[i]);
531 return addr;
532 }
533
534 return (struct in_addr *) NULL;
535}
536
537static char *
538addr_to_host(const struct in_addr *addr)
539{
540 struct hostent *host;
541
542 if ((host = gethostbyaddr((char *) addr,
543 sizeof(struct in_addr), AF_INET)) != NULL)
544 return (char *) host->h_name;
545
546 return (char *) NULL;
547}
548
549/*
550 * All functions starting with "parse" should succeed, otherwise
551 * the program fails.
552 * Most routines return pointers to static data that may change
553 * between calls to the same or other routines with a few exceptions:
554 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
555 * return global static data.
556*/
557
558static struct in_addr *
559parse_hostnetwork(const char *name, unsigned int *naddrs)
560{
561 struct in_addr *addrp, *addrptmp;
562
563 if ((addrptmp = dotted_to_addr(name)) != NULL ||
564 (addrptmp = network_to_addr(name)) != NULL) {
565 addrp = fw_malloc(sizeof(struct in_addr));
566 inaddrcpy(addrp, addrptmp);
567 *naddrs = 1;
568 return addrp;
569 }
570 if ((addrp = host_to_addr(name, naddrs)) != NULL)
571 return addrp;
572
573 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
574}
575
576static struct in_addr *
577parse_mask(char *mask)
578{
579 static struct in_addr maskaddr;
580 struct in_addr *addrp;
581 int bits;
582
583 if (mask == NULL) {
584 /* no mask at all defaults to 32 bits */
585 maskaddr.s_addr = 0xFFFFFFFF;
586 return &maskaddr;
587 }
588 if ((addrp = dotted_to_addr(mask)) != NULL)
589 /* dotted_to_addr already returns a network byte order addr */
590 return addrp;
591 if ((bits = string_to_number(mask, 0, 32)) == -1)
592 exit_error(PARAMETER_PROBLEM,
593 "invalid mask `%s' specified", mask);
594 if (bits != 0) {
595 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
596 return &maskaddr;
597 }
598
599 maskaddr.s_addr = 0L;
600 return &maskaddr;
601}
602
603static void
604parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
605 struct in_addr *maskp, unsigned int *naddrs)
606{
607 struct in_addr *addrp;
608 char buf[256];
609 char *p;
610 int i, j, k, n;
611
612 strncpy(buf, name, sizeof(buf) - 1);
613 if ((p = strrchr(buf, '/')) != NULL) {
614 *p = '\0';
615 addrp = parse_mask(p + 1);
616 } else
617 addrp = parse_mask(NULL);
618 inaddrcpy(maskp, addrp);
619
620 /* if a null mask is given, the name is ignored, like in "any/0" */
621 if (maskp->s_addr == 0L)
622 strcpy(buf, "0.0.0.0");
623
624 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
625 n = *naddrs;
626 for (i = 0, j = 0; i < n; i++) {
627 addrp[j++].s_addr &= maskp->s_addr;
628 for (k = 0; k < j - 1; k++) {
629 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
630 (*naddrs)--;
631 j--;
632 break;
633 }
634 }
635 }
636}
637
638struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000639find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000640{
641 struct iptables_match *ptr;
642
643 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
644 if (strcmp(name, ptr->name) == 0)
645 break;
646 }
647
Rusty Russell52a51492000-05-02 16:44:29 +0000648 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000649 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
650 + strlen(name)];
651 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000652 if (dlopen(path, RTLD_NOW)) {
653 /* Found library. If it didn't register itself,
654 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000655 ptr = find_match(name, DONT_LOAD);
656
Rusty Russell9e1d2142000-04-23 09:11:12 +0000657 if (!ptr)
658 exit_error(PARAMETER_PROBLEM,
659 "Couldn't load match `%s'\n",
660 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000661 } else if (tryload == LOAD_MUST_SUCCEED)
662 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000663 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000664 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000665 }
666
Harald Welteae1ff9f2000-12-01 14:26:20 +0000667 if (ptr)
668 ptr->used = 1;
669
Marc Bouchere6869a82000-03-20 06:03:29 +0000670 return ptr;
671}
672
Rusty Russell28381a42000-05-10 00:19:50 +0000673/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
674static struct iptables_match *
675find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
676{
677 int proto;
678
679 proto = string_to_number(pname, 0, 255);
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000680 if (proto != -1)
Rusty Russell28381a42000-05-10 00:19:50 +0000681 return find_match(proto_to_name(proto, nolookup), tryload);
682
683 return find_match(pname, tryload);
684}
685
Marc Bouchere6869a82000-03-20 06:03:29 +0000686static u_int16_t
687parse_protocol(const char *s)
688{
Rusty Russell28381a42000-05-10 00:19:50 +0000689 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000690
691 if (proto == -1) {
692 struct protoent *pent;
693
694 if ((pent = getprotobyname(s)))
695 proto = pent->p_proto;
696 else {
697 unsigned int i;
698 for (i = 0;
699 i < sizeof(chain_protos)/sizeof(struct pprot);
700 i++) {
701 if (strcmp(s, chain_protos[i].name) == 0) {
702 proto = chain_protos[i].num;
703 break;
704 }
705 }
706 if (i == sizeof(chain_protos)/sizeof(struct pprot))
707 exit_error(PARAMETER_PROBLEM,
708 "unknown protocol `%s' specified",
709 s);
710 }
711 }
712
713 return (u_int16_t)proto;
714}
715
716static void
717parse_interface(const char *arg, char *vianame, unsigned char *mask)
718{
719 int vialen = strlen(arg);
720 unsigned int i;
721
722 memset(mask, 0, IFNAMSIZ);
723 memset(vianame, 0, IFNAMSIZ);
724
725 if (vialen + 1 > IFNAMSIZ)
726 exit_error(PARAMETER_PROBLEM,
727 "interface name `%s' must be shorter than IFNAMSIZ"
728 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000729
Marc Bouchere6869a82000-03-20 06:03:29 +0000730 strcpy(vianame, arg);
731 if (vialen == 0)
732 memset(mask, 0, IFNAMSIZ);
733 else if (vianame[vialen - 1] == '+') {
734 memset(mask, 0xFF, vialen - 1);
735 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
736 /* Remove `+' */
737 vianame[vialen - 1] = '\0';
738 } else {
739 /* Include nul-terminator in match */
740 memset(mask, 0xFF, vialen + 1);
741 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
742 }
743 for (i = 0; vianame[i]; i++) {
744 if (!isalnum(vianame[i])) {
745 printf("Warning: wierd character in interface"
746 " `%s' (No aliases, :, ! or *).\n",
747 vianame);
748 break;
749 }
750 }
751}
752
753/* Can't be zero. */
754static int
755parse_rulenumber(const char *rule)
756{
757 int rulenum = string_to_number(rule, 1, INT_MAX);
758
759 if (rulenum == -1)
760 exit_error(PARAMETER_PROBLEM,
761 "Invalid rule number `%s'", rule);
762
763 return rulenum;
764}
765
766static const char *
767parse_target(const char *targetname)
768{
769 const char *ptr;
770
771 if (strlen(targetname) < 1)
772 exit_error(PARAMETER_PROBLEM,
773 "Invalid target name (too short)");
774
775 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
776 exit_error(PARAMETER_PROBLEM,
777 "Invalid target name `%s' (%i chars max)",
778 targetname, sizeof(ipt_chainlabel)-1);
779
780 for (ptr = targetname; *ptr; ptr++)
781 if (isspace(*ptr))
782 exit_error(PARAMETER_PROBLEM,
783 "Invalid target name `%s'", targetname);
784 return targetname;
785}
786
787static char *
788addr_to_network(const struct in_addr *addr)
789{
790 struct netent *net;
791
792 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
793 return (char *) net->n_name;
794
795 return (char *) NULL;
796}
797
798char *
799addr_to_dotted(const struct in_addr *addrp)
800{
801 static char buf[20];
802 const unsigned char *bytep;
803
804 bytep = (const unsigned char *) &(addrp->s_addr);
805 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
806 return buf;
807}
808static char *
809addr_to_anyname(const struct in_addr *addr)
810{
811 char *name;
812
813 if ((name = addr_to_host(addr)) != NULL ||
814 (name = addr_to_network(addr)) != NULL)
815 return name;
816
817 return addr_to_dotted(addr);
818}
819
820static char *
821mask_to_dotted(const struct in_addr *mask)
822{
823 int i;
824 static char buf[20];
825 u_int32_t maskaddr, bits;
826
827 maskaddr = ntohl(mask->s_addr);
828
829 if (maskaddr == 0xFFFFFFFFL)
830 /* we don't want to see "/32" */
831 return "";
832
833 i = 32;
834 bits = 0xFFFFFFFEL;
835 while (--i >= 0 && maskaddr != bits)
836 bits <<= 1;
837 if (i >= 0)
838 sprintf(buf, "/%d", i);
839 else
840 /* mask was not a decent combination of 1's and 0's */
841 sprintf(buf, "/%s", addr_to_dotted(mask));
842
843 return buf;
844}
845
846int
847string_to_number(const char *s, int min, int max)
848{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000849 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000850 char *end;
851
852 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000853 errno = 0;
854 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000855 if (*end == '\0' && end != s) {
856 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000857 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000858 return number;
859 }
860 return -1;
861}
862
863static void
864set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
865 int invert)
866{
867 if (*options & option)
868 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
869 opt2char(option));
870 *options |= option;
871
872 if (invert) {
873 unsigned int i;
874 for (i = 0; 1 << i != option; i++);
875
876 if (!inverse_for_options[i])
877 exit_error(PARAMETER_PROBLEM,
878 "cannot have ! before -%c",
879 opt2char(option));
880 *invflg |= inverse_for_options[i];
881 }
882}
883
884struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000885find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000886{
887 struct iptables_target *ptr;
888
889 /* Standard target? */
890 if (strcmp(name, "") == 0
891 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
892 || strcmp(name, IPTC_LABEL_DROP) == 0
893 || strcmp(name, IPTC_LABEL_QUEUE) == 0
894 || strcmp(name, IPTC_LABEL_RETURN) == 0)
895 name = "standard";
896
897 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
898 if (strcmp(name, ptr->name) == 0)
899 break;
900 }
901
Rusty Russell52a51492000-05-02 16:44:29 +0000902 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000903 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
904 + strlen(name)];
905 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000906 if (dlopen(path, RTLD_NOW)) {
907 /* Found library. If it didn't register itself,
908 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000909 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000910 if (!ptr)
911 exit_error(PARAMETER_PROBLEM,
912 "Couldn't load target `%s'\n",
913 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000914 } else if (tryload == LOAD_MUST_SUCCEED)
915 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000916 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000917 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000918 }
919
Harald Welteae1ff9f2000-12-01 14:26:20 +0000920 if (ptr)
921 ptr->used = 1;
922
Marc Bouchere6869a82000-03-20 06:03:29 +0000923 return ptr;
924}
925
926static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000927merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 unsigned int *option_offset)
929{
930 unsigned int num_old, num_new, i;
931 struct option *merge;
932
933 for (num_old = 0; oldopts[num_old].name; num_old++);
934 for (num_new = 0; newopts[num_new].name; num_new++);
935
936 global_option_offset += OPTION_OFFSET;
937 *option_offset = global_option_offset;
938
939 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
940 memcpy(merge, oldopts, num_old * sizeof(struct option));
941 for (i = 0; i < num_new; i++) {
942 merge[num_old + i] = newopts[i];
943 merge[num_old + i].val += *option_offset;
944 }
945 memset(merge + num_old + num_new, 0, sizeof(struct option));
946
947 return merge;
948}
949
950void
951register_match(struct iptables_match *me)
952{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000953 struct iptables_match **i;
954
Marc Bouchere6869a82000-03-20 06:03:29 +0000955 if (strcmp(me->version, program_version) != 0) {
956 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
957 program_name, me->name, me->version, program_version);
958 exit(1);
959 }
960
Rusty Russell52a51492000-05-02 16:44:29 +0000961 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000962 fprintf(stderr, "%s: match `%s' already registered.\n",
963 program_name, me->name);
964 exit(1);
965 }
966
Rusty Russell73f72f52000-07-03 10:17:57 +0000967 if (me->size != IPT_ALIGN(me->size)) {
968 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
969 program_name, me->name, me->size);
970 exit(1);
971 }
972
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000973 /* Append to list. */
974 for (i = &iptables_matches; *i; i = &(*i)->next);
975 me->next = NULL;
976 *i = me;
977
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 me->m = NULL;
979 me->mflags = 0;
980
981 opts = merge_options(opts, me->extra_opts, &me->option_offset);
982}
983
984void
985register_target(struct iptables_target *me)
986{
987 if (strcmp(me->version, program_version) != 0) {
988 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
989 program_name, me->name, me->version, program_version);
990 exit(1);
991 }
992
Rusty Russell52a51492000-05-02 16:44:29 +0000993 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000994 fprintf(stderr, "%s: target `%s' already registered.\n",
995 program_name, me->name);
996 exit(1);
997 }
998
Rusty Russell73f72f52000-07-03 10:17:57 +0000999 if (me->size != IPT_ALIGN(me->size)) {
1000 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1001 program_name, me->name, me->size);
1002 exit(1);
1003 }
1004
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 /* Prepend to list. */
1006 me->next = iptables_targets;
1007 iptables_targets = me;
1008 me->t = NULL;
1009 me->tflags = 0;
1010
1011 opts = merge_options(opts, me->extra_opts, &me->option_offset);
1012}
1013
1014static void
1015print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1016{
1017 struct ipt_counters counters;
1018 const char *pol = iptc_get_policy(chain, &counters, handle);
1019 printf("Chain %s", chain);
1020 if (pol) {
1021 printf(" (policy %s", pol);
1022 if (!(format & FMT_NOCOUNTS))
1023 printf(" %llu packets, %llu bytes",
1024 counters.pcnt, counters.bcnt);
1025 printf(")\n");
1026 } else {
1027 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001028 if (!iptc_get_references(&refs, chain, handle))
1029 printf(" (ERROR obtaining refs)\n");
1030 else
1031 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001032 }
1033
1034 if (format & FMT_LINENUMBERS)
1035 printf(FMT("%-4s ", "%s "), "num");
1036 if (!(format & FMT_NOCOUNTS)) {
1037 if (format & FMT_KILOMEGAGIGA) {
1038 printf(FMT("%5s ","%s "), "pkts");
1039 printf(FMT("%5s ","%s "), "bytes");
1040 } else {
1041 printf(FMT("%8s ","%s "), "pkts");
1042 printf(FMT("%10s ","%s "), "bytes");
1043 }
1044 }
1045 if (!(format & FMT_NOTARGET))
1046 printf(FMT("%-9s ","%s "), "target");
1047 fputs(" prot ", stdout);
1048 if (format & FMT_OPTIONS)
1049 fputs("opt", stdout);
1050 if (format & FMT_VIA) {
1051 printf(FMT(" %-6s ","%s "), "in");
1052 printf(FMT("%-6s ","%s "), "out");
1053 }
1054 printf(FMT(" %-19s ","%s "), "source");
1055 printf(FMT(" %-19s "," %s "), "destination");
1056 printf("\n");
1057}
1058
1059static void
1060print_num(u_int64_t number, unsigned int format)
1061{
1062 if (format & FMT_KILOMEGAGIGA) {
1063 if (number > 99999) {
1064 number = (number + 500) / 1000;
1065 if (number > 9999) {
1066 number = (number + 500) / 1000;
1067 if (number > 9999) {
1068 number = (number + 500) / 1000;
1069 printf(FMT("%4lluG ","%lluG "),number);
1070 }
1071 else printf(FMT("%4lluM ","%lluM "), number);
1072 } else
1073 printf(FMT("%4lluK ","%lluK "), number);
1074 } else
1075 printf(FMT("%5llu ","%llu "), number);
1076 } else
1077 printf(FMT("%8llu ","%llu "), number);
1078}
1079
1080static int
1081print_match(const struct ipt_entry_match *m,
1082 const struct ipt_ip *ip,
1083 int numeric)
1084{
Rusty Russell52a51492000-05-02 16:44:29 +00001085 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001086
1087 if (match) {
1088 if (match->print)
1089 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001090 else
Rusty Russellb039b022000-09-01 06:04:05 +00001091 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001092 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001093 if (m->u.user.name[0])
1094 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001095 }
1096 /* Don't stop iterating. */
1097 return 0;
1098}
1099
1100/* e is called `fw' here for hysterical raisins */
1101static void
1102print_firewall(const struct ipt_entry *fw,
1103 const char *targname,
1104 unsigned int num,
1105 unsigned int format,
1106 const iptc_handle_t handle)
1107{
1108 struct iptables_target *target = NULL;
1109 const struct ipt_entry_target *t;
1110 u_int8_t flags;
1111 char buf[BUFSIZ];
1112
1113 /* User creates a chain called "REJECT": this overrides the
1114 `REJECT' target module. Keep feeding them rope until the
1115 revolution... Bwahahahahah */
1116 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001117 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001118 else
Rusty Russell52a51492000-05-02 16:44:29 +00001119 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001120
1121 t = ipt_get_target((struct ipt_entry *)fw);
1122 flags = fw->ip.flags;
1123
1124 if (format & FMT_LINENUMBERS)
1125 printf(FMT("%-4u ", "%u "), num+1);
1126
1127 if (!(format & FMT_NOCOUNTS)) {
1128 print_num(fw->counters.pcnt, format);
1129 print_num(fw->counters.bcnt, format);
1130 }
1131
1132 if (!(format & FMT_NOTARGET))
1133 printf(FMT("%-9s ", "%s "), targname);
1134
1135 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1136 {
Rusty Russell28381a42000-05-10 00:19:50 +00001137 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001138 if (pname)
1139 printf(FMT("%-5s", "%s "), pname);
1140 else
1141 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1142 }
1143
1144 if (format & FMT_OPTIONS) {
1145 if (format & FMT_NOTABLE)
1146 fputs("opt ", stdout);
1147 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1148 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1149 fputc(' ', stdout);
1150 }
1151
1152 if (format & FMT_VIA) {
1153 char iface[IFNAMSIZ+2];
1154
1155 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1156 iface[0] = '!';
1157 iface[1] = '\0';
1158 }
1159 else iface[0] = '\0';
1160
1161 if (fw->ip.iniface[0] != '\0') {
1162 strcat(iface, fw->ip.iniface);
1163 /* If it doesn't compare the nul-term, it's a
1164 wildcard. */
1165 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1166 strcat(iface, "+");
1167 }
1168 else if (format & FMT_NUMERIC) strcat(iface, "*");
1169 else strcat(iface, "any");
1170 printf(FMT(" %-6s ","in %s "), iface);
1171
1172 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1173 iface[0] = '!';
1174 iface[1] = '\0';
1175 }
1176 else iface[0] = '\0';
1177
1178 if (fw->ip.outiface[0] != '\0') {
1179 strcat(iface, fw->ip.outiface);
1180 /* If it doesn't compare the nul-term, it's a
1181 wildcard. */
1182 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1183 strcat(iface, "+");
1184 }
1185 else if (format & FMT_NUMERIC) strcat(iface, "*");
1186 else strcat(iface, "any");
1187 printf(FMT("%-6s ","out %s "), iface);
1188 }
1189
1190 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1191 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1192 printf(FMT("%-19s ","%s "), "anywhere");
1193 else {
1194 if (format & FMT_NUMERIC)
1195 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1196 else
1197 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1198 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1199 printf(FMT("%-19s ","%s "), buf);
1200 }
1201
1202 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1203 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1204 printf(FMT("%-19s","-> %s"), "anywhere");
1205 else {
1206 if (format & FMT_NUMERIC)
1207 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1208 else
1209 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1210 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1211 printf(FMT("%-19s","-> %s"), buf);
1212 }
1213
1214 if (format & FMT_NOTABLE)
1215 fputs(" ", stdout);
1216
1217 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1218
1219 if (target) {
1220 if (target->print)
1221 /* Print the target information. */
1222 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001223 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001224 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001225 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001226
1227 if (!(format & FMT_NONEWLINE))
1228 fputc('\n', stdout);
1229}
1230
1231static void
1232print_firewall_line(const struct ipt_entry *fw,
1233 const iptc_handle_t h)
1234{
1235 struct ipt_entry_target *t;
1236
1237 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001238 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001239}
1240
1241static int
1242append_entry(const ipt_chainlabel chain,
1243 struct ipt_entry *fw,
1244 unsigned int nsaddrs,
1245 const struct in_addr saddrs[],
1246 unsigned int ndaddrs,
1247 const struct in_addr daddrs[],
1248 int verbose,
1249 iptc_handle_t *handle)
1250{
1251 unsigned int i, j;
1252 int ret = 1;
1253
1254 for (i = 0; i < nsaddrs; i++) {
1255 fw->ip.src.s_addr = saddrs[i].s_addr;
1256 for (j = 0; j < ndaddrs; j++) {
1257 fw->ip.dst.s_addr = daddrs[j].s_addr;
1258 if (verbose)
1259 print_firewall_line(fw, *handle);
1260 ret &= iptc_append_entry(chain, fw, handle);
1261 }
1262 }
1263
1264 return ret;
1265}
1266
1267static int
1268replace_entry(const ipt_chainlabel chain,
1269 struct ipt_entry *fw,
1270 unsigned int rulenum,
1271 const struct in_addr *saddr,
1272 const struct in_addr *daddr,
1273 int verbose,
1274 iptc_handle_t *handle)
1275{
1276 fw->ip.src.s_addr = saddr->s_addr;
1277 fw->ip.dst.s_addr = daddr->s_addr;
1278
1279 if (verbose)
1280 print_firewall_line(fw, *handle);
1281 return iptc_replace_entry(chain, fw, rulenum, handle);
1282}
1283
1284static int
1285insert_entry(const ipt_chainlabel chain,
1286 struct ipt_entry *fw,
1287 unsigned int rulenum,
1288 unsigned int nsaddrs,
1289 const struct in_addr saddrs[],
1290 unsigned int ndaddrs,
1291 const struct in_addr daddrs[],
1292 int verbose,
1293 iptc_handle_t *handle)
1294{
1295 unsigned int i, j;
1296 int ret = 1;
1297
1298 for (i = 0; i < nsaddrs; i++) {
1299 fw->ip.src.s_addr = saddrs[i].s_addr;
1300 for (j = 0; j < ndaddrs; j++) {
1301 fw->ip.dst.s_addr = daddrs[j].s_addr;
1302 if (verbose)
1303 print_firewall_line(fw, *handle);
1304 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1305 }
1306 }
1307
1308 return ret;
1309}
1310
Rusty Russell2e0a3212000-04-19 11:23:18 +00001311static unsigned char *
1312make_delete_mask(struct ipt_entry *fw)
1313{
1314 /* Establish mask for comparison */
1315 unsigned int size;
1316 struct iptables_match *m;
1317 unsigned char *mask, *mptr;
1318
1319 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001320 for (m = iptables_matches; m && m->used; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001321 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001322
Rusty Russell9e1d2142000-04-23 09:11:12 +00001323 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001324 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001325 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001326
Rusty Russell9e1d2142000-04-23 09:11:12 +00001327 memset(mask, 0xFF, sizeof(struct ipt_entry));
1328 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001329
Harald Welteae1ff9f2000-12-01 14:26:20 +00001330 for (m = iptables_matches; m && m->used; m = m->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001331 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001332 IPT_ALIGN(sizeof(struct ipt_entry_match))
1333 + m->userspacesize);
1334 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001335 }
1336
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001337 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001338 IPT_ALIGN(sizeof(struct ipt_entry_target))
1339 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001340
1341 return mask;
1342}
1343
Marc Bouchere6869a82000-03-20 06:03:29 +00001344static int
1345delete_entry(const ipt_chainlabel chain,
1346 struct ipt_entry *fw,
1347 unsigned int nsaddrs,
1348 const struct in_addr saddrs[],
1349 unsigned int ndaddrs,
1350 const struct in_addr daddrs[],
1351 int verbose,
1352 iptc_handle_t *handle)
1353{
1354 unsigned int i, j;
1355 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001356 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001357
Rusty Russell2e0a3212000-04-19 11:23:18 +00001358 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001359 for (i = 0; i < nsaddrs; i++) {
1360 fw->ip.src.s_addr = saddrs[i].s_addr;
1361 for (j = 0; j < ndaddrs; j++) {
1362 fw->ip.dst.s_addr = daddrs[j].s_addr;
1363 if (verbose)
1364 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001365 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001366 }
1367 }
1368 return ret;
1369}
1370
1371static int
1372check_packet(const ipt_chainlabel chain,
1373 struct ipt_entry *fw,
1374 unsigned int nsaddrs,
1375 const struct in_addr saddrs[],
1376 unsigned int ndaddrs,
1377 const struct in_addr daddrs[],
1378 int verbose,
1379 iptc_handle_t *handle)
1380{
1381 int ret = 1;
1382 unsigned int i, j;
1383 const char *msg;
1384
1385 for (i = 0; i < nsaddrs; i++) {
1386 fw->ip.src.s_addr = saddrs[i].s_addr;
1387 for (j = 0; j < ndaddrs; j++) {
1388 fw->ip.dst.s_addr = daddrs[j].s_addr;
1389 if (verbose)
1390 print_firewall_line(fw, *handle);
1391 msg = iptc_check_packet(chain, fw, handle);
1392 if (!msg) ret = 0;
1393 else printf("%s\n", msg);
1394 }
1395 }
1396
1397 return ret;
1398}
1399
Harald Welteae1ff9f2000-12-01 14:26:20 +00001400int
Marc Bouchere6869a82000-03-20 06:03:29 +00001401for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001402 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001403{
1404 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001405 const char *chain;
1406 char *chains;
1407 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001408
Rusty Russell9e1d2142000-04-23 09:11:12 +00001409 chain = iptc_first_chain(handle);
1410 while (chain) {
1411 chaincount++;
1412 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001413 }
1414
Rusty Russell9e1d2142000-04-23 09:11:12 +00001415 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1416 i = 0;
1417 chain = iptc_first_chain(handle);
1418 while (chain) {
1419 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1420 i++;
1421 chain = iptc_next_chain(handle);
1422 }
1423
1424 for (i = 0; i < chaincount; i++) {
1425 if (!builtinstoo
1426 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1427 *handle))
1428 continue;
1429 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1430 }
1431
1432 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001433 return ret;
1434}
1435
Harald Welteae1ff9f2000-12-01 14:26:20 +00001436int
Marc Bouchere6869a82000-03-20 06:03:29 +00001437flush_entries(const ipt_chainlabel chain, int verbose,
1438 iptc_handle_t *handle)
1439{
1440 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001441 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001442
1443 if (verbose)
1444 fprintf(stdout, "Flushing chain `%s'\n", chain);
1445 return iptc_flush_entries(chain, handle);
1446}
Marc Bouchere6869a82000-03-20 06:03:29 +00001447
1448static int
1449zero_entries(const ipt_chainlabel chain, int verbose,
1450 iptc_handle_t *handle)
1451{
1452 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001453 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001454
Marc Bouchere6869a82000-03-20 06:03:29 +00001455 if (verbose)
1456 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1457 return iptc_zero_entries(chain, handle);
1458}
1459
Harald Welteae1ff9f2000-12-01 14:26:20 +00001460int
Marc Bouchere6869a82000-03-20 06:03:29 +00001461delete_chain(const ipt_chainlabel chain, int verbose,
1462 iptc_handle_t *handle)
1463{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001464 if (!chain)
1465 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
1467 if (verbose)
1468 fprintf(stdout, "Deleting chain `%s'\n", chain);
1469 return iptc_delete_chain(chain, handle);
1470}
1471
1472static int
1473list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1474 int expanded, int linenumbers, iptc_handle_t *handle)
1475{
1476 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001477 unsigned int format;
1478 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001479
1480 format = FMT_OPTIONS;
1481 if (!verbose)
1482 format |= FMT_NOCOUNTS;
1483 else
1484 format |= FMT_VIA;
1485
1486 if (numeric)
1487 format |= FMT_NUMERIC;
1488
1489 if (!expanded)
1490 format |= FMT_KILOMEGAGIGA;
1491
1492 if (linenumbers)
1493 format |= FMT_LINENUMBERS;
1494
Rusty Russell9e1d2142000-04-23 09:11:12 +00001495 for (this = iptc_first_chain(handle);
1496 this;
1497 this = iptc_next_chain(handle)) {
1498 const struct ipt_entry *i;
1499 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001500
Marc Bouchere6869a82000-03-20 06:03:29 +00001501 if (chain && strcmp(chain, this) != 0)
1502 continue;
1503
1504 if (found) printf("\n");
1505
1506 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001507 i = iptc_first_rule(this, handle);
1508
1509 num = 0;
1510 while (i) {
1511 print_firewall(i,
1512 iptc_get_target(i, handle),
1513 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001514 format,
1515 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001516 i = iptc_next_rule(i, handle);
1517 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001518 found = 1;
1519 }
1520
1521 errno = ENOENT;
1522 return found;
1523}
1524
Harald Welte82dd2ec2000-12-19 05:18:15 +00001525static char *get_modprobe(void)
1526{
1527 int procfile;
1528 char *ret;
1529
1530 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1531 if (procfile < 0)
1532 return NULL;
1533
1534 ret = malloc(1024);
1535 if (ret) {
1536 switch (read(procfile, ret, 1024)) {
1537 case -1: goto fail;
1538 case 1024: goto fail; /* Partial read. Wierd */
1539 }
1540 return ret;
1541 }
1542 fail:
1543 free(ret);
1544 close(procfile);
1545 return NULL;
1546}
1547
1548static int iptables_insmod(const char *modname, const char *modprobe)
1549{
1550 char *buf = NULL;
1551 char *argv[3];
1552
1553 /* If they don't explicitly set it, read out of kernel */
1554 if (!modprobe) {
1555 buf = get_modprobe();
1556 if (!buf)
1557 return -1;
1558 modprobe = buf;
1559 }
1560
1561 switch (fork()) {
1562 case 0:
1563 argv[0] = (char *)modprobe;
1564 argv[1] = (char *)modname;
1565 argv[2] = NULL;
1566 execv(argv[0], argv);
1567
1568 /* not usually reached */
1569 exit(0);
1570 case -1:
1571 return -1;
1572
1573 default: /* parent */
1574 wait(NULL);
1575 }
1576
1577 free(buf);
1578 return 0;
1579}
1580
Marc Bouchere6869a82000-03-20 06:03:29 +00001581static struct ipt_entry *
1582generate_entry(const struct ipt_entry *fw,
1583 struct iptables_match *matches,
1584 struct ipt_entry_target *target)
1585{
1586 unsigned int size;
1587 struct iptables_match *m;
1588 struct ipt_entry *e;
1589
1590 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001591 for (m = matches; m && m->used; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001592 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001593
Rusty Russell228e98d2000-04-27 10:28:06 +00001594 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001595 *e = *fw;
1596 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001597 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001598
1599 size = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001600 for (m = matches; m && m->used; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001601 memcpy(e->elems + size, m->m, m->m->u.match_size);
1602 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001603 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001604 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001605
1606 return e;
1607}
1608
1609int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1610{
1611 struct ipt_entry fw, *e = NULL;
1612 int invert = 0;
1613 unsigned int nsaddrs = 0, ndaddrs = 0;
1614 struct in_addr *saddrs = NULL, *daddrs = NULL;
1615
1616 int c, verbose = 0;
1617 const char *chain = NULL;
1618 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1619 const char *policy = NULL, *newname = NULL;
1620 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001621 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001622 int ret = 1;
1623 struct iptables_match *m;
1624 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001625 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001626 const char *jumpto = "";
1627 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001628 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001629
1630 memset(&fw, 0, sizeof(fw));
1631
Harald Welteae1ff9f2000-12-01 14:26:20 +00001632 /* re-set optind to 0 in case do_command gets called
1633 * a second time */
1634 optind = 0;
1635
1636 /* clear mflags in case do_command gets called a second time
1637 * (we clear the global list of all matches for security)*/
1638 for (m = iptables_matches; m; m = m->next) {
1639 m->mflags = 0;
1640 m->used = 0;
1641 }
1642
1643 for (t = iptables_targets; t; t = t->next) {
1644 t->tflags = 0;
1645 t->used = 0;
1646 }
1647
Marc Bouchere6869a82000-03-20 06:03:29 +00001648 /* Suppress error messages: we may add new options if we
1649 demand-load a protocol. */
1650 opterr = 0;
1651
1652 while ((c = getopt_long(argc, argv,
Harald Welteccd49e52001-01-23 22:54:34 +00001653 "-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 +00001654 opts, NULL)) != -1) {
1655 switch (c) {
1656 /*
1657 * Command selection
1658 */
1659 case 'A':
1660 add_command(&command, CMD_APPEND, CMD_NONE,
1661 invert);
1662 chain = optarg;
1663 break;
1664
1665 case 'D':
1666 add_command(&command, CMD_DELETE, CMD_NONE,
1667 invert);
1668 chain = optarg;
1669 if (optind < argc && argv[optind][0] != '-'
1670 && argv[optind][0] != '!') {
1671 rulenum = parse_rulenumber(argv[optind++]);
1672 command = CMD_DELETE_NUM;
1673 }
1674 break;
1675
1676 case 'C':
1677 add_command(&command, CMD_CHECK, CMD_NONE,
1678 invert);
1679 chain = optarg;
1680 break;
1681
1682 case 'R':
1683 add_command(&command, CMD_REPLACE, CMD_NONE,
1684 invert);
1685 chain = optarg;
1686 if (optind < argc && argv[optind][0] != '-'
1687 && argv[optind][0] != '!')
1688 rulenum = parse_rulenumber(argv[optind++]);
1689 else
1690 exit_error(PARAMETER_PROBLEM,
1691 "-%c requires a rule number",
1692 cmd2char(CMD_REPLACE));
1693 break;
1694
1695 case 'I':
1696 add_command(&command, CMD_INSERT, CMD_NONE,
1697 invert);
1698 chain = optarg;
1699 if (optind < argc && argv[optind][0] != '-'
1700 && argv[optind][0] != '!')
1701 rulenum = parse_rulenumber(argv[optind++]);
1702 else rulenum = 1;
1703 break;
1704
1705 case 'L':
1706 add_command(&command, CMD_LIST, CMD_ZERO,
1707 invert);
1708 if (optarg) chain = optarg;
1709 else if (optind < argc && argv[optind][0] != '-'
1710 && argv[optind][0] != '!')
1711 chain = argv[optind++];
1712 break;
1713
1714 case 'F':
1715 add_command(&command, CMD_FLUSH, CMD_NONE,
1716 invert);
1717 if (optarg) chain = optarg;
1718 else if (optind < argc && argv[optind][0] != '-'
1719 && argv[optind][0] != '!')
1720 chain = argv[optind++];
1721 break;
1722
1723 case 'Z':
1724 add_command(&command, CMD_ZERO, CMD_LIST,
1725 invert);
1726 if (optarg) chain = optarg;
1727 else if (optind < argc && argv[optind][0] != '-'
1728 && argv[optind][0] != '!')
1729 chain = argv[optind++];
1730 break;
1731
1732 case 'N':
1733 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1734 invert);
1735 chain = optarg;
1736 break;
1737
1738 case 'X':
1739 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1740 invert);
1741 if (optarg) chain = optarg;
1742 else if (optind < argc && argv[optind][0] != '-'
1743 && argv[optind][0] != '!')
1744 chain = argv[optind++];
1745 break;
1746
1747 case 'E':
1748 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1749 invert);
1750 chain = optarg;
1751 if (optind < argc && argv[optind][0] != '-'
1752 && argv[optind][0] != '!')
1753 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001754 else
1755 exit_error(PARAMETER_PROBLEM,
1756 "-%c requires old-chain-name and "
1757 "new-chain-name",
1758 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001759 break;
1760
1761 case 'P':
1762 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1763 invert);
1764 chain = optarg;
1765 if (optind < argc && argv[optind][0] != '-'
1766 && argv[optind][0] != '!')
1767 policy = argv[optind++];
1768 else
1769 exit_error(PARAMETER_PROBLEM,
1770 "-%c requires a chain and a policy",
1771 cmd2char(CMD_SET_POLICY));
1772 break;
1773
1774 case 'h':
1775 if (!optarg)
1776 optarg = argv[optind];
1777
Rusty Russell2e0a3212000-04-19 11:23:18 +00001778 /* iptables -p icmp -h */
1779 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001780 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001781
Marc Bouchere6869a82000-03-20 06:03:29 +00001782 exit_printhelp();
1783
1784 /*
1785 * Option selection
1786 */
1787 case 'p':
1788 if (check_inverse(optarg, &invert))
1789 optind++;
1790 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1791 invert);
1792
1793 /* Canonicalize into lower case */
1794 for (protocol = argv[optind-1]; *protocol; protocol++)
1795 *protocol = tolower(*protocol);
1796
1797 protocol = argv[optind-1];
1798 fw.ip.proto = parse_protocol(protocol);
1799
1800 if (fw.ip.proto == 0
1801 && (fw.ip.invflags & IPT_INV_PROTO))
1802 exit_error(PARAMETER_PROBLEM,
1803 "rule would never match protocol");
1804 fw.nfcache |= NFC_IP_PROTO;
1805 break;
1806
1807 case 's':
1808 if (check_inverse(optarg, &invert))
1809 optind++;
1810 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1811 invert);
1812 shostnetworkmask = argv[optind-1];
1813 fw.nfcache |= NFC_IP_SRC;
1814 break;
1815
1816 case 'd':
1817 if (check_inverse(optarg, &invert))
1818 optind++;
1819 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1820 invert);
1821 dhostnetworkmask = argv[optind-1];
1822 fw.nfcache |= NFC_IP_DST;
1823 break;
1824
1825 case 'j':
1826 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1827 invert);
1828 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001829 /* TRY_LOAD (may be chain name) */
1830 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001831
1832 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001833 size_t size;
1834
Rusty Russell73f72f52000-07-03 10:17:57 +00001835 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1836 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001837
Rusty Russell2e0a3212000-04-19 11:23:18 +00001838 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001839 target->t->u.target_size = size;
1840 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001841 target->init(target->t, &fw.nfcache);
1842 }
1843 break;
1844
1845
1846 case 'i':
1847 if (check_inverse(optarg, &invert))
1848 optind++;
1849 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1850 invert);
1851 parse_interface(argv[optind-1],
1852 fw.ip.iniface,
1853 fw.ip.iniface_mask);
1854 fw.nfcache |= NFC_IP_IF_IN;
1855 break;
1856
1857 case 'o':
1858 if (check_inverse(optarg, &invert))
1859 optind++;
1860 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1861 invert);
1862 parse_interface(argv[optind-1],
1863 fw.ip.outiface,
1864 fw.ip.outiface_mask);
1865 fw.nfcache |= NFC_IP_IF_OUT;
1866 break;
1867
1868 case 'f':
1869 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1870 invert);
1871 fw.ip.flags |= IPT_F_FRAG;
1872 fw.nfcache |= NFC_IP_FRAG;
1873 break;
1874
1875 case 'v':
1876 if (!verbose)
1877 set_option(&options, OPT_VERBOSE,
1878 &fw.ip.invflags, invert);
1879 verbose++;
1880 break;
1881
Rusty Russell52a51492000-05-02 16:44:29 +00001882 case 'm': {
1883 size_t size;
1884
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 if (invert)
1886 exit_error(PARAMETER_PROBLEM,
1887 "unexpected ! flag before --match");
1888
Rusty Russell52a51492000-05-02 16:44:29 +00001889 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001890 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1891 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001892 m->m = fw_calloc(1, size);
1893 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001894 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001895 m->init(m->m, &fw.nfcache);
1896 }
1897 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001898
1899 case 'n':
1900 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1901 invert);
1902 break;
1903
1904 case 't':
1905 if (invert)
1906 exit_error(PARAMETER_PROBLEM,
1907 "unexpected ! flag before --table");
1908 *table = argv[optind-1];
1909 break;
1910
1911 case 'x':
1912 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1913 invert);
1914 break;
1915
1916 case 'V':
1917 if (invert)
1918 printf("Not %s ;-)\n", program_version);
1919 else
1920 printf("%s v%s\n",
1921 program_name, program_version);
1922 exit(0);
1923
1924 case '0':
1925 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1926 invert);
1927 break;
1928
Harald Welte82dd2ec2000-12-19 05:18:15 +00001929 case 'M':
1930 modprobe = optarg;
1931 break;
1932
Harald Welteccd49e52001-01-23 22:54:34 +00001933 case 'c':
1934
1935 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1936 invert);
1937 pcnt = optarg;
1938 if (optind < argc && argv[optind][0] != '-'
1939 && argv[optind][0] != '!')
1940 bcnt = argv[optind++];
1941 else
1942 exit_error(PARAMETER_PROBLEM,
1943 "-%c requires packet and byte counter",
1944 opt2char(OPT_COUNTERS));
1945
1946 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1947 exit_error(PARAMETER_PROBLEM,
1948 "-%c packet counter not numeric",
1949 opt2char(OPT_COUNTERS));
1950
1951 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1952 exit_error(PARAMETER_PROBLEM,
1953 "-%c byte counter not numeric",
1954 opt2char(OPT_COUNTERS));
1955
1956 break;
1957
1958
Marc Bouchere6869a82000-03-20 06:03:29 +00001959 case 1: /* non option */
1960 if (optarg[0] == '!' && optarg[1] == '\0') {
1961 if (invert)
1962 exit_error(PARAMETER_PROBLEM,
1963 "multiple consecutive ! not"
1964 " allowed");
1965 invert = TRUE;
1966 optarg[0] = '\0';
1967 continue;
1968 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001969 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001970 exit_tryhelp(2);
1971
1972 default:
1973 /* FIXME: This scheme doesn't allow two of the same
1974 matches --RR */
1975 if (!target
1976 || !(target->parse(c - target->option_offset,
1977 argv, invert,
1978 &target->tflags,
1979 &fw, &target->t))) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001980 for (m = iptables_matches; m && m->used; m = m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001981 if (m->parse(c - m->option_offset,
1982 argv, invert,
1983 &m->mflags,
1984 &fw,
1985 &fw.nfcache,
1986 &m->m))
1987 break;
1988 }
1989
1990 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001991 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001992 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001993 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001994 && !find_proto(protocol, DONT_LOAD,
1995 options&OPT_NUMERIC)
1996 && (m = find_proto(protocol, TRY_LOAD,
1997 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001999 size_t size;
2000
Rusty Russell73f72f52000-07-03 10:17:57 +00002001 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2002 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002003
Rusty Russell2e0a3212000-04-19 11:23:18 +00002004 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002005 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002006 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002007 m->init(m->m, &fw.nfcache);
2008
2009 optind--;
2010 continue;
2011 }
2012 if (!m)
2013 exit_error(PARAMETER_PROBLEM,
2014 "Unknown arg `%s'",
2015 argv[optind-1]);
2016 }
2017 }
2018 invert = FALSE;
2019 }
2020
Harald Welteae1ff9f2000-12-01 14:26:20 +00002021 for (m = iptables_matches; m && m->used; m = m->next)
Marc Bouchere6869a82000-03-20 06:03:29 +00002022 m->final_check(m->mflags);
2023 if (target)
2024 target->final_check(target->tflags);
2025
2026 /* Fix me: must put inverse options checking here --MN */
2027
2028 if (optind < argc)
2029 exit_error(PARAMETER_PROBLEM,
2030 "unknown arguments found on commandline");
2031 if (!command)
2032 exit_error(PARAMETER_PROBLEM, "no command specified");
2033 if (invert)
2034 exit_error(PARAMETER_PROBLEM,
2035 "nothing appropriate following !");
2036
Marc Boucher744bd022000-04-22 22:36:10 +00002037 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2038 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 if (!(options & OPT_DESTINATION))
2040 dhostnetworkmask = "0.0.0.0/0";
2041 if (!(options & OPT_SOURCE))
2042 shostnetworkmask = "0.0.0.0/0";
2043 }
2044
2045 if (shostnetworkmask)
2046 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2047 &(fw.ip.smsk), &nsaddrs);
2048
2049 if (dhostnetworkmask)
2050 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2051 &(fw.ip.dmsk), &ndaddrs);
2052
2053 if ((nsaddrs > 1 || ndaddrs > 1) &&
2054 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2055 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2056 " source or destination IP addresses");
2057
2058 if (command == CMD_CHECK && fw.ip.invflags != 0)
2059 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2060 cmd2char(CMD_CHECK));
2061
2062 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2063 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2064 "specify a unique address");
2065
2066 generic_opt_check(command, options);
2067
2068 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2069 exit_error(PARAMETER_PROBLEM,
2070 "chain name `%s' too long (must be under %i chars)",
2071 chain, IPT_FUNCTION_MAXNAMELEN);
2072
Harald Welteae1ff9f2000-12-01 14:26:20 +00002073 /* only allocate handle if we weren't called with a handle */
2074 if (!*handle)
2075 *handle = iptc_init(*table);
2076
Harald Welte82dd2ec2000-12-19 05:18:15 +00002077 if (!*handle) {
2078 /* try to insmod the module if iptc_init failed */
2079 iptables_insmod("ip_tables", modprobe);
2080 *handle = iptc_init(*table);
2081 }
2082
Marc Bouchere6869a82000-03-20 06:03:29 +00002083 if (!*handle)
2084 exit_error(VERSION_PROBLEM,
2085 "can't initialize iptables table `%s': %s",
2086 *table, iptc_strerror(errno));
2087
Marc Boucher744bd022000-04-22 22:36:10 +00002088 if (command == CMD_CHECK
2089 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002090 || command == CMD_DELETE
2091 || command == CMD_INSERT
2092 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002093 if (strcmp(chain, "PREROUTING") == 0
2094 || strcmp(chain, "INPUT") == 0) {
2095 /* -o not valid with incoming packets. */
2096 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002097 exit_error(PARAMETER_PROBLEM,
2098 "Can't use -%c with %s\n",
2099 opt2char(OPT_VIANAMEOUT),
2100 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002101 /* -i required with -C */
2102 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2103 exit_error(PARAMETER_PROBLEM,
2104 "Need -%c with %s\n",
2105 opt2char(OPT_VIANAMEIN),
2106 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002107 }
2108
Rusty Russella4860fd2000-06-17 16:13:02 +00002109 if (strcmp(chain, "POSTROUTING") == 0
2110 || strcmp(chain, "OUTPUT") == 0) {
2111 /* -i not valid with outgoing packets */
2112 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002113 exit_error(PARAMETER_PROBLEM,
2114 "Can't use -%c with %s\n",
2115 opt2char(OPT_VIANAMEIN),
2116 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002117 /* -o required with -C */
2118 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2119 exit_error(PARAMETER_PROBLEM,
2120 "Need -%c with %s\n",
2121 opt2char(OPT_VIANAMEOUT),
2122 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002123 }
2124
2125 if (target && iptc_is_chain(jumpto, *handle)) {
2126 printf("Warning: using chain %s, not extension\n",
2127 jumpto);
2128
2129 target = NULL;
2130 }
2131
2132 /* If they didn't specify a target, or it's a chain
2133 name, use standard. */
2134 if (!target
2135 && (strlen(jumpto) == 0
2136 || iptc_is_chain(jumpto, *handle))) {
2137 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002138
Rusty Russell52a51492000-05-02 16:44:29 +00002139 target = find_target(IPT_STANDARD_TARGET,
2140 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002141
2142 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002143 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002144 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002145 target->t->u.target_size = size;
2146 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002147 target->init(target->t, &fw.nfcache);
2148 }
2149
Rusty Russell7e53bf92000-03-20 07:03:28 +00002150 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002151 /* it is no chain, and we can't load a plugin.
2152 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002153 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002154 * chain. */
2155 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002156 } else {
2157 e = generate_entry(&fw, iptables_matches, target->t);
2158 }
2159 }
2160
2161 switch (command) {
2162 case CMD_APPEND:
2163 ret = append_entry(chain, e,
2164 nsaddrs, saddrs, ndaddrs, daddrs,
2165 options&OPT_VERBOSE,
2166 handle);
2167 break;
2168 case CMD_CHECK:
2169 ret = check_packet(chain, e,
2170 nsaddrs, saddrs, ndaddrs, daddrs,
2171 options&OPT_VERBOSE, handle);
2172 break;
2173 case CMD_DELETE:
2174 ret = delete_entry(chain, e,
2175 nsaddrs, saddrs, ndaddrs, daddrs,
2176 options&OPT_VERBOSE,
2177 handle);
2178 break;
2179 case CMD_DELETE_NUM:
2180 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2181 break;
2182 case CMD_REPLACE:
2183 ret = replace_entry(chain, e, rulenum - 1,
2184 saddrs, daddrs, options&OPT_VERBOSE,
2185 handle);
2186 break;
2187 case CMD_INSERT:
2188 ret = insert_entry(chain, e, rulenum - 1,
2189 nsaddrs, saddrs, ndaddrs, daddrs,
2190 options&OPT_VERBOSE,
2191 handle);
2192 break;
2193 case CMD_LIST:
2194 ret = list_entries(chain,
2195 options&OPT_VERBOSE,
2196 options&OPT_NUMERIC,
2197 options&OPT_EXPANDED,
2198 options&OPT_LINENUMBERS,
2199 handle);
2200 break;
2201 case CMD_FLUSH:
2202 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2203 break;
2204 case CMD_ZERO:
2205 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2206 break;
2207 case CMD_LIST|CMD_ZERO:
2208 ret = list_entries(chain,
2209 options&OPT_VERBOSE,
2210 options&OPT_NUMERIC,
2211 options&OPT_EXPANDED,
2212 options&OPT_LINENUMBERS,
2213 handle);
2214 if (ret)
2215 ret = zero_entries(chain,
2216 options&OPT_VERBOSE, handle);
2217 break;
2218 case CMD_NEW_CHAIN:
2219 ret = iptc_create_chain(chain, handle);
2220 break;
2221 case CMD_DELETE_CHAIN:
2222 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2223 break;
2224 case CMD_RENAME_CHAIN:
2225 ret = iptc_rename_chain(chain, newname, handle);
2226 break;
2227 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002228 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002229 break;
2230 default:
2231 /* We should never reach this... */
2232 exit_tryhelp(2);
2233 }
2234
2235 if (verbose > 1)
2236 dump_entries(*handle);
2237
2238 return ret;
2239}