blob: d94cbd9c103da9b678c680788bb5029bf853da86 [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);
1063 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001064 if (m->u.user.name[0])
1065 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001066 }
1067 /* Don't stop iterating. */
1068 return 0;
1069}
1070
1071/* e is called `fw' here for hysterical raisins */
1072static void
1073print_firewall(const struct ipt_entry *fw,
1074 const char *targname,
1075 unsigned int num,
1076 unsigned int format,
1077 const iptc_handle_t handle)
1078{
1079 struct iptables_target *target = NULL;
1080 const struct ipt_entry_target *t;
1081 u_int8_t flags;
1082 char buf[BUFSIZ];
1083
1084 /* User creates a chain called "REJECT": this overrides the
1085 `REJECT' target module. Keep feeding them rope until the
1086 revolution... Bwahahahahah */
1087 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001088 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001089 else
Rusty Russell52a51492000-05-02 16:44:29 +00001090 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001091
1092 t = ipt_get_target((struct ipt_entry *)fw);
1093 flags = fw->ip.flags;
1094
1095 if (format & FMT_LINENUMBERS)
1096 printf(FMT("%-4u ", "%u "), num+1);
1097
1098 if (!(format & FMT_NOCOUNTS)) {
1099 print_num(fw->counters.pcnt, format);
1100 print_num(fw->counters.bcnt, format);
1101 }
1102
1103 if (!(format & FMT_NOTARGET))
1104 printf(FMT("%-9s ", "%s "), targname);
1105
1106 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1107 {
Rusty Russell28381a42000-05-10 00:19:50 +00001108 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001109 if (pname)
1110 printf(FMT("%-5s", "%s "), pname);
1111 else
1112 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1113 }
1114
1115 if (format & FMT_OPTIONS) {
1116 if (format & FMT_NOTABLE)
1117 fputs("opt ", stdout);
1118 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1119 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1120 fputc(' ', stdout);
1121 }
1122
1123 if (format & FMT_VIA) {
1124 char iface[IFNAMSIZ+2];
1125
1126 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1127 iface[0] = '!';
1128 iface[1] = '\0';
1129 }
1130 else iface[0] = '\0';
1131
1132 if (fw->ip.iniface[0] != '\0') {
1133 strcat(iface, fw->ip.iniface);
1134 /* If it doesn't compare the nul-term, it's a
1135 wildcard. */
1136 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1137 strcat(iface, "+");
1138 }
1139 else if (format & FMT_NUMERIC) strcat(iface, "*");
1140 else strcat(iface, "any");
1141 printf(FMT(" %-6s ","in %s "), iface);
1142
1143 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1144 iface[0] = '!';
1145 iface[1] = '\0';
1146 }
1147 else iface[0] = '\0';
1148
1149 if (fw->ip.outiface[0] != '\0') {
1150 strcat(iface, fw->ip.outiface);
1151 /* If it doesn't compare the nul-term, it's a
1152 wildcard. */
1153 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1154 strcat(iface, "+");
1155 }
1156 else if (format & FMT_NUMERIC) strcat(iface, "*");
1157 else strcat(iface, "any");
1158 printf(FMT("%-6s ","out %s "), iface);
1159 }
1160
1161 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1162 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1163 printf(FMT("%-19s ","%s "), "anywhere");
1164 else {
1165 if (format & FMT_NUMERIC)
1166 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1167 else
1168 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1169 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1170 printf(FMT("%-19s ","%s "), buf);
1171 }
1172
1173 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1174 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1175 printf(FMT("%-19s","-> %s"), "anywhere");
1176 else {
1177 if (format & FMT_NUMERIC)
1178 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1179 else
1180 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1181 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1182 printf(FMT("%-19s","-> %s"), buf);
1183 }
1184
1185 if (format & FMT_NOTABLE)
1186 fputs(" ", stdout);
1187
1188 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1189
1190 if (target) {
1191 if (target->print)
1192 /* Print the target information. */
1193 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001194 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001195 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001196 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001197
1198 if (!(format & FMT_NONEWLINE))
1199 fputc('\n', stdout);
1200}
1201
1202static void
1203print_firewall_line(const struct ipt_entry *fw,
1204 const iptc_handle_t h)
1205{
1206 struct ipt_entry_target *t;
1207
1208 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001209 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001210}
1211
1212static int
1213append_entry(const ipt_chainlabel chain,
1214 struct ipt_entry *fw,
1215 unsigned int nsaddrs,
1216 const struct in_addr saddrs[],
1217 unsigned int ndaddrs,
1218 const struct in_addr daddrs[],
1219 int verbose,
1220 iptc_handle_t *handle)
1221{
1222 unsigned int i, j;
1223 int ret = 1;
1224
1225 for (i = 0; i < nsaddrs; i++) {
1226 fw->ip.src.s_addr = saddrs[i].s_addr;
1227 for (j = 0; j < ndaddrs; j++) {
1228 fw->ip.dst.s_addr = daddrs[j].s_addr;
1229 if (verbose)
1230 print_firewall_line(fw, *handle);
1231 ret &= iptc_append_entry(chain, fw, handle);
1232 }
1233 }
1234
1235 return ret;
1236}
1237
1238static int
1239replace_entry(const ipt_chainlabel chain,
1240 struct ipt_entry *fw,
1241 unsigned int rulenum,
1242 const struct in_addr *saddr,
1243 const struct in_addr *daddr,
1244 int verbose,
1245 iptc_handle_t *handle)
1246{
1247 fw->ip.src.s_addr = saddr->s_addr;
1248 fw->ip.dst.s_addr = daddr->s_addr;
1249
1250 if (verbose)
1251 print_firewall_line(fw, *handle);
1252 return iptc_replace_entry(chain, fw, rulenum, handle);
1253}
1254
1255static int
1256insert_entry(const ipt_chainlabel chain,
1257 struct ipt_entry *fw,
1258 unsigned int rulenum,
1259 unsigned int nsaddrs,
1260 const struct in_addr saddrs[],
1261 unsigned int ndaddrs,
1262 const struct in_addr daddrs[],
1263 int verbose,
1264 iptc_handle_t *handle)
1265{
1266 unsigned int i, j;
1267 int ret = 1;
1268
1269 for (i = 0; i < nsaddrs; i++) {
1270 fw->ip.src.s_addr = saddrs[i].s_addr;
1271 for (j = 0; j < ndaddrs; j++) {
1272 fw->ip.dst.s_addr = daddrs[j].s_addr;
1273 if (verbose)
1274 print_firewall_line(fw, *handle);
1275 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1276 }
1277 }
1278
1279 return ret;
1280}
1281
Rusty Russell2e0a3212000-04-19 11:23:18 +00001282static unsigned char *
1283make_delete_mask(struct ipt_entry *fw)
1284{
1285 /* Establish mask for comparison */
1286 unsigned int size;
1287 struct iptables_match *m;
1288 unsigned char *mask, *mptr;
1289
1290 size = sizeof(struct ipt_entry);
1291 for (m = iptables_matches; m; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001292 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001293
Rusty Russell9e1d2142000-04-23 09:11:12 +00001294 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001295 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001296 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001297
Rusty Russell9e1d2142000-04-23 09:11:12 +00001298 memset(mask, 0xFF, sizeof(struct ipt_entry));
1299 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001300
1301 for (m = iptables_matches; m; m = m->next) {
1302 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001303 IPT_ALIGN(sizeof(struct ipt_entry_match))
1304 + m->userspacesize);
1305 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001306 }
1307
Rusty Russell73f72f52000-07-03 10:17:57 +00001308 memset(mptr, 0xFF,
1309 IPT_ALIGN(sizeof(struct ipt_entry_target))
1310 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001311
1312 return mask;
1313}
1314
Marc Bouchere6869a82000-03-20 06:03:29 +00001315static int
1316delete_entry(const ipt_chainlabel chain,
1317 struct ipt_entry *fw,
1318 unsigned int nsaddrs,
1319 const struct in_addr saddrs[],
1320 unsigned int ndaddrs,
1321 const struct in_addr daddrs[],
1322 int verbose,
1323 iptc_handle_t *handle)
1324{
1325 unsigned int i, j;
1326 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001327 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001328
Rusty Russell2e0a3212000-04-19 11:23:18 +00001329 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 for (i = 0; i < nsaddrs; i++) {
1331 fw->ip.src.s_addr = saddrs[i].s_addr;
1332 for (j = 0; j < ndaddrs; j++) {
1333 fw->ip.dst.s_addr = daddrs[j].s_addr;
1334 if (verbose)
1335 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001336 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001337 }
1338 }
1339 return ret;
1340}
1341
1342static int
1343check_packet(const ipt_chainlabel chain,
1344 struct ipt_entry *fw,
1345 unsigned int nsaddrs,
1346 const struct in_addr saddrs[],
1347 unsigned int ndaddrs,
1348 const struct in_addr daddrs[],
1349 int verbose,
1350 iptc_handle_t *handle)
1351{
1352 int ret = 1;
1353 unsigned int i, j;
1354 const char *msg;
1355
1356 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);
1362 msg = iptc_check_packet(chain, fw, handle);
1363 if (!msg) ret = 0;
1364 else printf("%s\n", msg);
1365 }
1366 }
1367
1368 return ret;
1369}
1370
1371static int
1372for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001373 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001374{
1375 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001376 const char *chain;
1377 char *chains;
1378 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001379
Rusty Russell9e1d2142000-04-23 09:11:12 +00001380 chain = iptc_first_chain(handle);
1381 while (chain) {
1382 chaincount++;
1383 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001384 }
1385
Rusty Russell9e1d2142000-04-23 09:11:12 +00001386 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1387 i = 0;
1388 chain = iptc_first_chain(handle);
1389 while (chain) {
1390 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1391 i++;
1392 chain = iptc_next_chain(handle);
1393 }
1394
1395 for (i = 0; i < chaincount; i++) {
1396 if (!builtinstoo
1397 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1398 *handle))
1399 continue;
1400 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1401 }
1402
1403 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001404 return ret;
1405}
1406
1407static int
1408flush_entries(const ipt_chainlabel chain, int verbose,
1409 iptc_handle_t *handle)
1410{
1411 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001412 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001413
1414 if (verbose)
1415 fprintf(stdout, "Flushing chain `%s'\n", chain);
1416 return iptc_flush_entries(chain, handle);
1417}
Marc Bouchere6869a82000-03-20 06:03:29 +00001418
1419static int
1420zero_entries(const ipt_chainlabel chain, int verbose,
1421 iptc_handle_t *handle)
1422{
1423 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001424 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001425
Marc Bouchere6869a82000-03-20 06:03:29 +00001426 if (verbose)
1427 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1428 return iptc_zero_entries(chain, handle);
1429}
1430
1431static int
1432delete_chain(const ipt_chainlabel chain, int verbose,
1433 iptc_handle_t *handle)
1434{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001435 if (!chain)
1436 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001437
1438 if (verbose)
1439 fprintf(stdout, "Deleting chain `%s'\n", chain);
1440 return iptc_delete_chain(chain, handle);
1441}
1442
1443static int
1444list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1445 int expanded, int linenumbers, iptc_handle_t *handle)
1446{
1447 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001448 unsigned int format;
1449 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001450
1451 format = FMT_OPTIONS;
1452 if (!verbose)
1453 format |= FMT_NOCOUNTS;
1454 else
1455 format |= FMT_VIA;
1456
1457 if (numeric)
1458 format |= FMT_NUMERIC;
1459
1460 if (!expanded)
1461 format |= FMT_KILOMEGAGIGA;
1462
1463 if (linenumbers)
1464 format |= FMT_LINENUMBERS;
1465
Rusty Russell9e1d2142000-04-23 09:11:12 +00001466 for (this = iptc_first_chain(handle);
1467 this;
1468 this = iptc_next_chain(handle)) {
1469 const struct ipt_entry *i;
1470 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001471
Marc Bouchere6869a82000-03-20 06:03:29 +00001472 if (chain && strcmp(chain, this) != 0)
1473 continue;
1474
1475 if (found) printf("\n");
1476
1477 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001478 i = iptc_first_rule(this, handle);
1479
1480 num = 0;
1481 while (i) {
1482 print_firewall(i,
1483 iptc_get_target(i, handle),
1484 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001485 format,
1486 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001487 i = iptc_next_rule(i, handle);
1488 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001489 found = 1;
1490 }
1491
1492 errno = ENOENT;
1493 return found;
1494}
1495
1496static struct ipt_entry *
1497generate_entry(const struct ipt_entry *fw,
1498 struct iptables_match *matches,
1499 struct ipt_entry_target *target)
1500{
1501 unsigned int size;
1502 struct iptables_match *m;
1503 struct ipt_entry *e;
1504
1505 size = sizeof(struct ipt_entry);
1506 for (m = matches; m; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001507 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001508
Rusty Russell228e98d2000-04-27 10:28:06 +00001509 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001510 *e = *fw;
1511 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001512 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001513
1514 size = 0;
1515 for (m = matches; m; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001516 memcpy(e->elems + size, m->m, m->m->u.match_size);
1517 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001518 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001519 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001520
1521 return e;
1522}
1523
1524int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1525{
1526 struct ipt_entry fw, *e = NULL;
1527 int invert = 0;
1528 unsigned int nsaddrs = 0, ndaddrs = 0;
1529 struct in_addr *saddrs = NULL, *daddrs = NULL;
1530
1531 int c, verbose = 0;
1532 const char *chain = NULL;
1533 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1534 const char *policy = NULL, *newname = NULL;
1535 unsigned int rulenum = 0, options = 0, command = 0;
1536 int ret = 1;
1537 struct iptables_match *m;
1538 struct iptables_target *target = NULL;
1539 const char *jumpto = "";
1540 char *protocol = NULL;
1541
1542 memset(&fw, 0, sizeof(fw));
1543
1544 /* Suppress error messages: we may add new options if we
1545 demand-load a protocol. */
1546 opterr = 0;
1547
1548 while ((c = getopt_long(argc, argv,
1549 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1550 opts, NULL)) != -1) {
1551 switch (c) {
1552 /*
1553 * Command selection
1554 */
1555 case 'A':
1556 add_command(&command, CMD_APPEND, CMD_NONE,
1557 invert);
1558 chain = optarg;
1559 break;
1560
1561 case 'D':
1562 add_command(&command, CMD_DELETE, CMD_NONE,
1563 invert);
1564 chain = optarg;
1565 if (optind < argc && argv[optind][0] != '-'
1566 && argv[optind][0] != '!') {
1567 rulenum = parse_rulenumber(argv[optind++]);
1568 command = CMD_DELETE_NUM;
1569 }
1570 break;
1571
1572 case 'C':
1573 add_command(&command, CMD_CHECK, CMD_NONE,
1574 invert);
1575 chain = optarg;
1576 break;
1577
1578 case 'R':
1579 add_command(&command, CMD_REPLACE, CMD_NONE,
1580 invert);
1581 chain = optarg;
1582 if (optind < argc && argv[optind][0] != '-'
1583 && argv[optind][0] != '!')
1584 rulenum = parse_rulenumber(argv[optind++]);
1585 else
1586 exit_error(PARAMETER_PROBLEM,
1587 "-%c requires a rule number",
1588 cmd2char(CMD_REPLACE));
1589 break;
1590
1591 case 'I':
1592 add_command(&command, CMD_INSERT, CMD_NONE,
1593 invert);
1594 chain = optarg;
1595 if (optind < argc && argv[optind][0] != '-'
1596 && argv[optind][0] != '!')
1597 rulenum = parse_rulenumber(argv[optind++]);
1598 else rulenum = 1;
1599 break;
1600
1601 case 'L':
1602 add_command(&command, CMD_LIST, CMD_ZERO,
1603 invert);
1604 if (optarg) chain = optarg;
1605 else if (optind < argc && argv[optind][0] != '-'
1606 && argv[optind][0] != '!')
1607 chain = argv[optind++];
1608 break;
1609
1610 case 'F':
1611 add_command(&command, CMD_FLUSH, CMD_NONE,
1612 invert);
1613 if (optarg) chain = optarg;
1614 else if (optind < argc && argv[optind][0] != '-'
1615 && argv[optind][0] != '!')
1616 chain = argv[optind++];
1617 break;
1618
1619 case 'Z':
1620 add_command(&command, CMD_ZERO, CMD_LIST,
1621 invert);
1622 if (optarg) chain = optarg;
1623 else if (optind < argc && argv[optind][0] != '-'
1624 && argv[optind][0] != '!')
1625 chain = argv[optind++];
1626 break;
1627
1628 case 'N':
1629 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1630 invert);
1631 chain = optarg;
1632 break;
1633
1634 case 'X':
1635 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1636 invert);
1637 if (optarg) chain = optarg;
1638 else if (optind < argc && argv[optind][0] != '-'
1639 && argv[optind][0] != '!')
1640 chain = argv[optind++];
1641 break;
1642
1643 case 'E':
1644 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1645 invert);
1646 chain = optarg;
1647 if (optind < argc && argv[optind][0] != '-'
1648 && argv[optind][0] != '!')
1649 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001650 else
1651 exit_error(PARAMETER_PROBLEM,
1652 "-%c requires old-chain-name and "
1653 "new-chain-name",
1654 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001655 break;
1656
1657 case 'P':
1658 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1659 invert);
1660 chain = optarg;
1661 if (optind < argc && argv[optind][0] != '-'
1662 && argv[optind][0] != '!')
1663 policy = argv[optind++];
1664 else
1665 exit_error(PARAMETER_PROBLEM,
1666 "-%c requires a chain and a policy",
1667 cmd2char(CMD_SET_POLICY));
1668 break;
1669
1670 case 'h':
1671 if (!optarg)
1672 optarg = argv[optind];
1673
Rusty Russell2e0a3212000-04-19 11:23:18 +00001674 /* iptables -p icmp -h */
1675 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001676 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001677
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 exit_printhelp();
1679
1680 /*
1681 * Option selection
1682 */
1683 case 'p':
1684 if (check_inverse(optarg, &invert))
1685 optind++;
1686 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1687 invert);
1688
1689 /* Canonicalize into lower case */
1690 for (protocol = argv[optind-1]; *protocol; protocol++)
1691 *protocol = tolower(*protocol);
1692
1693 protocol = argv[optind-1];
1694 fw.ip.proto = parse_protocol(protocol);
1695
1696 if (fw.ip.proto == 0
1697 && (fw.ip.invflags & IPT_INV_PROTO))
1698 exit_error(PARAMETER_PROBLEM,
1699 "rule would never match protocol");
1700 fw.nfcache |= NFC_IP_PROTO;
1701 break;
1702
1703 case 's':
1704 if (check_inverse(optarg, &invert))
1705 optind++;
1706 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1707 invert);
1708 shostnetworkmask = argv[optind-1];
1709 fw.nfcache |= NFC_IP_SRC;
1710 break;
1711
1712 case 'd':
1713 if (check_inverse(optarg, &invert))
1714 optind++;
1715 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1716 invert);
1717 dhostnetworkmask = argv[optind-1];
1718 fw.nfcache |= NFC_IP_DST;
1719 break;
1720
1721 case 'j':
1722 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1723 invert);
1724 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001725 /* TRY_LOAD (may be chain name) */
1726 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001727
1728 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001729 size_t size;
1730
Rusty Russell73f72f52000-07-03 10:17:57 +00001731 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1732 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001733
Rusty Russell2e0a3212000-04-19 11:23:18 +00001734 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001735 target->t->u.target_size = size;
1736 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001737 target->init(target->t, &fw.nfcache);
1738 }
1739 break;
1740
1741
1742 case 'i':
1743 if (check_inverse(optarg, &invert))
1744 optind++;
1745 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1746 invert);
1747 parse_interface(argv[optind-1],
1748 fw.ip.iniface,
1749 fw.ip.iniface_mask);
1750 fw.nfcache |= NFC_IP_IF_IN;
1751 break;
1752
1753 case 'o':
1754 if (check_inverse(optarg, &invert))
1755 optind++;
1756 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1757 invert);
1758 parse_interface(argv[optind-1],
1759 fw.ip.outiface,
1760 fw.ip.outiface_mask);
1761 fw.nfcache |= NFC_IP_IF_OUT;
1762 break;
1763
1764 case 'f':
1765 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1766 invert);
1767 fw.ip.flags |= IPT_F_FRAG;
1768 fw.nfcache |= NFC_IP_FRAG;
1769 break;
1770
1771 case 'v':
1772 if (!verbose)
1773 set_option(&options, OPT_VERBOSE,
1774 &fw.ip.invflags, invert);
1775 verbose++;
1776 break;
1777
Rusty Russell52a51492000-05-02 16:44:29 +00001778 case 'm': {
1779 size_t size;
1780
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 if (invert)
1782 exit_error(PARAMETER_PROBLEM,
1783 "unexpected ! flag before --match");
1784
Rusty Russell52a51492000-05-02 16:44:29 +00001785 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001786 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1787 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001788 m->m = fw_calloc(1, size);
1789 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001790 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001791 m->init(m->m, &fw.nfcache);
1792 }
1793 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001794
1795 case 'n':
1796 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1797 invert);
1798 break;
1799
1800 case 't':
1801 if (invert)
1802 exit_error(PARAMETER_PROBLEM,
1803 "unexpected ! flag before --table");
1804 *table = argv[optind-1];
1805 break;
1806
1807 case 'x':
1808 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1809 invert);
1810 break;
1811
1812 case 'V':
1813 if (invert)
1814 printf("Not %s ;-)\n", program_version);
1815 else
1816 printf("%s v%s\n",
1817 program_name, program_version);
1818 exit(0);
1819
1820 case '0':
1821 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1822 invert);
1823 break;
1824
1825 case 1: /* non option */
1826 if (optarg[0] == '!' && optarg[1] == '\0') {
1827 if (invert)
1828 exit_error(PARAMETER_PROBLEM,
1829 "multiple consecutive ! not"
1830 " allowed");
1831 invert = TRUE;
1832 optarg[0] = '\0';
1833 continue;
1834 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001835 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001836 exit_tryhelp(2);
1837
1838 default:
1839 /* FIXME: This scheme doesn't allow two of the same
1840 matches --RR */
1841 if (!target
1842 || !(target->parse(c - target->option_offset,
1843 argv, invert,
1844 &target->tflags,
1845 &fw, &target->t))) {
1846 for (m = iptables_matches; m; m = m->next) {
1847 if (m->parse(c - m->option_offset,
1848 argv, invert,
1849 &m->mflags,
1850 &fw,
1851 &fw.nfcache,
1852 &m->m))
1853 break;
1854 }
1855
1856 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001857 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001858 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001859 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001860 && !find_proto(protocol, DONT_LOAD,
1861 options&OPT_NUMERIC)
1862 && (m = find_proto(protocol, TRY_LOAD,
1863 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001864 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001865 size_t size;
1866
Rusty Russell73f72f52000-07-03 10:17:57 +00001867 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1868 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001869
Rusty Russell2e0a3212000-04-19 11:23:18 +00001870 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001871 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001872 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001873 m->init(m->m, &fw.nfcache);
1874
1875 optind--;
1876 continue;
1877 }
1878 if (!m)
1879 exit_error(PARAMETER_PROBLEM,
1880 "Unknown arg `%s'",
1881 argv[optind-1]);
1882 }
1883 }
1884 invert = FALSE;
1885 }
1886
1887 for (m = iptables_matches; m; m = m->next)
1888 m->final_check(m->mflags);
1889 if (target)
1890 target->final_check(target->tflags);
1891
1892 /* Fix me: must put inverse options checking here --MN */
1893
1894 if (optind < argc)
1895 exit_error(PARAMETER_PROBLEM,
1896 "unknown arguments found on commandline");
1897 if (!command)
1898 exit_error(PARAMETER_PROBLEM, "no command specified");
1899 if (invert)
1900 exit_error(PARAMETER_PROBLEM,
1901 "nothing appropriate following !");
1902
Marc Boucher744bd022000-04-22 22:36:10 +00001903 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1904 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 if (!(options & OPT_DESTINATION))
1906 dhostnetworkmask = "0.0.0.0/0";
1907 if (!(options & OPT_SOURCE))
1908 shostnetworkmask = "0.0.0.0/0";
1909 }
1910
1911 if (shostnetworkmask)
1912 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1913 &(fw.ip.smsk), &nsaddrs);
1914
1915 if (dhostnetworkmask)
1916 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1917 &(fw.ip.dmsk), &ndaddrs);
1918
1919 if ((nsaddrs > 1 || ndaddrs > 1) &&
1920 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1921 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1922 " source or destination IP addresses");
1923
1924 if (command == CMD_CHECK && fw.ip.invflags != 0)
1925 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1926 cmd2char(CMD_CHECK));
1927
1928 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1929 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1930 "specify a unique address");
1931
1932 generic_opt_check(command, options);
1933
1934 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1935 exit_error(PARAMETER_PROBLEM,
1936 "chain name `%s' too long (must be under %i chars)",
1937 chain, IPT_FUNCTION_MAXNAMELEN);
1938
1939 *handle = iptc_init(*table);
1940 if (!*handle)
1941 exit_error(VERSION_PROBLEM,
1942 "can't initialize iptables table `%s': %s",
1943 *table, iptc_strerror(errno));
1944
Marc Boucher744bd022000-04-22 22:36:10 +00001945 if (command == CMD_CHECK
1946 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001947 || command == CMD_DELETE
1948 || command == CMD_INSERT
1949 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001950 if (strcmp(chain, "PREROUTING") == 0
1951 || strcmp(chain, "INPUT") == 0) {
1952 /* -o not valid with incoming packets. */
1953 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001954 exit_error(PARAMETER_PROBLEM,
1955 "Can't use -%c with %s\n",
1956 opt2char(OPT_VIANAMEOUT),
1957 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001958 /* -i required with -C */
1959 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1960 exit_error(PARAMETER_PROBLEM,
1961 "Need -%c with %s\n",
1962 opt2char(OPT_VIANAMEIN),
1963 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001964 }
1965
Rusty Russella4860fd2000-06-17 16:13:02 +00001966 if (strcmp(chain, "POSTROUTING") == 0
1967 || strcmp(chain, "OUTPUT") == 0) {
1968 /* -i not valid with outgoing packets */
1969 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001970 exit_error(PARAMETER_PROBLEM,
1971 "Can't use -%c with %s\n",
1972 opt2char(OPT_VIANAMEIN),
1973 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001974 /* -o required with -C */
1975 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1976 exit_error(PARAMETER_PROBLEM,
1977 "Need -%c with %s\n",
1978 opt2char(OPT_VIANAMEOUT),
1979 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001980 }
1981
1982 if (target && iptc_is_chain(jumpto, *handle)) {
1983 printf("Warning: using chain %s, not extension\n",
1984 jumpto);
1985
1986 target = NULL;
1987 }
1988
1989 /* If they didn't specify a target, or it's a chain
1990 name, use standard. */
1991 if (!target
1992 && (strlen(jumpto) == 0
1993 || iptc_is_chain(jumpto, *handle))) {
1994 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001995
Rusty Russell52a51492000-05-02 16:44:29 +00001996 target = find_target(IPT_STANDARD_TARGET,
1997 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998
1999 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002000 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002001 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002002 target->t->u.target_size = size;
2003 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 target->init(target->t, &fw.nfcache);
2005 }
2006
Rusty Russell7e53bf92000-03-20 07:03:28 +00002007 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002008 /* it is no chain, and we can't load a plugin.
2009 * We cannot know if the plugin is corrupt, non
2010 * existant OR if the user just misspelled a
2011 * chain. */
2012 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002013 } else {
2014 e = generate_entry(&fw, iptables_matches, target->t);
2015 }
2016 }
2017
2018 switch (command) {
2019 case CMD_APPEND:
2020 ret = append_entry(chain, e,
2021 nsaddrs, saddrs, ndaddrs, daddrs,
2022 options&OPT_VERBOSE,
2023 handle);
2024 break;
2025 case CMD_CHECK:
2026 ret = check_packet(chain, e,
2027 nsaddrs, saddrs, ndaddrs, daddrs,
2028 options&OPT_VERBOSE, handle);
2029 break;
2030 case CMD_DELETE:
2031 ret = delete_entry(chain, e,
2032 nsaddrs, saddrs, ndaddrs, daddrs,
2033 options&OPT_VERBOSE,
2034 handle);
2035 break;
2036 case CMD_DELETE_NUM:
2037 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2038 break;
2039 case CMD_REPLACE:
2040 ret = replace_entry(chain, e, rulenum - 1,
2041 saddrs, daddrs, options&OPT_VERBOSE,
2042 handle);
2043 break;
2044 case CMD_INSERT:
2045 ret = insert_entry(chain, e, rulenum - 1,
2046 nsaddrs, saddrs, ndaddrs, daddrs,
2047 options&OPT_VERBOSE,
2048 handle);
2049 break;
2050 case CMD_LIST:
2051 ret = list_entries(chain,
2052 options&OPT_VERBOSE,
2053 options&OPT_NUMERIC,
2054 options&OPT_EXPANDED,
2055 options&OPT_LINENUMBERS,
2056 handle);
2057 break;
2058 case CMD_FLUSH:
2059 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2060 break;
2061 case CMD_ZERO:
2062 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2063 break;
2064 case CMD_LIST|CMD_ZERO:
2065 ret = list_entries(chain,
2066 options&OPT_VERBOSE,
2067 options&OPT_NUMERIC,
2068 options&OPT_EXPANDED,
2069 options&OPT_LINENUMBERS,
2070 handle);
2071 if (ret)
2072 ret = zero_entries(chain,
2073 options&OPT_VERBOSE, handle);
2074 break;
2075 case CMD_NEW_CHAIN:
2076 ret = iptc_create_chain(chain, handle);
2077 break;
2078 case CMD_DELETE_CHAIN:
2079 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2080 break;
2081 case CMD_RENAME_CHAIN:
2082 ret = iptc_rename_chain(chain, newname, handle);
2083 break;
2084 case CMD_SET_POLICY:
2085 ret = iptc_set_policy(chain, policy, handle);
2086 break;
2087 default:
2088 /* We should never reach this... */
2089 exit_tryhelp(2);
2090 }
2091
2092 if (verbose > 1)
2093 dump_entries(*handle);
2094
2095 return ret;
2096}