blob: e6b028894c6ff587af8fd33c9bd283b0856c7f79 [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
98#define NUMBER_OF_OPT 11
99static const char optflags[NUMBER_OF_OPT]
100= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3'};
101
102static struct option original_opts[] = {
103 { "append", 1, 0, 'A' },
104 { "delete", 1, 0, 'D' },
105 { "insert", 1, 0, 'I' },
106 { "replace", 1, 0, 'R' },
107 { "list", 2, 0, 'L' },
108 { "flush", 2, 0, 'F' },
109 { "zero", 2, 0, 'Z' },
110 { "check", 1, 0, 'C' },
111 { "new-chain", 1, 0, 'N' },
112 { "delete-chain", 2, 0, 'X' },
113 { "rename-chain", 2, 0, 'E' },
114 { "policy", 1, 0, 'P' },
115 { "source", 1, 0, 's' },
116 { "destination", 1, 0, 'd' },
117 { "src", 1, 0, 's' }, /* synonym */
118 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000119 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 { "in-interface", 1, 0, 'i' },
121 { "jump", 1, 0, 'j' },
122 { "table", 1, 0, 't' },
123 { "match", 1, 0, 'm' },
124 { "numeric", 0, 0, 'n' },
125 { "out-interface", 1, 0, 'o' },
126 { "verbose", 0, 0, 'v' },
127 { "exact", 0, 0, 'x' },
128 { "fragments", 0, 0, 'f' },
129 { "version", 0, 0, 'V' },
130 { "help", 2, 0, 'h' },
131 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000132 { "modprobe", 1, 0, 'M' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 { 0 }
134};
135
Rusty Russell4e242f82000-05-31 06:33:50 +0000136#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000137struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000138ipt_get_target(struct ipt_entry *e)
139{
140 return (void *)e + e->target_offset;
141}
142#endif
143
Marc Bouchere6869a82000-03-20 06:03:29 +0000144static struct option *opts = original_opts;
145static unsigned int global_option_offset = 0;
146
147/* Table of legal combinations of commands and options. If any of the
148 * given commands make an option legal, that option is legal (applies to
149 * CMD_LIST and CMD_ZERO only).
150 * Key:
151 * + compulsory
152 * x illegal
153 * optional
154 */
155
156static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
157/* Well, it's better than "Re: Linux vs FreeBSD" */
158{
159 /* -n -s -d -p -j -v -x -i -o -f --line */
160/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
161/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
162/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
163/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
165/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
166/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000171/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000172/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
173};
174
175static int inverse_for_options[NUMBER_OF_OPT] =
176{
177/* -n */ 0,
178/* -s */ IPT_INV_SRCIP,
179/* -d */ IPT_INV_DSTIP,
180/* -p */ IPT_INV_PROTO,
181/* -j */ 0,
182/* -v */ 0,
183/* -x */ 0,
184/* -i */ IPT_INV_VIA_IN,
185/* -o */ IPT_INV_VIA_OUT,
186/* -f */ IPT_INV_FRAG,
187/*--line*/ 0
188};
189
190const char *program_version;
191const char *program_name;
192
Rusty Russell2e0a3212000-04-19 11:23:18 +0000193/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000194struct iptables_match *iptables_matches = NULL;
195struct iptables_target *iptables_targets = NULL;
196
197/* Extra debugging from libiptc */
198extern void dump_entries(const iptc_handle_t handle);
199
200/* A few hardcoded protocols for 'all' and in case the user has no
201 /etc/protocols */
202struct pprot {
203 char *name;
204 u_int8_t num;
205};
206
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000207/* Primitive headers... */
208#ifndef IPPROTO_ESP
209#define IPPROTO_ESP 50
210#endif
211#ifndef IPPROTO_AH
212#define IPPROTO_AH 51
213#endif
214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215static const struct pprot chain_protos[] = {
216 { "tcp", IPPROTO_TCP },
217 { "udp", IPPROTO_UDP },
218 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000219 { "esp", IPPROTO_ESP },
220 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000221 { "all", 0 },
222};
223
224static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000225proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000226{
227 unsigned int i;
228
Rusty Russell28381a42000-05-10 00:19:50 +0000229 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000230 struct protoent *pent = getprotobynumber(proto);
231 if (pent)
232 return pent->p_name;
233 }
234
235 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
236 if (chain_protos[i].num == proto)
237 return chain_protos[i].name;
238
239 return NULL;
240}
241
242struct in_addr *
243dotted_to_addr(const char *dotted)
244{
245 static struct in_addr addr;
246 unsigned char *addrp;
247 char *p, *q;
248 int onebyte, i;
249 char buf[20];
250
251 /* copy dotted string, because we need to modify it */
252 strncpy(buf, dotted, sizeof(buf) - 1);
253 addrp = (unsigned char *) &(addr.s_addr);
254
255 p = buf;
256 for (i = 0; i < 3; i++) {
257 if ((q = strchr(p, '.')) == NULL)
258 return (struct in_addr *) NULL;
259
260 *q = '\0';
261 if ((onebyte = string_to_number(p, 0, 255)) == -1)
262 return (struct in_addr *) NULL;
263
264 addrp[i] = (unsigned char) onebyte;
265 p = q + 1;
266 }
267
268 /* we've checked 3 bytes, now we check the last one */
269 if ((onebyte = string_to_number(p, 0, 255)) == -1)
270 return (struct in_addr *) NULL;
271
272 addrp[3] = (unsigned char) onebyte;
273
274 return &addr;
275}
276
277static struct in_addr *
278network_to_addr(const char *name)
279{
280 struct netent *net;
281 static struct in_addr addr;
282
283 if ((net = getnetbyname(name)) != NULL) {
284 if (net->n_addrtype != AF_INET)
285 return (struct in_addr *) NULL;
286 addr.s_addr = htonl((unsigned long) net->n_net);
287 return &addr;
288 }
289
290 return (struct in_addr *) NULL;
291}
292
293static void
294inaddrcpy(struct in_addr *dst, struct in_addr *src)
295{
296 /* memcpy(dst, src, sizeof(struct in_addr)); */
297 dst->s_addr = src->s_addr;
298}
299
300void
301exit_error(enum exittype status, char *msg, ...)
302{
303 va_list args;
304
305 va_start(args, msg);
306 fprintf(stderr, "%s v%s: ", program_name, program_version);
307 vfprintf(stderr, msg, args);
308 va_end(args);
309 fprintf(stderr, "\n");
310 if (status == PARAMETER_PROBLEM)
311 exit_tryhelp(status);
312 if (status == VERSION_PROBLEM)
313 fprintf(stderr,
314 "Perhaps iptables or your kernel needs to be upgraded.\n");
315 exit(status);
316}
317
318void
319exit_tryhelp(int status)
320{
321 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
322 program_name, program_name );
323 exit(status);
324}
325
326void
327exit_printhelp(void)
328{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000329 struct iptables_match *m = NULL;
330 struct iptables_target *t = NULL;
331
Marc Bouchere6869a82000-03-20 06:03:29 +0000332 printf("%s v%s\n\n"
333"Usage: %s -[ADC] chain rule-specification [options]\n"
334" %s -[RI] chain rulenum rule-specification [options]\n"
335" %s -D chain rulenum [options]\n"
336" %s -[LFZ] [chain] [options]\n"
337" %s -[NX] chain\n"
338" %s -E old-chain-name new-chain-name\n"
339" %s -P chain target [options]\n"
340" %s -h (print this help information)\n\n",
341 program_name, program_version, program_name, program_name,
342 program_name, program_name, program_name, program_name,
343 program_name, program_name);
344
345 printf(
346"Commands:\n"
347"Either long or short options are allowed.\n"
348" --append -A chain Append to chain\n"
349" --delete -D chain Delete matching rule from chain\n"
350" --delete -D chain rulenum\n"
351" Delete rule rulenum (1 = first) from chain\n"
352" --insert -I chain [rulenum]\n"
353" Insert in chain as rulenum (default 1=first)\n"
354" --replace -R chain rulenum\n"
355" Replace rule rulenum (1 = first) in chain\n"
356" --list -L [chain] List the rules in a chain or all chains\n"
357" --flush -F [chain] Delete all rules in chain or all chains\n"
358" --zero -Z [chain] Zero counters in chain or all chains\n"
359" --check -C chain Test this packet on chain\n"
360" --new -N chain Create a new user-defined chain\n"
361" --delete-chain\n"
362" -X [chain] Delete a user-defined chain\n"
363" --policy -P chain target\n"
364" Change policy on chain to target\n"
365" --rename-chain\n"
366" -E old-chain new-chain\n"
367" Change chain name, (moving any references)\n"
368
369"Options:\n"
370" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
371" --source -s [!] address[/mask]\n"
372" source specification\n"
373" --destination -d [!] address[/mask]\n"
374" destination specification\n"
375" --in-interface -i [!] input name[+]\n"
376" network interface name ([+] for wildcard)\n"
377" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000378" target for rule (may load target extension)\n"
379" --match -m match\n"
380" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000381" --numeric -n numeric output of addresses and ports\n"
382" --out-interface -o [!] output name[+]\n"
383" network interface name ([+] for wildcard)\n"
384" --table -t table table to manipulate (default: `filter')\n"
385" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000386" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000387" --exact -x expand numbers (display exact values)\n"
388"[!] --fragment -f match second or further fragments only\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000389" --modprobe=<command> try to insert modules using this commandx\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000390"[!] --version -V print package version.\n");
391
Rusty Russell363112d2000-08-11 13:49:26 +0000392 /* Print out any special helps. A user might like to be able
393 to add a --help to the commandline, and see expected
394 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000395 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000396 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000397 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000398 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000399 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000400 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000401 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 exit(0);
404}
405
406static void
407generic_opt_check(int command, int options)
408{
409 int i, j, legal = 0;
410
411 /* Check that commands are valid with options. Complicated by the
412 * fact that if an option is legal with *any* command given, it is
413 * legal overall (ie. -z and -l).
414 */
415 for (i = 0; i < NUMBER_OF_OPT; i++) {
416 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
417
418 for (j = 0; j < NUMBER_OF_CMD; j++) {
419 if (!(command & (1<<j)))
420 continue;
421
422 if (!(options & (1<<i))) {
423 if (commands_v_options[j][i] == '+')
424 exit_error(PARAMETER_PROBLEM,
425 "You need to supply the `-%c' "
426 "option for this command\n",
427 optflags[i]);
428 } else {
429 if (commands_v_options[j][i] != 'x')
430 legal = 1;
431 else if (legal == 0)
432 legal = -1;
433 }
434 }
435 if (legal == -1)
436 exit_error(PARAMETER_PROBLEM,
437 "Illegal option `-%c' with this command\n",
438 optflags[i]);
439 }
440}
441
442static char
443opt2char(int option)
444{
445 const char *ptr;
446 for (ptr = optflags; option > 1; option >>= 1, ptr++);
447
448 return *ptr;
449}
450
451static char
452cmd2char(int option)
453{
454 const char *ptr;
455 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
456
457 return *ptr;
458}
459
460static void
461add_command(int *cmd, const int newcmd, const int othercmds, int invert)
462{
463 if (invert)
464 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
465 if (*cmd & (~othercmds))
466 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
467 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
468 *cmd |= newcmd;
469}
470
471int
472check_inverse(const char option[], int *invert)
473{
474 if (option && strcmp(option, "!") == 0) {
475 if (*invert)
476 exit_error(PARAMETER_PROBLEM,
477 "Multiple `!' flags not allowed");
478
479 *invert = TRUE;
480 return TRUE;
481 }
482 return FALSE;
483}
484
485static void *
486fw_calloc(size_t count, size_t size)
487{
488 void *p;
489
490 if ((p = calloc(count, size)) == NULL) {
491 perror("iptables: calloc failed");
492 exit(1);
493 }
494 return p;
495}
496
497static void *
498fw_malloc(size_t size)
499{
500 void *p;
501
502 if ((p = malloc(size)) == NULL) {
503 perror("iptables: malloc failed");
504 exit(1);
505 }
506 return p;
507}
508
509static struct in_addr *
510host_to_addr(const char *name, unsigned int *naddr)
511{
512 struct hostent *host;
513 struct in_addr *addr;
514 unsigned int i;
515
516 *naddr = 0;
517 if ((host = gethostbyname(name)) != NULL) {
518 if (host->h_addrtype != AF_INET ||
519 host->h_length != sizeof(struct in_addr))
520 return (struct in_addr *) NULL;
521
522 while (host->h_addr_list[*naddr] != (char *) NULL)
523 (*naddr)++;
524 addr = fw_calloc(*naddr, sizeof(struct in_addr));
525 for (i = 0; i < *naddr; i++)
526 inaddrcpy(&(addr[i]),
527 (struct in_addr *) host->h_addr_list[i]);
528 return addr;
529 }
530
531 return (struct in_addr *) NULL;
532}
533
534static char *
535addr_to_host(const struct in_addr *addr)
536{
537 struct hostent *host;
538
539 if ((host = gethostbyaddr((char *) addr,
540 sizeof(struct in_addr), AF_INET)) != NULL)
541 return (char *) host->h_name;
542
543 return (char *) NULL;
544}
545
546/*
547 * All functions starting with "parse" should succeed, otherwise
548 * the program fails.
549 * Most routines return pointers to static data that may change
550 * between calls to the same or other routines with a few exceptions:
551 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
552 * return global static data.
553*/
554
555static struct in_addr *
556parse_hostnetwork(const char *name, unsigned int *naddrs)
557{
558 struct in_addr *addrp, *addrptmp;
559
560 if ((addrptmp = dotted_to_addr(name)) != NULL ||
561 (addrptmp = network_to_addr(name)) != NULL) {
562 addrp = fw_malloc(sizeof(struct in_addr));
563 inaddrcpy(addrp, addrptmp);
564 *naddrs = 1;
565 return addrp;
566 }
567 if ((addrp = host_to_addr(name, naddrs)) != NULL)
568 return addrp;
569
570 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
571}
572
573static struct in_addr *
574parse_mask(char *mask)
575{
576 static struct in_addr maskaddr;
577 struct in_addr *addrp;
578 int bits;
579
580 if (mask == NULL) {
581 /* no mask at all defaults to 32 bits */
582 maskaddr.s_addr = 0xFFFFFFFF;
583 return &maskaddr;
584 }
585 if ((addrp = dotted_to_addr(mask)) != NULL)
586 /* dotted_to_addr already returns a network byte order addr */
587 return addrp;
588 if ((bits = string_to_number(mask, 0, 32)) == -1)
589 exit_error(PARAMETER_PROBLEM,
590 "invalid mask `%s' specified", mask);
591 if (bits != 0) {
592 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
593 return &maskaddr;
594 }
595
596 maskaddr.s_addr = 0L;
597 return &maskaddr;
598}
599
600static void
601parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
602 struct in_addr *maskp, unsigned int *naddrs)
603{
604 struct in_addr *addrp;
605 char buf[256];
606 char *p;
607 int i, j, k, n;
608
609 strncpy(buf, name, sizeof(buf) - 1);
610 if ((p = strrchr(buf, '/')) != NULL) {
611 *p = '\0';
612 addrp = parse_mask(p + 1);
613 } else
614 addrp = parse_mask(NULL);
615 inaddrcpy(maskp, addrp);
616
617 /* if a null mask is given, the name is ignored, like in "any/0" */
618 if (maskp->s_addr == 0L)
619 strcpy(buf, "0.0.0.0");
620
621 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
622 n = *naddrs;
623 for (i = 0, j = 0; i < n; i++) {
624 addrp[j++].s_addr &= maskp->s_addr;
625 for (k = 0; k < j - 1; k++) {
626 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
627 (*naddrs)--;
628 j--;
629 break;
630 }
631 }
632 }
633}
634
635struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000636find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000637{
638 struct iptables_match *ptr;
639
640 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
641 if (strcmp(name, ptr->name) == 0)
642 break;
643 }
644
Rusty Russell52a51492000-05-02 16:44:29 +0000645 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000646 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
647 + strlen(name)];
648 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000649 if (dlopen(path, RTLD_NOW)) {
650 /* Found library. If it didn't register itself,
651 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000652 ptr = find_match(name, DONT_LOAD);
653
Rusty Russell9e1d2142000-04-23 09:11:12 +0000654 if (!ptr)
655 exit_error(PARAMETER_PROBLEM,
656 "Couldn't load match `%s'\n",
657 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000658 } else if (tryload == LOAD_MUST_SUCCEED)
659 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000660 "Couldn't load match `%s':%s\n",
661 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000662 }
663
Harald Welteae1ff9f2000-12-01 14:26:20 +0000664 if (ptr)
665 ptr->used = 1;
666
Marc Bouchere6869a82000-03-20 06:03:29 +0000667 return ptr;
668}
669
Rusty Russell28381a42000-05-10 00:19:50 +0000670/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
671static struct iptables_match *
672find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
673{
674 int proto;
675
676 proto = string_to_number(pname, 0, 255);
677 if (proto != -1)
678 return find_match(proto_to_name(proto, nolookup), tryload);
679
680 return find_match(pname, tryload);
681}
682
Marc Bouchere6869a82000-03-20 06:03:29 +0000683static u_int16_t
684parse_protocol(const char *s)
685{
Rusty Russell28381a42000-05-10 00:19:50 +0000686 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000687
688 if (proto == -1) {
689 struct protoent *pent;
690
691 if ((pent = getprotobyname(s)))
692 proto = pent->p_proto;
693 else {
694 unsigned int i;
695 for (i = 0;
696 i < sizeof(chain_protos)/sizeof(struct pprot);
697 i++) {
698 if (strcmp(s, chain_protos[i].name) == 0) {
699 proto = chain_protos[i].num;
700 break;
701 }
702 }
703 if (i == sizeof(chain_protos)/sizeof(struct pprot))
704 exit_error(PARAMETER_PROBLEM,
705 "unknown protocol `%s' specified",
706 s);
707 }
708 }
709
710 return (u_int16_t)proto;
711}
712
713static void
714parse_interface(const char *arg, char *vianame, unsigned char *mask)
715{
716 int vialen = strlen(arg);
717 unsigned int i;
718
719 memset(mask, 0, IFNAMSIZ);
720 memset(vianame, 0, IFNAMSIZ);
721
722 if (vialen + 1 > IFNAMSIZ)
723 exit_error(PARAMETER_PROBLEM,
724 "interface name `%s' must be shorter than IFNAMSIZ"
725 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000726
Marc Bouchere6869a82000-03-20 06:03:29 +0000727 strcpy(vianame, arg);
728 if (vialen == 0)
729 memset(mask, 0, IFNAMSIZ);
730 else if (vianame[vialen - 1] == '+') {
731 memset(mask, 0xFF, vialen - 1);
732 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
733 /* Remove `+' */
734 vianame[vialen - 1] = '\0';
735 } else {
736 /* Include nul-terminator in match */
737 memset(mask, 0xFF, vialen + 1);
738 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
739 }
740 for (i = 0; vianame[i]; i++) {
741 if (!isalnum(vianame[i])) {
742 printf("Warning: wierd character in interface"
743 " `%s' (No aliases, :, ! or *).\n",
744 vianame);
745 break;
746 }
747 }
748}
749
750/* Can't be zero. */
751static int
752parse_rulenumber(const char *rule)
753{
754 int rulenum = string_to_number(rule, 1, INT_MAX);
755
756 if (rulenum == -1)
757 exit_error(PARAMETER_PROBLEM,
758 "Invalid rule number `%s'", rule);
759
760 return rulenum;
761}
762
763static const char *
764parse_target(const char *targetname)
765{
766 const char *ptr;
767
768 if (strlen(targetname) < 1)
769 exit_error(PARAMETER_PROBLEM,
770 "Invalid target name (too short)");
771
772 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
773 exit_error(PARAMETER_PROBLEM,
774 "Invalid target name `%s' (%i chars max)",
775 targetname, sizeof(ipt_chainlabel)-1);
776
777 for (ptr = targetname; *ptr; ptr++)
778 if (isspace(*ptr))
779 exit_error(PARAMETER_PROBLEM,
780 "Invalid target name `%s'", targetname);
781 return targetname;
782}
783
784static char *
785addr_to_network(const struct in_addr *addr)
786{
787 struct netent *net;
788
789 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
790 return (char *) net->n_name;
791
792 return (char *) NULL;
793}
794
795char *
796addr_to_dotted(const struct in_addr *addrp)
797{
798 static char buf[20];
799 const unsigned char *bytep;
800
801 bytep = (const unsigned char *) &(addrp->s_addr);
802 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
803 return buf;
804}
805static char *
806addr_to_anyname(const struct in_addr *addr)
807{
808 char *name;
809
810 if ((name = addr_to_host(addr)) != NULL ||
811 (name = addr_to_network(addr)) != NULL)
812 return name;
813
814 return addr_to_dotted(addr);
815}
816
817static char *
818mask_to_dotted(const struct in_addr *mask)
819{
820 int i;
821 static char buf[20];
822 u_int32_t maskaddr, bits;
823
824 maskaddr = ntohl(mask->s_addr);
825
826 if (maskaddr == 0xFFFFFFFFL)
827 /* we don't want to see "/32" */
828 return "";
829
830 i = 32;
831 bits = 0xFFFFFFFEL;
832 while (--i >= 0 && maskaddr != bits)
833 bits <<= 1;
834 if (i >= 0)
835 sprintf(buf, "/%d", i);
836 else
837 /* mask was not a decent combination of 1's and 0's */
838 sprintf(buf, "/%s", addr_to_dotted(mask));
839
840 return buf;
841}
842
843int
844string_to_number(const char *s, int min, int max)
845{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000846 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000847 char *end;
848
849 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000850 errno = 0;
851 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000852 if (*end == '\0' && end != s) {
853 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000854 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000855 return number;
856 }
857 return -1;
858}
859
860static void
861set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
862 int invert)
863{
864 if (*options & option)
865 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
866 opt2char(option));
867 *options |= option;
868
869 if (invert) {
870 unsigned int i;
871 for (i = 0; 1 << i != option; i++);
872
873 if (!inverse_for_options[i])
874 exit_error(PARAMETER_PROBLEM,
875 "cannot have ! before -%c",
876 opt2char(option));
877 *invflg |= inverse_for_options[i];
878 }
879}
880
881struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000882find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000883{
884 struct iptables_target *ptr;
885
886 /* Standard target? */
887 if (strcmp(name, "") == 0
888 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
889 || strcmp(name, IPTC_LABEL_DROP) == 0
890 || strcmp(name, IPTC_LABEL_QUEUE) == 0
891 || strcmp(name, IPTC_LABEL_RETURN) == 0)
892 name = "standard";
893
894 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
895 if (strcmp(name, ptr->name) == 0)
896 break;
897 }
898
Rusty Russell52a51492000-05-02 16:44:29 +0000899 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000900 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
901 + strlen(name)];
902 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000903 if (dlopen(path, RTLD_NOW)) {
904 /* Found library. If it didn't register itself,
905 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000906 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000907 if (!ptr)
908 exit_error(PARAMETER_PROBLEM,
909 "Couldn't load target `%s'\n",
910 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000911 } else if (tryload == LOAD_MUST_SUCCEED)
912 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000913 "Couldn't load target `%s':%s\n",
914 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000915 }
916
Harald Welteae1ff9f2000-12-01 14:26:20 +0000917 if (ptr)
918 ptr->used = 1;
919
Marc Bouchere6869a82000-03-20 06:03:29 +0000920 return ptr;
921}
922
923static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000924merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000925 unsigned int *option_offset)
926{
927 unsigned int num_old, num_new, i;
928 struct option *merge;
929
930 for (num_old = 0; oldopts[num_old].name; num_old++);
931 for (num_new = 0; newopts[num_new].name; num_new++);
932
933 global_option_offset += OPTION_OFFSET;
934 *option_offset = global_option_offset;
935
936 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
937 memcpy(merge, oldopts, num_old * sizeof(struct option));
938 for (i = 0; i < num_new; i++) {
939 merge[num_old + i] = newopts[i];
940 merge[num_old + i].val += *option_offset;
941 }
942 memset(merge + num_old + num_new, 0, sizeof(struct option));
943
944 return merge;
945}
946
947void
948register_match(struct iptables_match *me)
949{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000950 struct iptables_match **i;
951
Marc Bouchere6869a82000-03-20 06:03:29 +0000952 if (strcmp(me->version, program_version) != 0) {
953 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
954 program_name, me->name, me->version, program_version);
955 exit(1);
956 }
957
Rusty Russell52a51492000-05-02 16:44:29 +0000958 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000959 fprintf(stderr, "%s: match `%s' already registered.\n",
960 program_name, me->name);
961 exit(1);
962 }
963
Rusty Russell73f72f52000-07-03 10:17:57 +0000964 if (me->size != IPT_ALIGN(me->size)) {
965 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
966 program_name, me->name, me->size);
967 exit(1);
968 }
969
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000970 /* Append to list. */
971 for (i = &iptables_matches; *i; i = &(*i)->next);
972 me->next = NULL;
973 *i = me;
974
Marc Bouchere6869a82000-03-20 06:03:29 +0000975 me->m = NULL;
976 me->mflags = 0;
977
978 opts = merge_options(opts, me->extra_opts, &me->option_offset);
979}
980
981void
982register_target(struct iptables_target *me)
983{
984 if (strcmp(me->version, program_version) != 0) {
985 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
986 program_name, me->name, me->version, program_version);
987 exit(1);
988 }
989
Rusty Russell52a51492000-05-02 16:44:29 +0000990 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000991 fprintf(stderr, "%s: target `%s' already registered.\n",
992 program_name, me->name);
993 exit(1);
994 }
995
Rusty Russell73f72f52000-07-03 10:17:57 +0000996 if (me->size != IPT_ALIGN(me->size)) {
997 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
998 program_name, me->name, me->size);
999 exit(1);
1000 }
1001
Marc Bouchere6869a82000-03-20 06:03:29 +00001002 /* Prepend to list. */
1003 me->next = iptables_targets;
1004 iptables_targets = me;
1005 me->t = NULL;
1006 me->tflags = 0;
1007
1008 opts = merge_options(opts, me->extra_opts, &me->option_offset);
1009}
1010
1011static void
1012print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1013{
1014 struct ipt_counters counters;
1015 const char *pol = iptc_get_policy(chain, &counters, handle);
1016 printf("Chain %s", chain);
1017 if (pol) {
1018 printf(" (policy %s", pol);
1019 if (!(format & FMT_NOCOUNTS))
1020 printf(" %llu packets, %llu bytes",
1021 counters.pcnt, counters.bcnt);
1022 printf(")\n");
1023 } else {
1024 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001025 if (!iptc_get_references(&refs, chain, handle))
1026 printf(" (ERROR obtaining refs)\n");
1027 else
1028 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001029 }
1030
1031 if (format & FMT_LINENUMBERS)
1032 printf(FMT("%-4s ", "%s "), "num");
1033 if (!(format & FMT_NOCOUNTS)) {
1034 if (format & FMT_KILOMEGAGIGA) {
1035 printf(FMT("%5s ","%s "), "pkts");
1036 printf(FMT("%5s ","%s "), "bytes");
1037 } else {
1038 printf(FMT("%8s ","%s "), "pkts");
1039 printf(FMT("%10s ","%s "), "bytes");
1040 }
1041 }
1042 if (!(format & FMT_NOTARGET))
1043 printf(FMT("%-9s ","%s "), "target");
1044 fputs(" prot ", stdout);
1045 if (format & FMT_OPTIONS)
1046 fputs("opt", stdout);
1047 if (format & FMT_VIA) {
1048 printf(FMT(" %-6s ","%s "), "in");
1049 printf(FMT("%-6s ","%s "), "out");
1050 }
1051 printf(FMT(" %-19s ","%s "), "source");
1052 printf(FMT(" %-19s "," %s "), "destination");
1053 printf("\n");
1054}
1055
1056static void
1057print_num(u_int64_t number, unsigned int format)
1058{
1059 if (format & FMT_KILOMEGAGIGA) {
1060 if (number > 99999) {
1061 number = (number + 500) / 1000;
1062 if (number > 9999) {
1063 number = (number + 500) / 1000;
1064 if (number > 9999) {
1065 number = (number + 500) / 1000;
1066 printf(FMT("%4lluG ","%lluG "),number);
1067 }
1068 else printf(FMT("%4lluM ","%lluM "), number);
1069 } else
1070 printf(FMT("%4lluK ","%lluK "), number);
1071 } else
1072 printf(FMT("%5llu ","%llu "), number);
1073 } else
1074 printf(FMT("%8llu ","%llu "), number);
1075}
1076
1077static int
1078print_match(const struct ipt_entry_match *m,
1079 const struct ipt_ip *ip,
1080 int numeric)
1081{
Rusty Russell52a51492000-05-02 16:44:29 +00001082 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001083
1084 if (match) {
1085 if (match->print)
1086 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001087 else
Rusty Russellb039b022000-09-01 06:04:05 +00001088 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001089 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001090 if (m->u.user.name[0])
1091 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001092 }
1093 /* Don't stop iterating. */
1094 return 0;
1095}
1096
1097/* e is called `fw' here for hysterical raisins */
1098static void
1099print_firewall(const struct ipt_entry *fw,
1100 const char *targname,
1101 unsigned int num,
1102 unsigned int format,
1103 const iptc_handle_t handle)
1104{
1105 struct iptables_target *target = NULL;
1106 const struct ipt_entry_target *t;
1107 u_int8_t flags;
1108 char buf[BUFSIZ];
1109
1110 /* User creates a chain called "REJECT": this overrides the
1111 `REJECT' target module. Keep feeding them rope until the
1112 revolution... Bwahahahahah */
1113 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001114 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001115 else
Rusty Russell52a51492000-05-02 16:44:29 +00001116 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001117
1118 t = ipt_get_target((struct ipt_entry *)fw);
1119 flags = fw->ip.flags;
1120
1121 if (format & FMT_LINENUMBERS)
1122 printf(FMT("%-4u ", "%u "), num+1);
1123
1124 if (!(format & FMT_NOCOUNTS)) {
1125 print_num(fw->counters.pcnt, format);
1126 print_num(fw->counters.bcnt, format);
1127 }
1128
1129 if (!(format & FMT_NOTARGET))
1130 printf(FMT("%-9s ", "%s "), targname);
1131
1132 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1133 {
Rusty Russell28381a42000-05-10 00:19:50 +00001134 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001135 if (pname)
1136 printf(FMT("%-5s", "%s "), pname);
1137 else
1138 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1139 }
1140
1141 if (format & FMT_OPTIONS) {
1142 if (format & FMT_NOTABLE)
1143 fputs("opt ", stdout);
1144 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1145 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1146 fputc(' ', stdout);
1147 }
1148
1149 if (format & FMT_VIA) {
1150 char iface[IFNAMSIZ+2];
1151
1152 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1153 iface[0] = '!';
1154 iface[1] = '\0';
1155 }
1156 else iface[0] = '\0';
1157
1158 if (fw->ip.iniface[0] != '\0') {
1159 strcat(iface, fw->ip.iniface);
1160 /* If it doesn't compare the nul-term, it's a
1161 wildcard. */
1162 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1163 strcat(iface, "+");
1164 }
1165 else if (format & FMT_NUMERIC) strcat(iface, "*");
1166 else strcat(iface, "any");
1167 printf(FMT(" %-6s ","in %s "), iface);
1168
1169 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1170 iface[0] = '!';
1171 iface[1] = '\0';
1172 }
1173 else iface[0] = '\0';
1174
1175 if (fw->ip.outiface[0] != '\0') {
1176 strcat(iface, fw->ip.outiface);
1177 /* If it doesn't compare the nul-term, it's a
1178 wildcard. */
1179 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1180 strcat(iface, "+");
1181 }
1182 else if (format & FMT_NUMERIC) strcat(iface, "*");
1183 else strcat(iface, "any");
1184 printf(FMT("%-6s ","out %s "), iface);
1185 }
1186
1187 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1188 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1189 printf(FMT("%-19s ","%s "), "anywhere");
1190 else {
1191 if (format & FMT_NUMERIC)
1192 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1193 else
1194 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1195 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1196 printf(FMT("%-19s ","%s "), buf);
1197 }
1198
1199 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1200 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1201 printf(FMT("%-19s","-> %s"), "anywhere");
1202 else {
1203 if (format & FMT_NUMERIC)
1204 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1205 else
1206 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1207 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1208 printf(FMT("%-19s","-> %s"), buf);
1209 }
1210
1211 if (format & FMT_NOTABLE)
1212 fputs(" ", stdout);
1213
1214 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1215
1216 if (target) {
1217 if (target->print)
1218 /* Print the target information. */
1219 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001220 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001221 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001222 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001223
1224 if (!(format & FMT_NONEWLINE))
1225 fputc('\n', stdout);
1226}
1227
1228static void
1229print_firewall_line(const struct ipt_entry *fw,
1230 const iptc_handle_t h)
1231{
1232 struct ipt_entry_target *t;
1233
1234 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001235 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001236}
1237
1238static int
1239append_entry(const ipt_chainlabel chain,
1240 struct ipt_entry *fw,
1241 unsigned int nsaddrs,
1242 const struct in_addr saddrs[],
1243 unsigned int ndaddrs,
1244 const struct in_addr daddrs[],
1245 int verbose,
1246 iptc_handle_t *handle)
1247{
1248 unsigned int i, j;
1249 int ret = 1;
1250
1251 for (i = 0; i < nsaddrs; i++) {
1252 fw->ip.src.s_addr = saddrs[i].s_addr;
1253 for (j = 0; j < ndaddrs; j++) {
1254 fw->ip.dst.s_addr = daddrs[j].s_addr;
1255 if (verbose)
1256 print_firewall_line(fw, *handle);
1257 ret &= iptc_append_entry(chain, fw, handle);
1258 }
1259 }
1260
1261 return ret;
1262}
1263
1264static int
1265replace_entry(const ipt_chainlabel chain,
1266 struct ipt_entry *fw,
1267 unsigned int rulenum,
1268 const struct in_addr *saddr,
1269 const struct in_addr *daddr,
1270 int verbose,
1271 iptc_handle_t *handle)
1272{
1273 fw->ip.src.s_addr = saddr->s_addr;
1274 fw->ip.dst.s_addr = daddr->s_addr;
1275
1276 if (verbose)
1277 print_firewall_line(fw, *handle);
1278 return iptc_replace_entry(chain, fw, rulenum, handle);
1279}
1280
1281static int
1282insert_entry(const ipt_chainlabel chain,
1283 struct ipt_entry *fw,
1284 unsigned int rulenum,
1285 unsigned int nsaddrs,
1286 const struct in_addr saddrs[],
1287 unsigned int ndaddrs,
1288 const struct in_addr daddrs[],
1289 int verbose,
1290 iptc_handle_t *handle)
1291{
1292 unsigned int i, j;
1293 int ret = 1;
1294
1295 for (i = 0; i < nsaddrs; i++) {
1296 fw->ip.src.s_addr = saddrs[i].s_addr;
1297 for (j = 0; j < ndaddrs; j++) {
1298 fw->ip.dst.s_addr = daddrs[j].s_addr;
1299 if (verbose)
1300 print_firewall_line(fw, *handle);
1301 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1302 }
1303 }
1304
1305 return ret;
1306}
1307
Rusty Russell2e0a3212000-04-19 11:23:18 +00001308static unsigned char *
1309make_delete_mask(struct ipt_entry *fw)
1310{
1311 /* Establish mask for comparison */
1312 unsigned int size;
1313 struct iptables_match *m;
1314 unsigned char *mask, *mptr;
1315
1316 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001317 for (m = iptables_matches; m && m->used; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001318 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001319
Rusty Russell9e1d2142000-04-23 09:11:12 +00001320 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001321 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001322 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001323
Rusty Russell9e1d2142000-04-23 09:11:12 +00001324 memset(mask, 0xFF, sizeof(struct ipt_entry));
1325 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001326
Harald Welteae1ff9f2000-12-01 14:26:20 +00001327 for (m = iptables_matches; m && m->used; m = m->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001328 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001329 IPT_ALIGN(sizeof(struct ipt_entry_match))
1330 + m->userspacesize);
1331 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001332 }
1333
Rusty Russell73f72f52000-07-03 10:17:57 +00001334 memset(mptr, 0xFF,
1335 IPT_ALIGN(sizeof(struct ipt_entry_target))
1336 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001337
1338 return mask;
1339}
1340
Marc Bouchere6869a82000-03-20 06:03:29 +00001341static int
1342delete_entry(const ipt_chainlabel chain,
1343 struct ipt_entry *fw,
1344 unsigned int nsaddrs,
1345 const struct in_addr saddrs[],
1346 unsigned int ndaddrs,
1347 const struct in_addr daddrs[],
1348 int verbose,
1349 iptc_handle_t *handle)
1350{
1351 unsigned int i, j;
1352 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001353 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Rusty Russell2e0a3212000-04-19 11:23:18 +00001355 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001356 for (i = 0; i < nsaddrs; i++) {
1357 fw->ip.src.s_addr = saddrs[i].s_addr;
1358 for (j = 0; j < ndaddrs; j++) {
1359 fw->ip.dst.s_addr = daddrs[j].s_addr;
1360 if (verbose)
1361 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001362 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001363 }
1364 }
1365 return ret;
1366}
1367
1368static int
1369check_packet(const ipt_chainlabel chain,
1370 struct ipt_entry *fw,
1371 unsigned int nsaddrs,
1372 const struct in_addr saddrs[],
1373 unsigned int ndaddrs,
1374 const struct in_addr daddrs[],
1375 int verbose,
1376 iptc_handle_t *handle)
1377{
1378 int ret = 1;
1379 unsigned int i, j;
1380 const char *msg;
1381
1382 for (i = 0; i < nsaddrs; i++) {
1383 fw->ip.src.s_addr = saddrs[i].s_addr;
1384 for (j = 0; j < ndaddrs; j++) {
1385 fw->ip.dst.s_addr = daddrs[j].s_addr;
1386 if (verbose)
1387 print_firewall_line(fw, *handle);
1388 msg = iptc_check_packet(chain, fw, handle);
1389 if (!msg) ret = 0;
1390 else printf("%s\n", msg);
1391 }
1392 }
1393
1394 return ret;
1395}
1396
Harald Welteae1ff9f2000-12-01 14:26:20 +00001397int
Marc Bouchere6869a82000-03-20 06:03:29 +00001398for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001399 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001400{
1401 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001402 const char *chain;
1403 char *chains;
1404 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001405
Rusty Russell9e1d2142000-04-23 09:11:12 +00001406 chain = iptc_first_chain(handle);
1407 while (chain) {
1408 chaincount++;
1409 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001410 }
1411
Rusty Russell9e1d2142000-04-23 09:11:12 +00001412 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1413 i = 0;
1414 chain = iptc_first_chain(handle);
1415 while (chain) {
1416 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1417 i++;
1418 chain = iptc_next_chain(handle);
1419 }
1420
1421 for (i = 0; i < chaincount; i++) {
1422 if (!builtinstoo
1423 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1424 *handle))
1425 continue;
1426 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1427 }
1428
1429 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 return ret;
1431}
1432
Harald Welteae1ff9f2000-12-01 14:26:20 +00001433int
Marc Bouchere6869a82000-03-20 06:03:29 +00001434flush_entries(const ipt_chainlabel chain, int verbose,
1435 iptc_handle_t *handle)
1436{
1437 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001438 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001439
1440 if (verbose)
1441 fprintf(stdout, "Flushing chain `%s'\n", chain);
1442 return iptc_flush_entries(chain, handle);
1443}
Marc Bouchere6869a82000-03-20 06:03:29 +00001444
1445static int
1446zero_entries(const ipt_chainlabel chain, int verbose,
1447 iptc_handle_t *handle)
1448{
1449 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001450 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001451
Marc Bouchere6869a82000-03-20 06:03:29 +00001452 if (verbose)
1453 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1454 return iptc_zero_entries(chain, handle);
1455}
1456
Harald Welteae1ff9f2000-12-01 14:26:20 +00001457int
Marc Bouchere6869a82000-03-20 06:03:29 +00001458delete_chain(const ipt_chainlabel chain, int verbose,
1459 iptc_handle_t *handle)
1460{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001461 if (!chain)
1462 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001463
1464 if (verbose)
1465 fprintf(stdout, "Deleting chain `%s'\n", chain);
1466 return iptc_delete_chain(chain, handle);
1467}
1468
1469static int
1470list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1471 int expanded, int linenumbers, iptc_handle_t *handle)
1472{
1473 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001474 unsigned int format;
1475 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001476
1477 format = FMT_OPTIONS;
1478 if (!verbose)
1479 format |= FMT_NOCOUNTS;
1480 else
1481 format |= FMT_VIA;
1482
1483 if (numeric)
1484 format |= FMT_NUMERIC;
1485
1486 if (!expanded)
1487 format |= FMT_KILOMEGAGIGA;
1488
1489 if (linenumbers)
1490 format |= FMT_LINENUMBERS;
1491
Rusty Russell9e1d2142000-04-23 09:11:12 +00001492 for (this = iptc_first_chain(handle);
1493 this;
1494 this = iptc_next_chain(handle)) {
1495 const struct ipt_entry *i;
1496 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001497
Marc Bouchere6869a82000-03-20 06:03:29 +00001498 if (chain && strcmp(chain, this) != 0)
1499 continue;
1500
1501 if (found) printf("\n");
1502
1503 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001504 i = iptc_first_rule(this, handle);
1505
1506 num = 0;
1507 while (i) {
1508 print_firewall(i,
1509 iptc_get_target(i, handle),
1510 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001511 format,
1512 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001513 i = iptc_next_rule(i, handle);
1514 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001515 found = 1;
1516 }
1517
1518 errno = ENOENT;
1519 return found;
1520}
1521
Harald Welte82dd2ec2000-12-19 05:18:15 +00001522static char *get_modprobe(void)
1523{
1524 int procfile;
1525 char *ret;
1526
1527 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1528 if (procfile < 0)
1529 return NULL;
1530
1531 ret = malloc(1024);
1532 if (ret) {
1533 switch (read(procfile, ret, 1024)) {
1534 case -1: goto fail;
1535 case 1024: goto fail; /* Partial read. Wierd */
1536 }
1537 return ret;
1538 }
1539 fail:
1540 free(ret);
1541 close(procfile);
1542 return NULL;
1543}
1544
1545static int iptables_insmod(const char *modname, const char *modprobe)
1546{
1547 char *buf = NULL;
1548 char *argv[3];
1549
1550 /* If they don't explicitly set it, read out of kernel */
1551 if (!modprobe) {
1552 buf = get_modprobe();
1553 if (!buf)
1554 return -1;
1555 modprobe = buf;
1556 }
1557
1558 switch (fork()) {
1559 case 0:
1560 argv[0] = (char *)modprobe;
1561 argv[1] = (char *)modname;
1562 argv[2] = NULL;
1563 execv(argv[0], argv);
1564
1565 /* not usually reached */
1566 exit(0);
1567 case -1:
1568 return -1;
1569
1570 default: /* parent */
1571 wait(NULL);
1572 }
1573
1574 free(buf);
1575 return 0;
1576}
1577
Marc Bouchere6869a82000-03-20 06:03:29 +00001578static struct ipt_entry *
1579generate_entry(const struct ipt_entry *fw,
1580 struct iptables_match *matches,
1581 struct ipt_entry_target *target)
1582{
1583 unsigned int size;
1584 struct iptables_match *m;
1585 struct ipt_entry *e;
1586
1587 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001588 for (m = matches; m && m->used; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001589 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001590
Rusty Russell228e98d2000-04-27 10:28:06 +00001591 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001592 *e = *fw;
1593 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001594 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001595
1596 size = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001597 for (m = matches; m && m->used; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001598 memcpy(e->elems + size, m->m, m->m->u.match_size);
1599 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001600 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001601 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001602
1603 return e;
1604}
1605
1606int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1607{
1608 struct ipt_entry fw, *e = NULL;
1609 int invert = 0;
1610 unsigned int nsaddrs = 0, ndaddrs = 0;
1611 struct in_addr *saddrs = NULL, *daddrs = NULL;
1612
1613 int c, verbose = 0;
1614 const char *chain = NULL;
1615 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1616 const char *policy = NULL, *newname = NULL;
1617 unsigned int rulenum = 0, options = 0, command = 0;
1618 int ret = 1;
1619 struct iptables_match *m;
1620 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001621 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001622 const char *jumpto = "";
1623 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001624 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001625
1626 memset(&fw, 0, sizeof(fw));
1627
Harald Welteae1ff9f2000-12-01 14:26:20 +00001628 /* re-set optind to 0 in case do_command gets called
1629 * a second time */
1630 optind = 0;
1631
1632 /* clear mflags in case do_command gets called a second time
1633 * (we clear the global list of all matches for security)*/
1634 for (m = iptables_matches; m; m = m->next) {
1635 m->mflags = 0;
1636 m->used = 0;
1637 }
1638
1639 for (t = iptables_targets; t; t = t->next) {
1640 t->tflags = 0;
1641 t->used = 0;
1642 }
1643
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 /* Suppress error messages: we may add new options if we
1645 demand-load a protocol. */
1646 opterr = 0;
1647
1648 while ((c = getopt_long(argc, argv,
1649 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1650 opts, NULL)) != -1) {
1651 switch (c) {
1652 /*
1653 * Command selection
1654 */
1655 case 'A':
1656 add_command(&command, CMD_APPEND, CMD_NONE,
1657 invert);
1658 chain = optarg;
1659 break;
1660
1661 case 'D':
1662 add_command(&command, CMD_DELETE, CMD_NONE,
1663 invert);
1664 chain = optarg;
1665 if (optind < argc && argv[optind][0] != '-'
1666 && argv[optind][0] != '!') {
1667 rulenum = parse_rulenumber(argv[optind++]);
1668 command = CMD_DELETE_NUM;
1669 }
1670 break;
1671
1672 case 'C':
1673 add_command(&command, CMD_CHECK, CMD_NONE,
1674 invert);
1675 chain = optarg;
1676 break;
1677
1678 case 'R':
1679 add_command(&command, CMD_REPLACE, CMD_NONE,
1680 invert);
1681 chain = optarg;
1682 if (optind < argc && argv[optind][0] != '-'
1683 && argv[optind][0] != '!')
1684 rulenum = parse_rulenumber(argv[optind++]);
1685 else
1686 exit_error(PARAMETER_PROBLEM,
1687 "-%c requires a rule number",
1688 cmd2char(CMD_REPLACE));
1689 break;
1690
1691 case 'I':
1692 add_command(&command, CMD_INSERT, CMD_NONE,
1693 invert);
1694 chain = optarg;
1695 if (optind < argc && argv[optind][0] != '-'
1696 && argv[optind][0] != '!')
1697 rulenum = parse_rulenumber(argv[optind++]);
1698 else rulenum = 1;
1699 break;
1700
1701 case 'L':
1702 add_command(&command, CMD_LIST, CMD_ZERO,
1703 invert);
1704 if (optarg) chain = optarg;
1705 else if (optind < argc && argv[optind][0] != '-'
1706 && argv[optind][0] != '!')
1707 chain = argv[optind++];
1708 break;
1709
1710 case 'F':
1711 add_command(&command, CMD_FLUSH, CMD_NONE,
1712 invert);
1713 if (optarg) chain = optarg;
1714 else if (optind < argc && argv[optind][0] != '-'
1715 && argv[optind][0] != '!')
1716 chain = argv[optind++];
1717 break;
1718
1719 case 'Z':
1720 add_command(&command, CMD_ZERO, CMD_LIST,
1721 invert);
1722 if (optarg) chain = optarg;
1723 else if (optind < argc && argv[optind][0] != '-'
1724 && argv[optind][0] != '!')
1725 chain = argv[optind++];
1726 break;
1727
1728 case 'N':
1729 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1730 invert);
1731 chain = optarg;
1732 break;
1733
1734 case 'X':
1735 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1736 invert);
1737 if (optarg) chain = optarg;
1738 else if (optind < argc && argv[optind][0] != '-'
1739 && argv[optind][0] != '!')
1740 chain = argv[optind++];
1741 break;
1742
1743 case 'E':
1744 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1745 invert);
1746 chain = optarg;
1747 if (optind < argc && argv[optind][0] != '-'
1748 && argv[optind][0] != '!')
1749 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001750 else
1751 exit_error(PARAMETER_PROBLEM,
1752 "-%c requires old-chain-name and "
1753 "new-chain-name",
1754 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001755 break;
1756
1757 case 'P':
1758 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1759 invert);
1760 chain = optarg;
1761 if (optind < argc && argv[optind][0] != '-'
1762 && argv[optind][0] != '!')
1763 policy = argv[optind++];
1764 else
1765 exit_error(PARAMETER_PROBLEM,
1766 "-%c requires a chain and a policy",
1767 cmd2char(CMD_SET_POLICY));
1768 break;
1769
1770 case 'h':
1771 if (!optarg)
1772 optarg = argv[optind];
1773
Rusty Russell2e0a3212000-04-19 11:23:18 +00001774 /* iptables -p icmp -h */
1775 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001776 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001777
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 exit_printhelp();
1779
1780 /*
1781 * Option selection
1782 */
1783 case 'p':
1784 if (check_inverse(optarg, &invert))
1785 optind++;
1786 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1787 invert);
1788
1789 /* Canonicalize into lower case */
1790 for (protocol = argv[optind-1]; *protocol; protocol++)
1791 *protocol = tolower(*protocol);
1792
1793 protocol = argv[optind-1];
1794 fw.ip.proto = parse_protocol(protocol);
1795
1796 if (fw.ip.proto == 0
1797 && (fw.ip.invflags & IPT_INV_PROTO))
1798 exit_error(PARAMETER_PROBLEM,
1799 "rule would never match protocol");
1800 fw.nfcache |= NFC_IP_PROTO;
1801 break;
1802
1803 case 's':
1804 if (check_inverse(optarg, &invert))
1805 optind++;
1806 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1807 invert);
1808 shostnetworkmask = argv[optind-1];
1809 fw.nfcache |= NFC_IP_SRC;
1810 break;
1811
1812 case 'd':
1813 if (check_inverse(optarg, &invert))
1814 optind++;
1815 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1816 invert);
1817 dhostnetworkmask = argv[optind-1];
1818 fw.nfcache |= NFC_IP_DST;
1819 break;
1820
1821 case 'j':
1822 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1823 invert);
1824 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001825 /* TRY_LOAD (may be chain name) */
1826 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001827
1828 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001829 size_t size;
1830
Rusty Russell73f72f52000-07-03 10:17:57 +00001831 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1832 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001833
Rusty Russell2e0a3212000-04-19 11:23:18 +00001834 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001835 target->t->u.target_size = size;
1836 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001837 target->init(target->t, &fw.nfcache);
1838 }
1839 break;
1840
1841
1842 case 'i':
1843 if (check_inverse(optarg, &invert))
1844 optind++;
1845 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1846 invert);
1847 parse_interface(argv[optind-1],
1848 fw.ip.iniface,
1849 fw.ip.iniface_mask);
1850 fw.nfcache |= NFC_IP_IF_IN;
1851 break;
1852
1853 case 'o':
1854 if (check_inverse(optarg, &invert))
1855 optind++;
1856 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1857 invert);
1858 parse_interface(argv[optind-1],
1859 fw.ip.outiface,
1860 fw.ip.outiface_mask);
1861 fw.nfcache |= NFC_IP_IF_OUT;
1862 break;
1863
1864 case 'f':
1865 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1866 invert);
1867 fw.ip.flags |= IPT_F_FRAG;
1868 fw.nfcache |= NFC_IP_FRAG;
1869 break;
1870
1871 case 'v':
1872 if (!verbose)
1873 set_option(&options, OPT_VERBOSE,
1874 &fw.ip.invflags, invert);
1875 verbose++;
1876 break;
1877
Rusty Russell52a51492000-05-02 16:44:29 +00001878 case 'm': {
1879 size_t size;
1880
Marc Bouchere6869a82000-03-20 06:03:29 +00001881 if (invert)
1882 exit_error(PARAMETER_PROBLEM,
1883 "unexpected ! flag before --match");
1884
Rusty Russell52a51492000-05-02 16:44:29 +00001885 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001886 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1887 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001888 m->m = fw_calloc(1, size);
1889 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001890 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001891 m->init(m->m, &fw.nfcache);
1892 }
1893 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001894
1895 case 'n':
1896 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1897 invert);
1898 break;
1899
1900 case 't':
1901 if (invert)
1902 exit_error(PARAMETER_PROBLEM,
1903 "unexpected ! flag before --table");
1904 *table = argv[optind-1];
1905 break;
1906
1907 case 'x':
1908 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1909 invert);
1910 break;
1911
1912 case 'V':
1913 if (invert)
1914 printf("Not %s ;-)\n", program_version);
1915 else
1916 printf("%s v%s\n",
1917 program_name, program_version);
1918 exit(0);
1919
1920 case '0':
1921 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1922 invert);
1923 break;
1924
Harald Welte82dd2ec2000-12-19 05:18:15 +00001925 case 'M':
1926 modprobe = optarg;
1927 break;
1928
Marc Bouchere6869a82000-03-20 06:03:29 +00001929 case 1: /* non option */
1930 if (optarg[0] == '!' && optarg[1] == '\0') {
1931 if (invert)
1932 exit_error(PARAMETER_PROBLEM,
1933 "multiple consecutive ! not"
1934 " allowed");
1935 invert = TRUE;
1936 optarg[0] = '\0';
1937 continue;
1938 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001939 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001940 exit_tryhelp(2);
1941
1942 default:
1943 /* FIXME: This scheme doesn't allow two of the same
1944 matches --RR */
1945 if (!target
1946 || !(target->parse(c - target->option_offset,
1947 argv, invert,
1948 &target->tflags,
1949 &fw, &target->t))) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001950 for (m = iptables_matches; m && m->used; m = m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001951 if (m->parse(c - m->option_offset,
1952 argv, invert,
1953 &m->mflags,
1954 &fw,
1955 &fw.nfcache,
1956 &m->m))
1957 break;
1958 }
1959
1960 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001961 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001962 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001963 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001964 && !find_proto(protocol, DONT_LOAD,
1965 options&OPT_NUMERIC)
1966 && (m = find_proto(protocol, TRY_LOAD,
1967 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001969 size_t size;
1970
Rusty Russell73f72f52000-07-03 10:17:57 +00001971 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1972 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001973
Rusty Russell2e0a3212000-04-19 11:23:18 +00001974 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001975 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001976 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001977 m->init(m->m, &fw.nfcache);
1978
1979 optind--;
1980 continue;
1981 }
1982 if (!m)
1983 exit_error(PARAMETER_PROBLEM,
1984 "Unknown arg `%s'",
1985 argv[optind-1]);
1986 }
1987 }
1988 invert = FALSE;
1989 }
1990
Harald Welteae1ff9f2000-12-01 14:26:20 +00001991 for (m = iptables_matches; m && m->used; m = m->next)
Marc Bouchere6869a82000-03-20 06:03:29 +00001992 m->final_check(m->mflags);
1993 if (target)
1994 target->final_check(target->tflags);
1995
1996 /* Fix me: must put inverse options checking here --MN */
1997
1998 if (optind < argc)
1999 exit_error(PARAMETER_PROBLEM,
2000 "unknown arguments found on commandline");
2001 if (!command)
2002 exit_error(PARAMETER_PROBLEM, "no command specified");
2003 if (invert)
2004 exit_error(PARAMETER_PROBLEM,
2005 "nothing appropriate following !");
2006
Marc Boucher744bd022000-04-22 22:36:10 +00002007 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2008 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002009 if (!(options & OPT_DESTINATION))
2010 dhostnetworkmask = "0.0.0.0/0";
2011 if (!(options & OPT_SOURCE))
2012 shostnetworkmask = "0.0.0.0/0";
2013 }
2014
2015 if (shostnetworkmask)
2016 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2017 &(fw.ip.smsk), &nsaddrs);
2018
2019 if (dhostnetworkmask)
2020 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2021 &(fw.ip.dmsk), &ndaddrs);
2022
2023 if ((nsaddrs > 1 || ndaddrs > 1) &&
2024 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2025 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2026 " source or destination IP addresses");
2027
2028 if (command == CMD_CHECK && fw.ip.invflags != 0)
2029 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2030 cmd2char(CMD_CHECK));
2031
2032 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2033 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2034 "specify a unique address");
2035
2036 generic_opt_check(command, options);
2037
2038 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2039 exit_error(PARAMETER_PROBLEM,
2040 "chain name `%s' too long (must be under %i chars)",
2041 chain, IPT_FUNCTION_MAXNAMELEN);
2042
Harald Welteae1ff9f2000-12-01 14:26:20 +00002043 /* only allocate handle if we weren't called with a handle */
2044 if (!*handle)
2045 *handle = iptc_init(*table);
2046
Harald Welte82dd2ec2000-12-19 05:18:15 +00002047 if (!*handle) {
2048 /* try to insmod the module if iptc_init failed */
2049 iptables_insmod("ip_tables", modprobe);
2050 *handle = iptc_init(*table);
2051 }
2052
Marc Bouchere6869a82000-03-20 06:03:29 +00002053 if (!*handle)
2054 exit_error(VERSION_PROBLEM,
2055 "can't initialize iptables table `%s': %s",
2056 *table, iptc_strerror(errno));
2057
Marc Boucher744bd022000-04-22 22:36:10 +00002058 if (command == CMD_CHECK
2059 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002060 || command == CMD_DELETE
2061 || command == CMD_INSERT
2062 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002063 if (strcmp(chain, "PREROUTING") == 0
2064 || strcmp(chain, "INPUT") == 0) {
2065 /* -o not valid with incoming packets. */
2066 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 exit_error(PARAMETER_PROBLEM,
2068 "Can't use -%c with %s\n",
2069 opt2char(OPT_VIANAMEOUT),
2070 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002071 /* -i required with -C */
2072 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2073 exit_error(PARAMETER_PROBLEM,
2074 "Need -%c with %s\n",
2075 opt2char(OPT_VIANAMEIN),
2076 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 }
2078
Rusty Russella4860fd2000-06-17 16:13:02 +00002079 if (strcmp(chain, "POSTROUTING") == 0
2080 || strcmp(chain, "OUTPUT") == 0) {
2081 /* -i not valid with outgoing packets */
2082 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002083 exit_error(PARAMETER_PROBLEM,
2084 "Can't use -%c with %s\n",
2085 opt2char(OPT_VIANAMEIN),
2086 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002087 /* -o required with -C */
2088 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2089 exit_error(PARAMETER_PROBLEM,
2090 "Need -%c with %s\n",
2091 opt2char(OPT_VIANAMEOUT),
2092 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002093 }
2094
2095 if (target && iptc_is_chain(jumpto, *handle)) {
2096 printf("Warning: using chain %s, not extension\n",
2097 jumpto);
2098
2099 target = NULL;
2100 }
2101
2102 /* If they didn't specify a target, or it's a chain
2103 name, use standard. */
2104 if (!target
2105 && (strlen(jumpto) == 0
2106 || iptc_is_chain(jumpto, *handle))) {
2107 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002108
Rusty Russell52a51492000-05-02 16:44:29 +00002109 target = find_target(IPT_STANDARD_TARGET,
2110 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002111
2112 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002113 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002114 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002115 target->t->u.target_size = size;
2116 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002117 target->init(target->t, &fw.nfcache);
2118 }
2119
Rusty Russell7e53bf92000-03-20 07:03:28 +00002120 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002121 /* it is no chain, and we can't load a plugin.
2122 * We cannot know if the plugin is corrupt, non
2123 * existant OR if the user just misspelled a
2124 * chain. */
2125 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 } else {
2127 e = generate_entry(&fw, iptables_matches, target->t);
2128 }
2129 }
2130
2131 switch (command) {
2132 case CMD_APPEND:
2133 ret = append_entry(chain, e,
2134 nsaddrs, saddrs, ndaddrs, daddrs,
2135 options&OPT_VERBOSE,
2136 handle);
2137 break;
2138 case CMD_CHECK:
2139 ret = check_packet(chain, e,
2140 nsaddrs, saddrs, ndaddrs, daddrs,
2141 options&OPT_VERBOSE, handle);
2142 break;
2143 case CMD_DELETE:
2144 ret = delete_entry(chain, e,
2145 nsaddrs, saddrs, ndaddrs, daddrs,
2146 options&OPT_VERBOSE,
2147 handle);
2148 break;
2149 case CMD_DELETE_NUM:
2150 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2151 break;
2152 case CMD_REPLACE:
2153 ret = replace_entry(chain, e, rulenum - 1,
2154 saddrs, daddrs, options&OPT_VERBOSE,
2155 handle);
2156 break;
2157 case CMD_INSERT:
2158 ret = insert_entry(chain, e, rulenum - 1,
2159 nsaddrs, saddrs, ndaddrs, daddrs,
2160 options&OPT_VERBOSE,
2161 handle);
2162 break;
2163 case CMD_LIST:
2164 ret = list_entries(chain,
2165 options&OPT_VERBOSE,
2166 options&OPT_NUMERIC,
2167 options&OPT_EXPANDED,
2168 options&OPT_LINENUMBERS,
2169 handle);
2170 break;
2171 case CMD_FLUSH:
2172 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2173 break;
2174 case CMD_ZERO:
2175 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2176 break;
2177 case CMD_LIST|CMD_ZERO:
2178 ret = list_entries(chain,
2179 options&OPT_VERBOSE,
2180 options&OPT_NUMERIC,
2181 options&OPT_EXPANDED,
2182 options&OPT_LINENUMBERS,
2183 handle);
2184 if (ret)
2185 ret = zero_entries(chain,
2186 options&OPT_VERBOSE, handle);
2187 break;
2188 case CMD_NEW_CHAIN:
2189 ret = iptc_create_chain(chain, handle);
2190 break;
2191 case CMD_DELETE_CHAIN:
2192 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2193 break;
2194 case CMD_RENAME_CHAIN:
2195 ret = iptc_rename_chain(chain, newname, handle);
2196 break;
2197 case CMD_SET_POLICY:
2198 ret = iptc_set_policy(chain, policy, handle);
2199 break;
2200 default:
2201 /* We should never reach this... */
2202 exit_tryhelp(2);
2203 }
2204
2205 if (verbose > 1)
2206 dump_entries(*handle);
2207
2208 return ret;
2209}