blob: 4dbe8917d54c5ed20459c236fd50599e2036c68c [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>
31#include <iptables.h>
32
33#ifndef TRUE
34#define TRUE 1
35#endif
36#ifndef FALSE
37#define FALSE 0
38#endif
39
40#ifndef IPT_LIB_DIR
41#define IPT_LIB_DIR "/usr/local/lib/iptables"
42#endif
43
44#define FMT_NUMERIC 0x0001
45#define FMT_NOCOUNTS 0x0002
46#define FMT_KILOMEGAGIGA 0x0004
47#define FMT_OPTIONS 0x0008
48#define FMT_NOTABLE 0x0010
49#define FMT_NOTARGET 0x0020
50#define FMT_VIA 0x0040
51#define FMT_NONEWLINE 0x0080
52#define FMT_LINENUMBERS 0x0100
53
54#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
55 | FMT_NUMERIC | FMT_NOTABLE)
56#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
57
58
59#define CMD_NONE 0x0000U
60#define CMD_INSERT 0x0001U
61#define CMD_DELETE 0x0002U
62#define CMD_DELETE_NUM 0x0004U
63#define CMD_REPLACE 0x0008U
64#define CMD_APPEND 0x0010U
65#define CMD_LIST 0x0020U
66#define CMD_FLUSH 0x0040U
67#define CMD_ZERO 0x0080U
68#define CMD_NEW_CHAIN 0x0100U
69#define CMD_DELETE_CHAIN 0x0200U
70#define CMD_SET_POLICY 0x0400U
71#define CMD_CHECK 0x0800U
72#define CMD_RENAME_CHAIN 0x1000U
73#define NUMBER_OF_CMD 13
74static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
75 'N', 'X', 'P', 'C', 'E' };
76
77#define OPTION_OFFSET 256
78
79#define OPT_NONE 0x00000U
80#define OPT_NUMERIC 0x00001U
81#define OPT_SOURCE 0x00002U
82#define OPT_DESTINATION 0x00004U
83#define OPT_PROTOCOL 0x00008U
84#define OPT_JUMP 0x00010U
85#define OPT_VERBOSE 0x00020U
86#define OPT_EXPANDED 0x00040U
87#define OPT_VIANAMEIN 0x00080U
88#define OPT_VIANAMEOUT 0x00100U
89#define OPT_FRAGMENT 0x00200U
90#define OPT_LINENUMBERS 0x00400U
91#define NUMBER_OF_OPT 11
92static const char optflags[NUMBER_OF_OPT]
93= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3'};
94
95static struct option original_opts[] = {
96 { "append", 1, 0, 'A' },
97 { "delete", 1, 0, 'D' },
98 { "insert", 1, 0, 'I' },
99 { "replace", 1, 0, 'R' },
100 { "list", 2, 0, 'L' },
101 { "flush", 2, 0, 'F' },
102 { "zero", 2, 0, 'Z' },
103 { "check", 1, 0, 'C' },
104 { "new-chain", 1, 0, 'N' },
105 { "delete-chain", 2, 0, 'X' },
106 { "rename-chain", 2, 0, 'E' },
107 { "policy", 1, 0, 'P' },
108 { "source", 1, 0, 's' },
109 { "destination", 1, 0, 'd' },
110 { "src", 1, 0, 's' }, /* synonym */
111 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000112 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000113 { "in-interface", 1, 0, 'i' },
114 { "jump", 1, 0, 'j' },
115 { "table", 1, 0, 't' },
116 { "match", 1, 0, 'm' },
117 { "numeric", 0, 0, 'n' },
118 { "out-interface", 1, 0, 'o' },
119 { "verbose", 0, 0, 'v' },
120 { "exact", 0, 0, 'x' },
121 { "fragments", 0, 0, 'f' },
122 { "version", 0, 0, 'V' },
123 { "help", 2, 0, 'h' },
124 { "line-numbers", 0, 0, '0' },
125 { 0 }
126};
127
Rusty Russell4e242f82000-05-31 06:33:50 +0000128#ifndef __OPTIMIZE__
Rusty Russell9e1d2142000-04-23 09:11:12 +0000129static struct ipt_entry_target *
130ipt_get_target(struct ipt_entry *e)
131{
132 return (void *)e + e->target_offset;
133}
134#endif
135
Marc Bouchere6869a82000-03-20 06:03:29 +0000136static struct option *opts = original_opts;
137static unsigned int global_option_offset = 0;
138
139/* Table of legal combinations of commands and options. If any of the
140 * given commands make an option legal, that option is legal (applies to
141 * CMD_LIST and CMD_ZERO only).
142 * Key:
143 * + compulsory
144 * x illegal
145 * optional
146 */
147
148static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
149/* Well, it's better than "Re: Linux vs FreeBSD" */
150{
151 /* -n -s -d -p -j -v -x -i -o -f --line */
152/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
153/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
154/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
155/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
156/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
157/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
158/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
159/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
160/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
161/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
162/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000163/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000164/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
165};
166
167static int inverse_for_options[NUMBER_OF_OPT] =
168{
169/* -n */ 0,
170/* -s */ IPT_INV_SRCIP,
171/* -d */ IPT_INV_DSTIP,
172/* -p */ IPT_INV_PROTO,
173/* -j */ 0,
174/* -v */ 0,
175/* -x */ 0,
176/* -i */ IPT_INV_VIA_IN,
177/* -o */ IPT_INV_VIA_OUT,
178/* -f */ IPT_INV_FRAG,
179/*--line*/ 0
180};
181
182const char *program_version;
183const char *program_name;
184
Rusty Russell2e0a3212000-04-19 11:23:18 +0000185/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000186struct iptables_match *iptables_matches = NULL;
187struct iptables_target *iptables_targets = NULL;
188
189/* Extra debugging from libiptc */
190extern void dump_entries(const iptc_handle_t handle);
191
192/* A few hardcoded protocols for 'all' and in case the user has no
193 /etc/protocols */
194struct pprot {
195 char *name;
196 u_int8_t num;
197};
198
199static const struct pprot chain_protos[] = {
200 { "tcp", IPPROTO_TCP },
201 { "udp", IPPROTO_UDP },
202 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000203 { "esp", IPPROTO_ESP },
204 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000205 { "all", 0 },
206};
207
208static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000209proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 unsigned int i;
212
Rusty Russell28381a42000-05-10 00:19:50 +0000213 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000214 struct protoent *pent = getprotobynumber(proto);
215 if (pent)
216 return pent->p_name;
217 }
218
219 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
220 if (chain_protos[i].num == proto)
221 return chain_protos[i].name;
222
223 return NULL;
224}
225
226struct in_addr *
227dotted_to_addr(const char *dotted)
228{
229 static struct in_addr addr;
230 unsigned char *addrp;
231 char *p, *q;
232 int onebyte, i;
233 char buf[20];
234
235 /* copy dotted string, because we need to modify it */
236 strncpy(buf, dotted, sizeof(buf) - 1);
237 addrp = (unsigned char *) &(addr.s_addr);
238
239 p = buf;
240 for (i = 0; i < 3; i++) {
241 if ((q = strchr(p, '.')) == NULL)
242 return (struct in_addr *) NULL;
243
244 *q = '\0';
245 if ((onebyte = string_to_number(p, 0, 255)) == -1)
246 return (struct in_addr *) NULL;
247
248 addrp[i] = (unsigned char) onebyte;
249 p = q + 1;
250 }
251
252 /* we've checked 3 bytes, now we check the last one */
253 if ((onebyte = string_to_number(p, 0, 255)) == -1)
254 return (struct in_addr *) NULL;
255
256 addrp[3] = (unsigned char) onebyte;
257
258 return &addr;
259}
260
261static struct in_addr *
262network_to_addr(const char *name)
263{
264 struct netent *net;
265 static struct in_addr addr;
266
267 if ((net = getnetbyname(name)) != NULL) {
268 if (net->n_addrtype != AF_INET)
269 return (struct in_addr *) NULL;
270 addr.s_addr = htonl((unsigned long) net->n_net);
271 return &addr;
272 }
273
274 return (struct in_addr *) NULL;
275}
276
277static void
278inaddrcpy(struct in_addr *dst, struct in_addr *src)
279{
280 /* memcpy(dst, src, sizeof(struct in_addr)); */
281 dst->s_addr = src->s_addr;
282}
283
284void
285exit_error(enum exittype status, char *msg, ...)
286{
287 va_list args;
288
289 va_start(args, msg);
290 fprintf(stderr, "%s v%s: ", program_name, program_version);
291 vfprintf(stderr, msg, args);
292 va_end(args);
293 fprintf(stderr, "\n");
294 if (status == PARAMETER_PROBLEM)
295 exit_tryhelp(status);
296 if (status == VERSION_PROBLEM)
297 fprintf(stderr,
298 "Perhaps iptables or your kernel needs to be upgraded.\n");
299 exit(status);
300}
301
302void
303exit_tryhelp(int status)
304{
305 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
306 program_name, program_name );
307 exit(status);
308}
309
310void
311exit_printhelp(void)
312{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000313 struct iptables_match *m = NULL;
314 struct iptables_target *t = NULL;
315
Marc Bouchere6869a82000-03-20 06:03:29 +0000316 printf("%s v%s\n\n"
317"Usage: %s -[ADC] chain rule-specification [options]\n"
318" %s -[RI] chain rulenum rule-specification [options]\n"
319" %s -D chain rulenum [options]\n"
320" %s -[LFZ] [chain] [options]\n"
321" %s -[NX] chain\n"
322" %s -E old-chain-name new-chain-name\n"
323" %s -P chain target [options]\n"
324" %s -h (print this help information)\n\n",
325 program_name, program_version, program_name, program_name,
326 program_name, program_name, program_name, program_name,
327 program_name, program_name);
328
329 printf(
330"Commands:\n"
331"Either long or short options are allowed.\n"
332" --append -A chain Append to chain\n"
333" --delete -D chain Delete matching rule from chain\n"
334" --delete -D chain rulenum\n"
335" Delete rule rulenum (1 = first) from chain\n"
336" --insert -I chain [rulenum]\n"
337" Insert in chain as rulenum (default 1=first)\n"
338" --replace -R chain rulenum\n"
339" Replace rule rulenum (1 = first) in chain\n"
340" --list -L [chain] List the rules in a chain or all chains\n"
341" --flush -F [chain] Delete all rules in chain or all chains\n"
342" --zero -Z [chain] Zero counters in chain or all chains\n"
343" --check -C chain Test this packet on chain\n"
344" --new -N chain Create a new user-defined chain\n"
345" --delete-chain\n"
346" -X [chain] Delete a user-defined chain\n"
347" --policy -P chain target\n"
348" Change policy on chain to target\n"
349" --rename-chain\n"
350" -E old-chain new-chain\n"
351" Change chain name, (moving any references)\n"
352
353"Options:\n"
354" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
355" --source -s [!] address[/mask]\n"
356" source specification\n"
357" --destination -d [!] address[/mask]\n"
358" destination specification\n"
359" --in-interface -i [!] input name[+]\n"
360" network interface name ([+] for wildcard)\n"
361" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000362" target for rule (may load target extension)\n"
363" --match -m match\n"
364" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000365" --numeric -n numeric output of addresses and ports\n"
366" --out-interface -o [!] output name[+]\n"
367" network interface name ([+] for wildcard)\n"
368" --table -t table table to manipulate (default: `filter')\n"
369" --verbose -v verbose mode\n"
370" --exact -x expand numbers (display exact values)\n"
371"[!] --fragment -f match second or further fragments only\n"
372"[!] --version -V print package version.\n");
373
Rusty Russell363112d2000-08-11 13:49:26 +0000374 /* Print out any special helps. A user might like to be able
375 to add a --help to the commandline, and see expected
376 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000377 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000378 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000379 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000380 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000381 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000382 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000383 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000384 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000385 exit(0);
386}
387
388static void
389generic_opt_check(int command, int options)
390{
391 int i, j, legal = 0;
392
393 /* Check that commands are valid with options. Complicated by the
394 * fact that if an option is legal with *any* command given, it is
395 * legal overall (ie. -z and -l).
396 */
397 for (i = 0; i < NUMBER_OF_OPT; i++) {
398 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
399
400 for (j = 0; j < NUMBER_OF_CMD; j++) {
401 if (!(command & (1<<j)))
402 continue;
403
404 if (!(options & (1<<i))) {
405 if (commands_v_options[j][i] == '+')
406 exit_error(PARAMETER_PROBLEM,
407 "You need to supply the `-%c' "
408 "option for this command\n",
409 optflags[i]);
410 } else {
411 if (commands_v_options[j][i] != 'x')
412 legal = 1;
413 else if (legal == 0)
414 legal = -1;
415 }
416 }
417 if (legal == -1)
418 exit_error(PARAMETER_PROBLEM,
419 "Illegal option `-%c' with this command\n",
420 optflags[i]);
421 }
422}
423
424static char
425opt2char(int option)
426{
427 const char *ptr;
428 for (ptr = optflags; option > 1; option >>= 1, ptr++);
429
430 return *ptr;
431}
432
433static char
434cmd2char(int option)
435{
436 const char *ptr;
437 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
438
439 return *ptr;
440}
441
442static void
443add_command(int *cmd, const int newcmd, const int othercmds, int invert)
444{
445 if (invert)
446 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
447 if (*cmd & (~othercmds))
448 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
449 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
450 *cmd |= newcmd;
451}
452
453int
454check_inverse(const char option[], int *invert)
455{
456 if (option && strcmp(option, "!") == 0) {
457 if (*invert)
458 exit_error(PARAMETER_PROBLEM,
459 "Multiple `!' flags not allowed");
460
461 *invert = TRUE;
462 return TRUE;
463 }
464 return FALSE;
465}
466
467static void *
468fw_calloc(size_t count, size_t size)
469{
470 void *p;
471
472 if ((p = calloc(count, size)) == NULL) {
473 perror("iptables: calloc failed");
474 exit(1);
475 }
476 return p;
477}
478
479static void *
480fw_malloc(size_t size)
481{
482 void *p;
483
484 if ((p = malloc(size)) == NULL) {
485 perror("iptables: malloc failed");
486 exit(1);
487 }
488 return p;
489}
490
491static struct in_addr *
492host_to_addr(const char *name, unsigned int *naddr)
493{
494 struct hostent *host;
495 struct in_addr *addr;
496 unsigned int i;
497
498 *naddr = 0;
499 if ((host = gethostbyname(name)) != NULL) {
500 if (host->h_addrtype != AF_INET ||
501 host->h_length != sizeof(struct in_addr))
502 return (struct in_addr *) NULL;
503
504 while (host->h_addr_list[*naddr] != (char *) NULL)
505 (*naddr)++;
506 addr = fw_calloc(*naddr, sizeof(struct in_addr));
507 for (i = 0; i < *naddr; i++)
508 inaddrcpy(&(addr[i]),
509 (struct in_addr *) host->h_addr_list[i]);
510 return addr;
511 }
512
513 return (struct in_addr *) NULL;
514}
515
516static char *
517addr_to_host(const struct in_addr *addr)
518{
519 struct hostent *host;
520
521 if ((host = gethostbyaddr((char *) addr,
522 sizeof(struct in_addr), AF_INET)) != NULL)
523 return (char *) host->h_name;
524
525 return (char *) NULL;
526}
527
528/*
529 * All functions starting with "parse" should succeed, otherwise
530 * the program fails.
531 * Most routines return pointers to static data that may change
532 * between calls to the same or other routines with a few exceptions:
533 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
534 * return global static data.
535*/
536
537static struct in_addr *
538parse_hostnetwork(const char *name, unsigned int *naddrs)
539{
540 struct in_addr *addrp, *addrptmp;
541
542 if ((addrptmp = dotted_to_addr(name)) != NULL ||
543 (addrptmp = network_to_addr(name)) != NULL) {
544 addrp = fw_malloc(sizeof(struct in_addr));
545 inaddrcpy(addrp, addrptmp);
546 *naddrs = 1;
547 return addrp;
548 }
549 if ((addrp = host_to_addr(name, naddrs)) != NULL)
550 return addrp;
551
552 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
553}
554
555static struct in_addr *
556parse_mask(char *mask)
557{
558 static struct in_addr maskaddr;
559 struct in_addr *addrp;
560 int bits;
561
562 if (mask == NULL) {
563 /* no mask at all defaults to 32 bits */
564 maskaddr.s_addr = 0xFFFFFFFF;
565 return &maskaddr;
566 }
567 if ((addrp = dotted_to_addr(mask)) != NULL)
568 /* dotted_to_addr already returns a network byte order addr */
569 return addrp;
570 if ((bits = string_to_number(mask, 0, 32)) == -1)
571 exit_error(PARAMETER_PROBLEM,
572 "invalid mask `%s' specified", mask);
573 if (bits != 0) {
574 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
575 return &maskaddr;
576 }
577
578 maskaddr.s_addr = 0L;
579 return &maskaddr;
580}
581
582static void
583parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
584 struct in_addr *maskp, unsigned int *naddrs)
585{
586 struct in_addr *addrp;
587 char buf[256];
588 char *p;
589 int i, j, k, n;
590
591 strncpy(buf, name, sizeof(buf) - 1);
592 if ((p = strrchr(buf, '/')) != NULL) {
593 *p = '\0';
594 addrp = parse_mask(p + 1);
595 } else
596 addrp = parse_mask(NULL);
597 inaddrcpy(maskp, addrp);
598
599 /* if a null mask is given, the name is ignored, like in "any/0" */
600 if (maskp->s_addr == 0L)
601 strcpy(buf, "0.0.0.0");
602
603 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
604 n = *naddrs;
605 for (i = 0, j = 0; i < n; i++) {
606 addrp[j++].s_addr &= maskp->s_addr;
607 for (k = 0; k < j - 1; k++) {
608 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
609 (*naddrs)--;
610 j--;
611 break;
612 }
613 }
614 }
615}
616
617struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000618find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000619{
620 struct iptables_match *ptr;
621
622 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
623 if (strcmp(name, ptr->name) == 0)
624 break;
625 }
626
Rusty Russell52a51492000-05-02 16:44:29 +0000627 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000628 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
629 + strlen(name)];
630 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000631 if (dlopen(path, RTLD_NOW)) {
632 /* Found library. If it didn't register itself,
633 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000634 ptr = find_match(name, DONT_LOAD);
635
Rusty Russell9e1d2142000-04-23 09:11:12 +0000636 if (!ptr)
637 exit_error(PARAMETER_PROBLEM,
638 "Couldn't load match `%s'\n",
639 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000640 } else if (tryload == LOAD_MUST_SUCCEED)
641 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000642 "Couldn't load match `%s':%s\n",
643 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000644 }
645
646 return ptr;
647}
648
Rusty Russell28381a42000-05-10 00:19:50 +0000649/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
650static struct iptables_match *
651find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
652{
653 int proto;
654
655 proto = string_to_number(pname, 0, 255);
656 if (proto != -1)
657 return find_match(proto_to_name(proto, nolookup), tryload);
658
659 return find_match(pname, tryload);
660}
661
Marc Bouchere6869a82000-03-20 06:03:29 +0000662static u_int16_t
663parse_protocol(const char *s)
664{
Rusty Russell28381a42000-05-10 00:19:50 +0000665 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000666
667 if (proto == -1) {
668 struct protoent *pent;
669
670 if ((pent = getprotobyname(s)))
671 proto = pent->p_proto;
672 else {
673 unsigned int i;
674 for (i = 0;
675 i < sizeof(chain_protos)/sizeof(struct pprot);
676 i++) {
677 if (strcmp(s, chain_protos[i].name) == 0) {
678 proto = chain_protos[i].num;
679 break;
680 }
681 }
682 if (i == sizeof(chain_protos)/sizeof(struct pprot))
683 exit_error(PARAMETER_PROBLEM,
684 "unknown protocol `%s' specified",
685 s);
686 }
687 }
688
689 return (u_int16_t)proto;
690}
691
692static void
693parse_interface(const char *arg, char *vianame, unsigned char *mask)
694{
695 int vialen = strlen(arg);
696 unsigned int i;
697
698 memset(mask, 0, IFNAMSIZ);
699 memset(vianame, 0, IFNAMSIZ);
700
701 if (vialen + 1 > IFNAMSIZ)
702 exit_error(PARAMETER_PROBLEM,
703 "interface name `%s' must be shorter than IFNAMSIZ"
704 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000705
Marc Bouchere6869a82000-03-20 06:03:29 +0000706 strcpy(vianame, arg);
707 if (vialen == 0)
708 memset(mask, 0, IFNAMSIZ);
709 else if (vianame[vialen - 1] == '+') {
710 memset(mask, 0xFF, vialen - 1);
711 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
712 /* Remove `+' */
713 vianame[vialen - 1] = '\0';
714 } else {
715 /* Include nul-terminator in match */
716 memset(mask, 0xFF, vialen + 1);
717 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
718 }
719 for (i = 0; vianame[i]; i++) {
720 if (!isalnum(vianame[i])) {
721 printf("Warning: wierd character in interface"
722 " `%s' (No aliases, :, ! or *).\n",
723 vianame);
724 break;
725 }
726 }
727}
728
729/* Can't be zero. */
730static int
731parse_rulenumber(const char *rule)
732{
733 int rulenum = string_to_number(rule, 1, INT_MAX);
734
735 if (rulenum == -1)
736 exit_error(PARAMETER_PROBLEM,
737 "Invalid rule number `%s'", rule);
738
739 return rulenum;
740}
741
742static const char *
743parse_target(const char *targetname)
744{
745 const char *ptr;
746
747 if (strlen(targetname) < 1)
748 exit_error(PARAMETER_PROBLEM,
749 "Invalid target name (too short)");
750
751 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
752 exit_error(PARAMETER_PROBLEM,
753 "Invalid target name `%s' (%i chars max)",
754 targetname, sizeof(ipt_chainlabel)-1);
755
756 for (ptr = targetname; *ptr; ptr++)
757 if (isspace(*ptr))
758 exit_error(PARAMETER_PROBLEM,
759 "Invalid target name `%s'", targetname);
760 return targetname;
761}
762
763static char *
764addr_to_network(const struct in_addr *addr)
765{
766 struct netent *net;
767
768 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
769 return (char *) net->n_name;
770
771 return (char *) NULL;
772}
773
774char *
775addr_to_dotted(const struct in_addr *addrp)
776{
777 static char buf[20];
778 const unsigned char *bytep;
779
780 bytep = (const unsigned char *) &(addrp->s_addr);
781 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
782 return buf;
783}
784static char *
785addr_to_anyname(const struct in_addr *addr)
786{
787 char *name;
788
789 if ((name = addr_to_host(addr)) != NULL ||
790 (name = addr_to_network(addr)) != NULL)
791 return name;
792
793 return addr_to_dotted(addr);
794}
795
796static char *
797mask_to_dotted(const struct in_addr *mask)
798{
799 int i;
800 static char buf[20];
801 u_int32_t maskaddr, bits;
802
803 maskaddr = ntohl(mask->s_addr);
804
805 if (maskaddr == 0xFFFFFFFFL)
806 /* we don't want to see "/32" */
807 return "";
808
809 i = 32;
810 bits = 0xFFFFFFFEL;
811 while (--i >= 0 && maskaddr != bits)
812 bits <<= 1;
813 if (i >= 0)
814 sprintf(buf, "/%d", i);
815 else
816 /* mask was not a decent combination of 1's and 0's */
817 sprintf(buf, "/%s", addr_to_dotted(mask));
818
819 return buf;
820}
821
822int
823string_to_number(const char *s, int min, int max)
824{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000825 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000826 char *end;
827
828 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000829 errno = 0;
830 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000831 if (*end == '\0' && end != s) {
832 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000833 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000834 return number;
835 }
836 return -1;
837}
838
839static void
840set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
841 int invert)
842{
843 if (*options & option)
844 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
845 opt2char(option));
846 *options |= option;
847
848 if (invert) {
849 unsigned int i;
850 for (i = 0; 1 << i != option; i++);
851
852 if (!inverse_for_options[i])
853 exit_error(PARAMETER_PROBLEM,
854 "cannot have ! before -%c",
855 opt2char(option));
856 *invflg |= inverse_for_options[i];
857 }
858}
859
860struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000861find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000862{
863 struct iptables_target *ptr;
864
865 /* Standard target? */
866 if (strcmp(name, "") == 0
867 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
868 || strcmp(name, IPTC_LABEL_DROP) == 0
869 || strcmp(name, IPTC_LABEL_QUEUE) == 0
870 || strcmp(name, IPTC_LABEL_RETURN) == 0)
871 name = "standard";
872
873 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
874 if (strcmp(name, ptr->name) == 0)
875 break;
876 }
877
Rusty Russell52a51492000-05-02 16:44:29 +0000878 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000879 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
880 + strlen(name)];
881 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000882 if (dlopen(path, RTLD_NOW)) {
883 /* Found library. If it didn't register itself,
884 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000885 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000886 if (!ptr)
887 exit_error(PARAMETER_PROBLEM,
888 "Couldn't load target `%s'\n",
889 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000890 } else if (tryload == LOAD_MUST_SUCCEED)
891 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000892 "Couldn't load target `%s':%s\n",
893 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 }
895
896 return ptr;
897}
898
899static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000900merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000901 unsigned int *option_offset)
902{
903 unsigned int num_old, num_new, i;
904 struct option *merge;
905
906 for (num_old = 0; oldopts[num_old].name; num_old++);
907 for (num_new = 0; newopts[num_new].name; num_new++);
908
909 global_option_offset += OPTION_OFFSET;
910 *option_offset = global_option_offset;
911
912 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
913 memcpy(merge, oldopts, num_old * sizeof(struct option));
914 for (i = 0; i < num_new; i++) {
915 merge[num_old + i] = newopts[i];
916 merge[num_old + i].val += *option_offset;
917 }
918 memset(merge + num_old + num_new, 0, sizeof(struct option));
919
920 return merge;
921}
922
923void
924register_match(struct iptables_match *me)
925{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000926 struct iptables_match **i;
927
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 if (strcmp(me->version, program_version) != 0) {
929 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
930 program_name, me->name, me->version, program_version);
931 exit(1);
932 }
933
Rusty Russell52a51492000-05-02 16:44:29 +0000934 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000935 fprintf(stderr, "%s: match `%s' already registered.\n",
936 program_name, me->name);
937 exit(1);
938 }
939
Rusty Russell73f72f52000-07-03 10:17:57 +0000940 if (me->size != IPT_ALIGN(me->size)) {
941 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
942 program_name, me->name, me->size);
943 exit(1);
944 }
945
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000946 /* Append to list. */
947 for (i = &iptables_matches; *i; i = &(*i)->next);
948 me->next = NULL;
949 *i = me;
950
Marc Bouchere6869a82000-03-20 06:03:29 +0000951 me->m = NULL;
952 me->mflags = 0;
953
954 opts = merge_options(opts, me->extra_opts, &me->option_offset);
955}
956
957void
958register_target(struct iptables_target *me)
959{
960 if (strcmp(me->version, program_version) != 0) {
961 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
962 program_name, me->name, me->version, program_version);
963 exit(1);
964 }
965
Rusty Russell52a51492000-05-02 16:44:29 +0000966 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000967 fprintf(stderr, "%s: target `%s' already registered.\n",
968 program_name, me->name);
969 exit(1);
970 }
971
Rusty Russell73f72f52000-07-03 10:17:57 +0000972 if (me->size != IPT_ALIGN(me->size)) {
973 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
974 program_name, me->name, me->size);
975 exit(1);
976 }
977
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 /* Prepend to list. */
979 me->next = iptables_targets;
980 iptables_targets = me;
981 me->t = NULL;
982 me->tflags = 0;
983
984 opts = merge_options(opts, me->extra_opts, &me->option_offset);
985}
986
987static void
988print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
989{
990 struct ipt_counters counters;
991 const char *pol = iptc_get_policy(chain, &counters, handle);
992 printf("Chain %s", chain);
993 if (pol) {
994 printf(" (policy %s", pol);
995 if (!(format & FMT_NOCOUNTS))
996 printf(" %llu packets, %llu bytes",
997 counters.pcnt, counters.bcnt);
998 printf(")\n");
999 } else {
1000 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001001 if (!iptc_get_references(&refs, chain, handle))
1002 printf(" (ERROR obtaining refs)\n");
1003 else
1004 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 }
1006
1007 if (format & FMT_LINENUMBERS)
1008 printf(FMT("%-4s ", "%s "), "num");
1009 if (!(format & FMT_NOCOUNTS)) {
1010 if (format & FMT_KILOMEGAGIGA) {
1011 printf(FMT("%5s ","%s "), "pkts");
1012 printf(FMT("%5s ","%s "), "bytes");
1013 } else {
1014 printf(FMT("%8s ","%s "), "pkts");
1015 printf(FMT("%10s ","%s "), "bytes");
1016 }
1017 }
1018 if (!(format & FMT_NOTARGET))
1019 printf(FMT("%-9s ","%s "), "target");
1020 fputs(" prot ", stdout);
1021 if (format & FMT_OPTIONS)
1022 fputs("opt", stdout);
1023 if (format & FMT_VIA) {
1024 printf(FMT(" %-6s ","%s "), "in");
1025 printf(FMT("%-6s ","%s "), "out");
1026 }
1027 printf(FMT(" %-19s ","%s "), "source");
1028 printf(FMT(" %-19s "," %s "), "destination");
1029 printf("\n");
1030}
1031
1032static void
1033print_num(u_int64_t number, unsigned int format)
1034{
1035 if (format & FMT_KILOMEGAGIGA) {
1036 if (number > 99999) {
1037 number = (number + 500) / 1000;
1038 if (number > 9999) {
1039 number = (number + 500) / 1000;
1040 if (number > 9999) {
1041 number = (number + 500) / 1000;
1042 printf(FMT("%4lluG ","%lluG "),number);
1043 }
1044 else printf(FMT("%4lluM ","%lluM "), number);
1045 } else
1046 printf(FMT("%4lluK ","%lluK "), number);
1047 } else
1048 printf(FMT("%5llu ","%llu "), number);
1049 } else
1050 printf(FMT("%8llu ","%llu "), number);
1051}
1052
1053static int
1054print_match(const struct ipt_entry_match *m,
1055 const struct ipt_ip *ip,
1056 int numeric)
1057{
Rusty Russell52a51492000-05-02 16:44:29 +00001058 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001059
1060 if (match) {
1061 if (match->print)
1062 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001063 else
Rusty Russellb039b022000-09-01 06:04:05 +00001064 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001065 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001066 if (m->u.user.name[0])
1067 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001068 }
1069 /* Don't stop iterating. */
1070 return 0;
1071}
1072
1073/* e is called `fw' here for hysterical raisins */
1074static void
1075print_firewall(const struct ipt_entry *fw,
1076 const char *targname,
1077 unsigned int num,
1078 unsigned int format,
1079 const iptc_handle_t handle)
1080{
1081 struct iptables_target *target = NULL;
1082 const struct ipt_entry_target *t;
1083 u_int8_t flags;
1084 char buf[BUFSIZ];
1085
1086 /* User creates a chain called "REJECT": this overrides the
1087 `REJECT' target module. Keep feeding them rope until the
1088 revolution... Bwahahahahah */
1089 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001090 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001091 else
Rusty Russell52a51492000-05-02 16:44:29 +00001092 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001093
1094 t = ipt_get_target((struct ipt_entry *)fw);
1095 flags = fw->ip.flags;
1096
1097 if (format & FMT_LINENUMBERS)
1098 printf(FMT("%-4u ", "%u "), num+1);
1099
1100 if (!(format & FMT_NOCOUNTS)) {
1101 print_num(fw->counters.pcnt, format);
1102 print_num(fw->counters.bcnt, format);
1103 }
1104
1105 if (!(format & FMT_NOTARGET))
1106 printf(FMT("%-9s ", "%s "), targname);
1107
1108 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1109 {
Rusty Russell28381a42000-05-10 00:19:50 +00001110 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001111 if (pname)
1112 printf(FMT("%-5s", "%s "), pname);
1113 else
1114 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1115 }
1116
1117 if (format & FMT_OPTIONS) {
1118 if (format & FMT_NOTABLE)
1119 fputs("opt ", stdout);
1120 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1121 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1122 fputc(' ', stdout);
1123 }
1124
1125 if (format & FMT_VIA) {
1126 char iface[IFNAMSIZ+2];
1127
1128 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1129 iface[0] = '!';
1130 iface[1] = '\0';
1131 }
1132 else iface[0] = '\0';
1133
1134 if (fw->ip.iniface[0] != '\0') {
1135 strcat(iface, fw->ip.iniface);
1136 /* If it doesn't compare the nul-term, it's a
1137 wildcard. */
1138 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1139 strcat(iface, "+");
1140 }
1141 else if (format & FMT_NUMERIC) strcat(iface, "*");
1142 else strcat(iface, "any");
1143 printf(FMT(" %-6s ","in %s "), iface);
1144
1145 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1146 iface[0] = '!';
1147 iface[1] = '\0';
1148 }
1149 else iface[0] = '\0';
1150
1151 if (fw->ip.outiface[0] != '\0') {
1152 strcat(iface, fw->ip.outiface);
1153 /* If it doesn't compare the nul-term, it's a
1154 wildcard. */
1155 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1156 strcat(iface, "+");
1157 }
1158 else if (format & FMT_NUMERIC) strcat(iface, "*");
1159 else strcat(iface, "any");
1160 printf(FMT("%-6s ","out %s "), iface);
1161 }
1162
1163 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1164 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1165 printf(FMT("%-19s ","%s "), "anywhere");
1166 else {
1167 if (format & FMT_NUMERIC)
1168 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1169 else
1170 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1171 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1172 printf(FMT("%-19s ","%s "), buf);
1173 }
1174
1175 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1176 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1177 printf(FMT("%-19s","-> %s"), "anywhere");
1178 else {
1179 if (format & FMT_NUMERIC)
1180 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1181 else
1182 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1183 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1184 printf(FMT("%-19s","-> %s"), buf);
1185 }
1186
1187 if (format & FMT_NOTABLE)
1188 fputs(" ", stdout);
1189
1190 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1191
1192 if (target) {
1193 if (target->print)
1194 /* Print the target information. */
1195 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001196 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001197 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001198 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001199
1200 if (!(format & FMT_NONEWLINE))
1201 fputc('\n', stdout);
1202}
1203
1204static void
1205print_firewall_line(const struct ipt_entry *fw,
1206 const iptc_handle_t h)
1207{
1208 struct ipt_entry_target *t;
1209
1210 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001211 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001212}
1213
1214static int
1215append_entry(const ipt_chainlabel chain,
1216 struct ipt_entry *fw,
1217 unsigned int nsaddrs,
1218 const struct in_addr saddrs[],
1219 unsigned int ndaddrs,
1220 const struct in_addr daddrs[],
1221 int verbose,
1222 iptc_handle_t *handle)
1223{
1224 unsigned int i, j;
1225 int ret = 1;
1226
1227 for (i = 0; i < nsaddrs; i++) {
1228 fw->ip.src.s_addr = saddrs[i].s_addr;
1229 for (j = 0; j < ndaddrs; j++) {
1230 fw->ip.dst.s_addr = daddrs[j].s_addr;
1231 if (verbose)
1232 print_firewall_line(fw, *handle);
1233 ret &= iptc_append_entry(chain, fw, handle);
1234 }
1235 }
1236
1237 return ret;
1238}
1239
1240static int
1241replace_entry(const ipt_chainlabel chain,
1242 struct ipt_entry *fw,
1243 unsigned int rulenum,
1244 const struct in_addr *saddr,
1245 const struct in_addr *daddr,
1246 int verbose,
1247 iptc_handle_t *handle)
1248{
1249 fw->ip.src.s_addr = saddr->s_addr;
1250 fw->ip.dst.s_addr = daddr->s_addr;
1251
1252 if (verbose)
1253 print_firewall_line(fw, *handle);
1254 return iptc_replace_entry(chain, fw, rulenum, handle);
1255}
1256
1257static int
1258insert_entry(const ipt_chainlabel chain,
1259 struct ipt_entry *fw,
1260 unsigned int rulenum,
1261 unsigned int nsaddrs,
1262 const struct in_addr saddrs[],
1263 unsigned int ndaddrs,
1264 const struct in_addr daddrs[],
1265 int verbose,
1266 iptc_handle_t *handle)
1267{
1268 unsigned int i, j;
1269 int ret = 1;
1270
1271 for (i = 0; i < nsaddrs; i++) {
1272 fw->ip.src.s_addr = saddrs[i].s_addr;
1273 for (j = 0; j < ndaddrs; j++) {
1274 fw->ip.dst.s_addr = daddrs[j].s_addr;
1275 if (verbose)
1276 print_firewall_line(fw, *handle);
1277 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1278 }
1279 }
1280
1281 return ret;
1282}
1283
Rusty Russell2e0a3212000-04-19 11:23:18 +00001284static unsigned char *
1285make_delete_mask(struct ipt_entry *fw)
1286{
1287 /* Establish mask for comparison */
1288 unsigned int size;
1289 struct iptables_match *m;
1290 unsigned char *mask, *mptr;
1291
1292 size = sizeof(struct ipt_entry);
1293 for (m = iptables_matches; m; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001294 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001295
Rusty Russell9e1d2142000-04-23 09:11:12 +00001296 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001297 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001298 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001299
Rusty Russell9e1d2142000-04-23 09:11:12 +00001300 memset(mask, 0xFF, sizeof(struct ipt_entry));
1301 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001302
1303 for (m = iptables_matches; m; m = m->next) {
1304 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001305 IPT_ALIGN(sizeof(struct ipt_entry_match))
1306 + m->userspacesize);
1307 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001308 }
1309
Rusty Russell73f72f52000-07-03 10:17:57 +00001310 memset(mptr, 0xFF,
1311 IPT_ALIGN(sizeof(struct ipt_entry_target))
1312 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001313
1314 return mask;
1315}
1316
Marc Bouchere6869a82000-03-20 06:03:29 +00001317static int
1318delete_entry(const ipt_chainlabel chain,
1319 struct ipt_entry *fw,
1320 unsigned int nsaddrs,
1321 const struct in_addr saddrs[],
1322 unsigned int ndaddrs,
1323 const struct in_addr daddrs[],
1324 int verbose,
1325 iptc_handle_t *handle)
1326{
1327 unsigned int i, j;
1328 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001329 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001330
Rusty Russell2e0a3212000-04-19 11:23:18 +00001331 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 for (i = 0; i < nsaddrs; i++) {
1333 fw->ip.src.s_addr = saddrs[i].s_addr;
1334 for (j = 0; j < ndaddrs; j++) {
1335 fw->ip.dst.s_addr = daddrs[j].s_addr;
1336 if (verbose)
1337 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001338 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 }
1340 }
1341 return ret;
1342}
1343
1344static int
1345check_packet(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 int ret = 1;
1355 unsigned int i, j;
1356 const char *msg;
1357
1358 for (i = 0; i < nsaddrs; i++) {
1359 fw->ip.src.s_addr = saddrs[i].s_addr;
1360 for (j = 0; j < ndaddrs; j++) {
1361 fw->ip.dst.s_addr = daddrs[j].s_addr;
1362 if (verbose)
1363 print_firewall_line(fw, *handle);
1364 msg = iptc_check_packet(chain, fw, handle);
1365 if (!msg) ret = 0;
1366 else printf("%s\n", msg);
1367 }
1368 }
1369
1370 return ret;
1371}
1372
1373static int
1374for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001375 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001376{
1377 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001378 const char *chain;
1379 char *chains;
1380 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001381
Rusty Russell9e1d2142000-04-23 09:11:12 +00001382 chain = iptc_first_chain(handle);
1383 while (chain) {
1384 chaincount++;
1385 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001386 }
1387
Rusty Russell9e1d2142000-04-23 09:11:12 +00001388 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1389 i = 0;
1390 chain = iptc_first_chain(handle);
1391 while (chain) {
1392 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1393 i++;
1394 chain = iptc_next_chain(handle);
1395 }
1396
1397 for (i = 0; i < chaincount; i++) {
1398 if (!builtinstoo
1399 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1400 *handle))
1401 continue;
1402 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1403 }
1404
1405 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001406 return ret;
1407}
1408
1409static int
1410flush_entries(const ipt_chainlabel chain, int verbose,
1411 iptc_handle_t *handle)
1412{
1413 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001414 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001415
1416 if (verbose)
1417 fprintf(stdout, "Flushing chain `%s'\n", chain);
1418 return iptc_flush_entries(chain, handle);
1419}
Marc Bouchere6869a82000-03-20 06:03:29 +00001420
1421static int
1422zero_entries(const ipt_chainlabel chain, int verbose,
1423 iptc_handle_t *handle)
1424{
1425 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001426 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001427
Marc Bouchere6869a82000-03-20 06:03:29 +00001428 if (verbose)
1429 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1430 return iptc_zero_entries(chain, handle);
1431}
1432
1433static int
1434delete_chain(const ipt_chainlabel chain, int verbose,
1435 iptc_handle_t *handle)
1436{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001437 if (!chain)
1438 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001439
1440 if (verbose)
1441 fprintf(stdout, "Deleting chain `%s'\n", chain);
1442 return iptc_delete_chain(chain, handle);
1443}
1444
1445static int
1446list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1447 int expanded, int linenumbers, iptc_handle_t *handle)
1448{
1449 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001450 unsigned int format;
1451 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001452
1453 format = FMT_OPTIONS;
1454 if (!verbose)
1455 format |= FMT_NOCOUNTS;
1456 else
1457 format |= FMT_VIA;
1458
1459 if (numeric)
1460 format |= FMT_NUMERIC;
1461
1462 if (!expanded)
1463 format |= FMT_KILOMEGAGIGA;
1464
1465 if (linenumbers)
1466 format |= FMT_LINENUMBERS;
1467
Rusty Russell9e1d2142000-04-23 09:11:12 +00001468 for (this = iptc_first_chain(handle);
1469 this;
1470 this = iptc_next_chain(handle)) {
1471 const struct ipt_entry *i;
1472 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001473
Marc Bouchere6869a82000-03-20 06:03:29 +00001474 if (chain && strcmp(chain, this) != 0)
1475 continue;
1476
1477 if (found) printf("\n");
1478
1479 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001480 i = iptc_first_rule(this, handle);
1481
1482 num = 0;
1483 while (i) {
1484 print_firewall(i,
1485 iptc_get_target(i, handle),
1486 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001487 format,
1488 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001489 i = iptc_next_rule(i, handle);
1490 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001491 found = 1;
1492 }
1493
1494 errno = ENOENT;
1495 return found;
1496}
1497
1498static struct ipt_entry *
1499generate_entry(const struct ipt_entry *fw,
1500 struct iptables_match *matches,
1501 struct ipt_entry_target *target)
1502{
1503 unsigned int size;
1504 struct iptables_match *m;
1505 struct ipt_entry *e;
1506
1507 size = sizeof(struct ipt_entry);
1508 for (m = matches; m; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001509 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001510
Rusty Russell228e98d2000-04-27 10:28:06 +00001511 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001512 *e = *fw;
1513 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001514 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001515
1516 size = 0;
1517 for (m = matches; m; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001518 memcpy(e->elems + size, m->m, m->m->u.match_size);
1519 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001520 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001521 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001522
1523 return e;
1524}
1525
1526int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1527{
1528 struct ipt_entry fw, *e = NULL;
1529 int invert = 0;
1530 unsigned int nsaddrs = 0, ndaddrs = 0;
1531 struct in_addr *saddrs = NULL, *daddrs = NULL;
1532
1533 int c, verbose = 0;
1534 const char *chain = NULL;
1535 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1536 const char *policy = NULL, *newname = NULL;
1537 unsigned int rulenum = 0, options = 0, command = 0;
1538 int ret = 1;
1539 struct iptables_match *m;
1540 struct iptables_target *target = NULL;
1541 const char *jumpto = "";
1542 char *protocol = NULL;
1543
1544 memset(&fw, 0, sizeof(fw));
1545
1546 /* Suppress error messages: we may add new options if we
1547 demand-load a protocol. */
1548 opterr = 0;
1549
1550 while ((c = getopt_long(argc, argv,
1551 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1552 opts, NULL)) != -1) {
1553 switch (c) {
1554 /*
1555 * Command selection
1556 */
1557 case 'A':
1558 add_command(&command, CMD_APPEND, CMD_NONE,
1559 invert);
1560 chain = optarg;
1561 break;
1562
1563 case 'D':
1564 add_command(&command, CMD_DELETE, CMD_NONE,
1565 invert);
1566 chain = optarg;
1567 if (optind < argc && argv[optind][0] != '-'
1568 && argv[optind][0] != '!') {
1569 rulenum = parse_rulenumber(argv[optind++]);
1570 command = CMD_DELETE_NUM;
1571 }
1572 break;
1573
1574 case 'C':
1575 add_command(&command, CMD_CHECK, CMD_NONE,
1576 invert);
1577 chain = optarg;
1578 break;
1579
1580 case 'R':
1581 add_command(&command, CMD_REPLACE, CMD_NONE,
1582 invert);
1583 chain = optarg;
1584 if (optind < argc && argv[optind][0] != '-'
1585 && argv[optind][0] != '!')
1586 rulenum = parse_rulenumber(argv[optind++]);
1587 else
1588 exit_error(PARAMETER_PROBLEM,
1589 "-%c requires a rule number",
1590 cmd2char(CMD_REPLACE));
1591 break;
1592
1593 case 'I':
1594 add_command(&command, CMD_INSERT, CMD_NONE,
1595 invert);
1596 chain = optarg;
1597 if (optind < argc && argv[optind][0] != '-'
1598 && argv[optind][0] != '!')
1599 rulenum = parse_rulenumber(argv[optind++]);
1600 else rulenum = 1;
1601 break;
1602
1603 case 'L':
1604 add_command(&command, CMD_LIST, CMD_ZERO,
1605 invert);
1606 if (optarg) chain = optarg;
1607 else if (optind < argc && argv[optind][0] != '-'
1608 && argv[optind][0] != '!')
1609 chain = argv[optind++];
1610 break;
1611
1612 case 'F':
1613 add_command(&command, CMD_FLUSH, CMD_NONE,
1614 invert);
1615 if (optarg) chain = optarg;
1616 else if (optind < argc && argv[optind][0] != '-'
1617 && argv[optind][0] != '!')
1618 chain = argv[optind++];
1619 break;
1620
1621 case 'Z':
1622 add_command(&command, CMD_ZERO, CMD_LIST,
1623 invert);
1624 if (optarg) chain = optarg;
1625 else if (optind < argc && argv[optind][0] != '-'
1626 && argv[optind][0] != '!')
1627 chain = argv[optind++];
1628 break;
1629
1630 case 'N':
1631 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1632 invert);
1633 chain = optarg;
1634 break;
1635
1636 case 'X':
1637 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1638 invert);
1639 if (optarg) chain = optarg;
1640 else if (optind < argc && argv[optind][0] != '-'
1641 && argv[optind][0] != '!')
1642 chain = argv[optind++];
1643 break;
1644
1645 case 'E':
1646 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1647 invert);
1648 chain = optarg;
1649 if (optind < argc && argv[optind][0] != '-'
1650 && argv[optind][0] != '!')
1651 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001652 else
1653 exit_error(PARAMETER_PROBLEM,
1654 "-%c requires old-chain-name and "
1655 "new-chain-name",
1656 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001657 break;
1658
1659 case 'P':
1660 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1661 invert);
1662 chain = optarg;
1663 if (optind < argc && argv[optind][0] != '-'
1664 && argv[optind][0] != '!')
1665 policy = argv[optind++];
1666 else
1667 exit_error(PARAMETER_PROBLEM,
1668 "-%c requires a chain and a policy",
1669 cmd2char(CMD_SET_POLICY));
1670 break;
1671
1672 case 'h':
1673 if (!optarg)
1674 optarg = argv[optind];
1675
Rusty Russell2e0a3212000-04-19 11:23:18 +00001676 /* iptables -p icmp -h */
1677 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001678 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001679
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 exit_printhelp();
1681
1682 /*
1683 * Option selection
1684 */
1685 case 'p':
1686 if (check_inverse(optarg, &invert))
1687 optind++;
1688 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1689 invert);
1690
1691 /* Canonicalize into lower case */
1692 for (protocol = argv[optind-1]; *protocol; protocol++)
1693 *protocol = tolower(*protocol);
1694
1695 protocol = argv[optind-1];
1696 fw.ip.proto = parse_protocol(protocol);
1697
1698 if (fw.ip.proto == 0
1699 && (fw.ip.invflags & IPT_INV_PROTO))
1700 exit_error(PARAMETER_PROBLEM,
1701 "rule would never match protocol");
1702 fw.nfcache |= NFC_IP_PROTO;
1703 break;
1704
1705 case 's':
1706 if (check_inverse(optarg, &invert))
1707 optind++;
1708 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1709 invert);
1710 shostnetworkmask = argv[optind-1];
1711 fw.nfcache |= NFC_IP_SRC;
1712 break;
1713
1714 case 'd':
1715 if (check_inverse(optarg, &invert))
1716 optind++;
1717 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1718 invert);
1719 dhostnetworkmask = argv[optind-1];
1720 fw.nfcache |= NFC_IP_DST;
1721 break;
1722
1723 case 'j':
1724 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1725 invert);
1726 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001727 /* TRY_LOAD (may be chain name) */
1728 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001729
1730 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001731 size_t size;
1732
Rusty Russell73f72f52000-07-03 10:17:57 +00001733 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1734 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001735
Rusty Russell2e0a3212000-04-19 11:23:18 +00001736 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001737 target->t->u.target_size = size;
1738 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001739 target->init(target->t, &fw.nfcache);
1740 }
1741 break;
1742
1743
1744 case 'i':
1745 if (check_inverse(optarg, &invert))
1746 optind++;
1747 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1748 invert);
1749 parse_interface(argv[optind-1],
1750 fw.ip.iniface,
1751 fw.ip.iniface_mask);
1752 fw.nfcache |= NFC_IP_IF_IN;
1753 break;
1754
1755 case 'o':
1756 if (check_inverse(optarg, &invert))
1757 optind++;
1758 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1759 invert);
1760 parse_interface(argv[optind-1],
1761 fw.ip.outiface,
1762 fw.ip.outiface_mask);
1763 fw.nfcache |= NFC_IP_IF_OUT;
1764 break;
1765
1766 case 'f':
1767 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1768 invert);
1769 fw.ip.flags |= IPT_F_FRAG;
1770 fw.nfcache |= NFC_IP_FRAG;
1771 break;
1772
1773 case 'v':
1774 if (!verbose)
1775 set_option(&options, OPT_VERBOSE,
1776 &fw.ip.invflags, invert);
1777 verbose++;
1778 break;
1779
Rusty Russell52a51492000-05-02 16:44:29 +00001780 case 'm': {
1781 size_t size;
1782
Marc Bouchere6869a82000-03-20 06:03:29 +00001783 if (invert)
1784 exit_error(PARAMETER_PROBLEM,
1785 "unexpected ! flag before --match");
1786
Rusty Russell52a51492000-05-02 16:44:29 +00001787 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001788 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1789 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001790 m->m = fw_calloc(1, size);
1791 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001792 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001793 m->init(m->m, &fw.nfcache);
1794 }
1795 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001796
1797 case 'n':
1798 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1799 invert);
1800 break;
1801
1802 case 't':
1803 if (invert)
1804 exit_error(PARAMETER_PROBLEM,
1805 "unexpected ! flag before --table");
1806 *table = argv[optind-1];
1807 break;
1808
1809 case 'x':
1810 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1811 invert);
1812 break;
1813
1814 case 'V':
1815 if (invert)
1816 printf("Not %s ;-)\n", program_version);
1817 else
1818 printf("%s v%s\n",
1819 program_name, program_version);
1820 exit(0);
1821
1822 case '0':
1823 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1824 invert);
1825 break;
1826
1827 case 1: /* non option */
1828 if (optarg[0] == '!' && optarg[1] == '\0') {
1829 if (invert)
1830 exit_error(PARAMETER_PROBLEM,
1831 "multiple consecutive ! not"
1832 " allowed");
1833 invert = TRUE;
1834 optarg[0] = '\0';
1835 continue;
1836 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001837 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001838 exit_tryhelp(2);
1839
1840 default:
1841 /* FIXME: This scheme doesn't allow two of the same
1842 matches --RR */
1843 if (!target
1844 || !(target->parse(c - target->option_offset,
1845 argv, invert,
1846 &target->tflags,
1847 &fw, &target->t))) {
1848 for (m = iptables_matches; m; m = m->next) {
1849 if (m->parse(c - m->option_offset,
1850 argv, invert,
1851 &m->mflags,
1852 &fw,
1853 &fw.nfcache,
1854 &m->m))
1855 break;
1856 }
1857
1858 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001859 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001860 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001861 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001862 && !find_proto(protocol, DONT_LOAD,
1863 options&OPT_NUMERIC)
1864 && (m = find_proto(protocol, TRY_LOAD,
1865 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001866 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001867 size_t size;
1868
Rusty Russell73f72f52000-07-03 10:17:57 +00001869 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1870 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001871
Rusty Russell2e0a3212000-04-19 11:23:18 +00001872 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001873 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001874 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001875 m->init(m->m, &fw.nfcache);
1876
1877 optind--;
1878 continue;
1879 }
1880 if (!m)
1881 exit_error(PARAMETER_PROBLEM,
1882 "Unknown arg `%s'",
1883 argv[optind-1]);
1884 }
1885 }
1886 invert = FALSE;
1887 }
1888
1889 for (m = iptables_matches; m; m = m->next)
1890 m->final_check(m->mflags);
1891 if (target)
1892 target->final_check(target->tflags);
1893
1894 /* Fix me: must put inverse options checking here --MN */
1895
1896 if (optind < argc)
1897 exit_error(PARAMETER_PROBLEM,
1898 "unknown arguments found on commandline");
1899 if (!command)
1900 exit_error(PARAMETER_PROBLEM, "no command specified");
1901 if (invert)
1902 exit_error(PARAMETER_PROBLEM,
1903 "nothing appropriate following !");
1904
Marc Boucher744bd022000-04-22 22:36:10 +00001905 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1906 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001907 if (!(options & OPT_DESTINATION))
1908 dhostnetworkmask = "0.0.0.0/0";
1909 if (!(options & OPT_SOURCE))
1910 shostnetworkmask = "0.0.0.0/0";
1911 }
1912
1913 if (shostnetworkmask)
1914 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1915 &(fw.ip.smsk), &nsaddrs);
1916
1917 if (dhostnetworkmask)
1918 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1919 &(fw.ip.dmsk), &ndaddrs);
1920
1921 if ((nsaddrs > 1 || ndaddrs > 1) &&
1922 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1923 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1924 " source or destination IP addresses");
1925
1926 if (command == CMD_CHECK && fw.ip.invflags != 0)
1927 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1928 cmd2char(CMD_CHECK));
1929
1930 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1931 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1932 "specify a unique address");
1933
1934 generic_opt_check(command, options);
1935
1936 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1937 exit_error(PARAMETER_PROBLEM,
1938 "chain name `%s' too long (must be under %i chars)",
1939 chain, IPT_FUNCTION_MAXNAMELEN);
1940
1941 *handle = iptc_init(*table);
1942 if (!*handle)
1943 exit_error(VERSION_PROBLEM,
1944 "can't initialize iptables table `%s': %s",
1945 *table, iptc_strerror(errno));
1946
Marc Boucher744bd022000-04-22 22:36:10 +00001947 if (command == CMD_CHECK
1948 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001949 || command == CMD_DELETE
1950 || command == CMD_INSERT
1951 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001952 if (strcmp(chain, "PREROUTING") == 0
1953 || strcmp(chain, "INPUT") == 0) {
1954 /* -o not valid with incoming packets. */
1955 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001956 exit_error(PARAMETER_PROBLEM,
1957 "Can't use -%c with %s\n",
1958 opt2char(OPT_VIANAMEOUT),
1959 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001960 /* -i required with -C */
1961 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1962 exit_error(PARAMETER_PROBLEM,
1963 "Need -%c with %s\n",
1964 opt2char(OPT_VIANAMEIN),
1965 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001966 }
1967
Rusty Russella4860fd2000-06-17 16:13:02 +00001968 if (strcmp(chain, "POSTROUTING") == 0
1969 || strcmp(chain, "OUTPUT") == 0) {
1970 /* -i not valid with outgoing packets */
1971 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001972 exit_error(PARAMETER_PROBLEM,
1973 "Can't use -%c with %s\n",
1974 opt2char(OPT_VIANAMEIN),
1975 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001976 /* -o required with -C */
1977 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1978 exit_error(PARAMETER_PROBLEM,
1979 "Need -%c with %s\n",
1980 opt2char(OPT_VIANAMEOUT),
1981 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001982 }
1983
1984 if (target && iptc_is_chain(jumpto, *handle)) {
1985 printf("Warning: using chain %s, not extension\n",
1986 jumpto);
1987
1988 target = NULL;
1989 }
1990
1991 /* If they didn't specify a target, or it's a chain
1992 name, use standard. */
1993 if (!target
1994 && (strlen(jumpto) == 0
1995 || iptc_is_chain(jumpto, *handle))) {
1996 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001997
Rusty Russell52a51492000-05-02 16:44:29 +00001998 target = find_target(IPT_STANDARD_TARGET,
1999 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002000
2001 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002002 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002003 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002004 target->t->u.target_size = size;
2005 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 target->init(target->t, &fw.nfcache);
2007 }
2008
Rusty Russell7e53bf92000-03-20 07:03:28 +00002009 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002010 /* it is no chain, and we can't load a plugin.
2011 * We cannot know if the plugin is corrupt, non
2012 * existant OR if the user just misspelled a
2013 * chain. */
2014 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002015 } else {
2016 e = generate_entry(&fw, iptables_matches, target->t);
2017 }
2018 }
2019
2020 switch (command) {
2021 case CMD_APPEND:
2022 ret = append_entry(chain, e,
2023 nsaddrs, saddrs, ndaddrs, daddrs,
2024 options&OPT_VERBOSE,
2025 handle);
2026 break;
2027 case CMD_CHECK:
2028 ret = check_packet(chain, e,
2029 nsaddrs, saddrs, ndaddrs, daddrs,
2030 options&OPT_VERBOSE, handle);
2031 break;
2032 case CMD_DELETE:
2033 ret = delete_entry(chain, e,
2034 nsaddrs, saddrs, ndaddrs, daddrs,
2035 options&OPT_VERBOSE,
2036 handle);
2037 break;
2038 case CMD_DELETE_NUM:
2039 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2040 break;
2041 case CMD_REPLACE:
2042 ret = replace_entry(chain, e, rulenum - 1,
2043 saddrs, daddrs, options&OPT_VERBOSE,
2044 handle);
2045 break;
2046 case CMD_INSERT:
2047 ret = insert_entry(chain, e, rulenum - 1,
2048 nsaddrs, saddrs, ndaddrs, daddrs,
2049 options&OPT_VERBOSE,
2050 handle);
2051 break;
2052 case CMD_LIST:
2053 ret = list_entries(chain,
2054 options&OPT_VERBOSE,
2055 options&OPT_NUMERIC,
2056 options&OPT_EXPANDED,
2057 options&OPT_LINENUMBERS,
2058 handle);
2059 break;
2060 case CMD_FLUSH:
2061 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2062 break;
2063 case CMD_ZERO:
2064 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2065 break;
2066 case CMD_LIST|CMD_ZERO:
2067 ret = list_entries(chain,
2068 options&OPT_VERBOSE,
2069 options&OPT_NUMERIC,
2070 options&OPT_EXPANDED,
2071 options&OPT_LINENUMBERS,
2072 handle);
2073 if (ret)
2074 ret = zero_entries(chain,
2075 options&OPT_VERBOSE, handle);
2076 break;
2077 case CMD_NEW_CHAIN:
2078 ret = iptc_create_chain(chain, handle);
2079 break;
2080 case CMD_DELETE_CHAIN:
2081 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2082 break;
2083 case CMD_RENAME_CHAIN:
2084 ret = iptc_rename_chain(chain, newname, handle);
2085 break;
2086 case CMD_SET_POLICY:
2087 ret = iptc_set_policy(chain, policy, handle);
2088 break;
2089 default:
2090 /* We should never reach this... */
2091 exit_tryhelp(2);
2092 }
2093
2094 if (verbose > 1)
2095 dump_entries(*handle);
2096
2097 return ret;
2098}