blob: 73f400b5330e8b84a945c10758fb9e506fc70aa6 [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__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000129struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000130ipt_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
Harald Welteae1ff9f2000-12-01 14:26:20 +0000646 if (ptr)
647 ptr->used = 1;
648
Marc Bouchere6869a82000-03-20 06:03:29 +0000649 return ptr;
650}
651
Rusty Russell28381a42000-05-10 00:19:50 +0000652/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
653static struct iptables_match *
654find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
655{
656 int proto;
657
658 proto = string_to_number(pname, 0, 255);
659 if (proto != -1)
660 return find_match(proto_to_name(proto, nolookup), tryload);
661
662 return find_match(pname, tryload);
663}
664
Marc Bouchere6869a82000-03-20 06:03:29 +0000665static u_int16_t
666parse_protocol(const char *s)
667{
Rusty Russell28381a42000-05-10 00:19:50 +0000668 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000669
670 if (proto == -1) {
671 struct protoent *pent;
672
673 if ((pent = getprotobyname(s)))
674 proto = pent->p_proto;
675 else {
676 unsigned int i;
677 for (i = 0;
678 i < sizeof(chain_protos)/sizeof(struct pprot);
679 i++) {
680 if (strcmp(s, chain_protos[i].name) == 0) {
681 proto = chain_protos[i].num;
682 break;
683 }
684 }
685 if (i == sizeof(chain_protos)/sizeof(struct pprot))
686 exit_error(PARAMETER_PROBLEM,
687 "unknown protocol `%s' specified",
688 s);
689 }
690 }
691
692 return (u_int16_t)proto;
693}
694
695static void
696parse_interface(const char *arg, char *vianame, unsigned char *mask)
697{
698 int vialen = strlen(arg);
699 unsigned int i;
700
701 memset(mask, 0, IFNAMSIZ);
702 memset(vianame, 0, IFNAMSIZ);
703
704 if (vialen + 1 > IFNAMSIZ)
705 exit_error(PARAMETER_PROBLEM,
706 "interface name `%s' must be shorter than IFNAMSIZ"
707 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000708
Marc Bouchere6869a82000-03-20 06:03:29 +0000709 strcpy(vianame, arg);
710 if (vialen == 0)
711 memset(mask, 0, IFNAMSIZ);
712 else if (vianame[vialen - 1] == '+') {
713 memset(mask, 0xFF, vialen - 1);
714 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
715 /* Remove `+' */
716 vianame[vialen - 1] = '\0';
717 } else {
718 /* Include nul-terminator in match */
719 memset(mask, 0xFF, vialen + 1);
720 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
721 }
722 for (i = 0; vianame[i]; i++) {
723 if (!isalnum(vianame[i])) {
724 printf("Warning: wierd character in interface"
725 " `%s' (No aliases, :, ! or *).\n",
726 vianame);
727 break;
728 }
729 }
730}
731
732/* Can't be zero. */
733static int
734parse_rulenumber(const char *rule)
735{
736 int rulenum = string_to_number(rule, 1, INT_MAX);
737
738 if (rulenum == -1)
739 exit_error(PARAMETER_PROBLEM,
740 "Invalid rule number `%s'", rule);
741
742 return rulenum;
743}
744
745static const char *
746parse_target(const char *targetname)
747{
748 const char *ptr;
749
750 if (strlen(targetname) < 1)
751 exit_error(PARAMETER_PROBLEM,
752 "Invalid target name (too short)");
753
754 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
755 exit_error(PARAMETER_PROBLEM,
756 "Invalid target name `%s' (%i chars max)",
757 targetname, sizeof(ipt_chainlabel)-1);
758
759 for (ptr = targetname; *ptr; ptr++)
760 if (isspace(*ptr))
761 exit_error(PARAMETER_PROBLEM,
762 "Invalid target name `%s'", targetname);
763 return targetname;
764}
765
766static char *
767addr_to_network(const struct in_addr *addr)
768{
769 struct netent *net;
770
771 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
772 return (char *) net->n_name;
773
774 return (char *) NULL;
775}
776
777char *
778addr_to_dotted(const struct in_addr *addrp)
779{
780 static char buf[20];
781 const unsigned char *bytep;
782
783 bytep = (const unsigned char *) &(addrp->s_addr);
784 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
785 return buf;
786}
787static char *
788addr_to_anyname(const struct in_addr *addr)
789{
790 char *name;
791
792 if ((name = addr_to_host(addr)) != NULL ||
793 (name = addr_to_network(addr)) != NULL)
794 return name;
795
796 return addr_to_dotted(addr);
797}
798
799static char *
800mask_to_dotted(const struct in_addr *mask)
801{
802 int i;
803 static char buf[20];
804 u_int32_t maskaddr, bits;
805
806 maskaddr = ntohl(mask->s_addr);
807
808 if (maskaddr == 0xFFFFFFFFL)
809 /* we don't want to see "/32" */
810 return "";
811
812 i = 32;
813 bits = 0xFFFFFFFEL;
814 while (--i >= 0 && maskaddr != bits)
815 bits <<= 1;
816 if (i >= 0)
817 sprintf(buf, "/%d", i);
818 else
819 /* mask was not a decent combination of 1's and 0's */
820 sprintf(buf, "/%s", addr_to_dotted(mask));
821
822 return buf;
823}
824
825int
826string_to_number(const char *s, int min, int max)
827{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000828 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000829 char *end;
830
831 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000832 errno = 0;
833 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000834 if (*end == '\0' && end != s) {
835 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000836 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000837 return number;
838 }
839 return -1;
840}
841
842static void
843set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
844 int invert)
845{
846 if (*options & option)
847 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
848 opt2char(option));
849 *options |= option;
850
851 if (invert) {
852 unsigned int i;
853 for (i = 0; 1 << i != option; i++);
854
855 if (!inverse_for_options[i])
856 exit_error(PARAMETER_PROBLEM,
857 "cannot have ! before -%c",
858 opt2char(option));
859 *invflg |= inverse_for_options[i];
860 }
861}
862
863struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000864find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000865{
866 struct iptables_target *ptr;
867
868 /* Standard target? */
869 if (strcmp(name, "") == 0
870 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
871 || strcmp(name, IPTC_LABEL_DROP) == 0
872 || strcmp(name, IPTC_LABEL_QUEUE) == 0
873 || strcmp(name, IPTC_LABEL_RETURN) == 0)
874 name = "standard";
875
876 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
877 if (strcmp(name, ptr->name) == 0)
878 break;
879 }
880
Rusty Russell52a51492000-05-02 16:44:29 +0000881 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000882 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
883 + strlen(name)];
884 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000885 if (dlopen(path, RTLD_NOW)) {
886 /* Found library. If it didn't register itself,
887 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000888 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000889 if (!ptr)
890 exit_error(PARAMETER_PROBLEM,
891 "Couldn't load target `%s'\n",
892 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000893 } else if (tryload == LOAD_MUST_SUCCEED)
894 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000895 "Couldn't load target `%s':%s\n",
896 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000897 }
898
Harald Welteae1ff9f2000-12-01 14:26:20 +0000899 if (ptr)
900 ptr->used = 1;
901
Marc Bouchere6869a82000-03-20 06:03:29 +0000902 return ptr;
903}
904
905static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000906merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000907 unsigned int *option_offset)
908{
909 unsigned int num_old, num_new, i;
910 struct option *merge;
911
912 for (num_old = 0; oldopts[num_old].name; num_old++);
913 for (num_new = 0; newopts[num_new].name; num_new++);
914
915 global_option_offset += OPTION_OFFSET;
916 *option_offset = global_option_offset;
917
918 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
919 memcpy(merge, oldopts, num_old * sizeof(struct option));
920 for (i = 0; i < num_new; i++) {
921 merge[num_old + i] = newopts[i];
922 merge[num_old + i].val += *option_offset;
923 }
924 memset(merge + num_old + num_new, 0, sizeof(struct option));
925
926 return merge;
927}
928
929void
930register_match(struct iptables_match *me)
931{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000932 struct iptables_match **i;
933
Marc Bouchere6869a82000-03-20 06:03:29 +0000934 if (strcmp(me->version, program_version) != 0) {
935 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
936 program_name, me->name, me->version, program_version);
937 exit(1);
938 }
939
Rusty Russell52a51492000-05-02 16:44:29 +0000940 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000941 fprintf(stderr, "%s: match `%s' already registered.\n",
942 program_name, me->name);
943 exit(1);
944 }
945
Rusty Russell73f72f52000-07-03 10:17:57 +0000946 if (me->size != IPT_ALIGN(me->size)) {
947 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
948 program_name, me->name, me->size);
949 exit(1);
950 }
951
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000952 /* Append to list. */
953 for (i = &iptables_matches; *i; i = &(*i)->next);
954 me->next = NULL;
955 *i = me;
956
Marc Bouchere6869a82000-03-20 06:03:29 +0000957 me->m = NULL;
958 me->mflags = 0;
959
960 opts = merge_options(opts, me->extra_opts, &me->option_offset);
961}
962
963void
964register_target(struct iptables_target *me)
965{
966 if (strcmp(me->version, program_version) != 0) {
967 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
968 program_name, me->name, me->version, program_version);
969 exit(1);
970 }
971
Rusty Russell52a51492000-05-02 16:44:29 +0000972 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000973 fprintf(stderr, "%s: target `%s' already registered.\n",
974 program_name, me->name);
975 exit(1);
976 }
977
Rusty Russell73f72f52000-07-03 10:17:57 +0000978 if (me->size != IPT_ALIGN(me->size)) {
979 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
980 program_name, me->name, me->size);
981 exit(1);
982 }
983
Marc Bouchere6869a82000-03-20 06:03:29 +0000984 /* Prepend to list. */
985 me->next = iptables_targets;
986 iptables_targets = me;
987 me->t = NULL;
988 me->tflags = 0;
989
990 opts = merge_options(opts, me->extra_opts, &me->option_offset);
991}
992
993static void
994print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
995{
996 struct ipt_counters counters;
997 const char *pol = iptc_get_policy(chain, &counters, handle);
998 printf("Chain %s", chain);
999 if (pol) {
1000 printf(" (policy %s", pol);
1001 if (!(format & FMT_NOCOUNTS))
1002 printf(" %llu packets, %llu bytes",
1003 counters.pcnt, counters.bcnt);
1004 printf(")\n");
1005 } else {
1006 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001007 if (!iptc_get_references(&refs, chain, handle))
1008 printf(" (ERROR obtaining refs)\n");
1009 else
1010 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001011 }
1012
1013 if (format & FMT_LINENUMBERS)
1014 printf(FMT("%-4s ", "%s "), "num");
1015 if (!(format & FMT_NOCOUNTS)) {
1016 if (format & FMT_KILOMEGAGIGA) {
1017 printf(FMT("%5s ","%s "), "pkts");
1018 printf(FMT("%5s ","%s "), "bytes");
1019 } else {
1020 printf(FMT("%8s ","%s "), "pkts");
1021 printf(FMT("%10s ","%s "), "bytes");
1022 }
1023 }
1024 if (!(format & FMT_NOTARGET))
1025 printf(FMT("%-9s ","%s "), "target");
1026 fputs(" prot ", stdout);
1027 if (format & FMT_OPTIONS)
1028 fputs("opt", stdout);
1029 if (format & FMT_VIA) {
1030 printf(FMT(" %-6s ","%s "), "in");
1031 printf(FMT("%-6s ","%s "), "out");
1032 }
1033 printf(FMT(" %-19s ","%s "), "source");
1034 printf(FMT(" %-19s "," %s "), "destination");
1035 printf("\n");
1036}
1037
1038static void
1039print_num(u_int64_t number, unsigned int format)
1040{
1041 if (format & FMT_KILOMEGAGIGA) {
1042 if (number > 99999) {
1043 number = (number + 500) / 1000;
1044 if (number > 9999) {
1045 number = (number + 500) / 1000;
1046 if (number > 9999) {
1047 number = (number + 500) / 1000;
1048 printf(FMT("%4lluG ","%lluG "),number);
1049 }
1050 else printf(FMT("%4lluM ","%lluM "), number);
1051 } else
1052 printf(FMT("%4lluK ","%lluK "), number);
1053 } else
1054 printf(FMT("%5llu ","%llu "), number);
1055 } else
1056 printf(FMT("%8llu ","%llu "), number);
1057}
1058
1059static int
1060print_match(const struct ipt_entry_match *m,
1061 const struct ipt_ip *ip,
1062 int numeric)
1063{
Rusty Russell52a51492000-05-02 16:44:29 +00001064 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001065
1066 if (match) {
1067 if (match->print)
1068 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001069 else
Rusty Russellb039b022000-09-01 06:04:05 +00001070 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001071 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001072 if (m->u.user.name[0])
1073 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001074 }
1075 /* Don't stop iterating. */
1076 return 0;
1077}
1078
1079/* e is called `fw' here for hysterical raisins */
1080static void
1081print_firewall(const struct ipt_entry *fw,
1082 const char *targname,
1083 unsigned int num,
1084 unsigned int format,
1085 const iptc_handle_t handle)
1086{
1087 struct iptables_target *target = NULL;
1088 const struct ipt_entry_target *t;
1089 u_int8_t flags;
1090 char buf[BUFSIZ];
1091
1092 /* User creates a chain called "REJECT": this overrides the
1093 `REJECT' target module. Keep feeding them rope until the
1094 revolution... Bwahahahahah */
1095 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001096 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001097 else
Rusty Russell52a51492000-05-02 16:44:29 +00001098 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001099
1100 t = ipt_get_target((struct ipt_entry *)fw);
1101 flags = fw->ip.flags;
1102
1103 if (format & FMT_LINENUMBERS)
1104 printf(FMT("%-4u ", "%u "), num+1);
1105
1106 if (!(format & FMT_NOCOUNTS)) {
1107 print_num(fw->counters.pcnt, format);
1108 print_num(fw->counters.bcnt, format);
1109 }
1110
1111 if (!(format & FMT_NOTARGET))
1112 printf(FMT("%-9s ", "%s "), targname);
1113
1114 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1115 {
Rusty Russell28381a42000-05-10 00:19:50 +00001116 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001117 if (pname)
1118 printf(FMT("%-5s", "%s "), pname);
1119 else
1120 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1121 }
1122
1123 if (format & FMT_OPTIONS) {
1124 if (format & FMT_NOTABLE)
1125 fputs("opt ", stdout);
1126 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1127 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1128 fputc(' ', stdout);
1129 }
1130
1131 if (format & FMT_VIA) {
1132 char iface[IFNAMSIZ+2];
1133
1134 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1135 iface[0] = '!';
1136 iface[1] = '\0';
1137 }
1138 else iface[0] = '\0';
1139
1140 if (fw->ip.iniface[0] != '\0') {
1141 strcat(iface, fw->ip.iniface);
1142 /* If it doesn't compare the nul-term, it's a
1143 wildcard. */
1144 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1145 strcat(iface, "+");
1146 }
1147 else if (format & FMT_NUMERIC) strcat(iface, "*");
1148 else strcat(iface, "any");
1149 printf(FMT(" %-6s ","in %s "), iface);
1150
1151 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1152 iface[0] = '!';
1153 iface[1] = '\0';
1154 }
1155 else iface[0] = '\0';
1156
1157 if (fw->ip.outiface[0] != '\0') {
1158 strcat(iface, fw->ip.outiface);
1159 /* If it doesn't compare the nul-term, it's a
1160 wildcard. */
1161 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1162 strcat(iface, "+");
1163 }
1164 else if (format & FMT_NUMERIC) strcat(iface, "*");
1165 else strcat(iface, "any");
1166 printf(FMT("%-6s ","out %s "), iface);
1167 }
1168
1169 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1170 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1171 printf(FMT("%-19s ","%s "), "anywhere");
1172 else {
1173 if (format & FMT_NUMERIC)
1174 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1175 else
1176 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1177 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1178 printf(FMT("%-19s ","%s "), buf);
1179 }
1180
1181 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1182 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1183 printf(FMT("%-19s","-> %s"), "anywhere");
1184 else {
1185 if (format & FMT_NUMERIC)
1186 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1187 else
1188 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1189 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1190 printf(FMT("%-19s","-> %s"), buf);
1191 }
1192
1193 if (format & FMT_NOTABLE)
1194 fputs(" ", stdout);
1195
1196 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1197
1198 if (target) {
1199 if (target->print)
1200 /* Print the target information. */
1201 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001202 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001203 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001204 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001205
1206 if (!(format & FMT_NONEWLINE))
1207 fputc('\n', stdout);
1208}
1209
1210static void
1211print_firewall_line(const struct ipt_entry *fw,
1212 const iptc_handle_t h)
1213{
1214 struct ipt_entry_target *t;
1215
1216 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001217 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001218}
1219
1220static int
1221append_entry(const ipt_chainlabel chain,
1222 struct ipt_entry *fw,
1223 unsigned int nsaddrs,
1224 const struct in_addr saddrs[],
1225 unsigned int ndaddrs,
1226 const struct in_addr daddrs[],
1227 int verbose,
1228 iptc_handle_t *handle)
1229{
1230 unsigned int i, j;
1231 int ret = 1;
1232
1233 for (i = 0; i < nsaddrs; i++) {
1234 fw->ip.src.s_addr = saddrs[i].s_addr;
1235 for (j = 0; j < ndaddrs; j++) {
1236 fw->ip.dst.s_addr = daddrs[j].s_addr;
1237 if (verbose)
1238 print_firewall_line(fw, *handle);
1239 ret &= iptc_append_entry(chain, fw, handle);
1240 }
1241 }
1242
1243 return ret;
1244}
1245
1246static int
1247replace_entry(const ipt_chainlabel chain,
1248 struct ipt_entry *fw,
1249 unsigned int rulenum,
1250 const struct in_addr *saddr,
1251 const struct in_addr *daddr,
1252 int verbose,
1253 iptc_handle_t *handle)
1254{
1255 fw->ip.src.s_addr = saddr->s_addr;
1256 fw->ip.dst.s_addr = daddr->s_addr;
1257
1258 if (verbose)
1259 print_firewall_line(fw, *handle);
1260 return iptc_replace_entry(chain, fw, rulenum, handle);
1261}
1262
1263static int
1264insert_entry(const ipt_chainlabel chain,
1265 struct ipt_entry *fw,
1266 unsigned int rulenum,
1267 unsigned int nsaddrs,
1268 const struct in_addr saddrs[],
1269 unsigned int ndaddrs,
1270 const struct in_addr daddrs[],
1271 int verbose,
1272 iptc_handle_t *handle)
1273{
1274 unsigned int i, j;
1275 int ret = 1;
1276
1277 for (i = 0; i < nsaddrs; i++) {
1278 fw->ip.src.s_addr = saddrs[i].s_addr;
1279 for (j = 0; j < ndaddrs; j++) {
1280 fw->ip.dst.s_addr = daddrs[j].s_addr;
1281 if (verbose)
1282 print_firewall_line(fw, *handle);
1283 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1284 }
1285 }
1286
1287 return ret;
1288}
1289
Rusty Russell2e0a3212000-04-19 11:23:18 +00001290static unsigned char *
1291make_delete_mask(struct ipt_entry *fw)
1292{
1293 /* Establish mask for comparison */
1294 unsigned int size;
1295 struct iptables_match *m;
1296 unsigned char *mask, *mptr;
1297
1298 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001299 for (m = iptables_matches; m && m->used; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001300 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001301
Rusty Russell9e1d2142000-04-23 09:11:12 +00001302 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001303 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001304 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001305
Rusty Russell9e1d2142000-04-23 09:11:12 +00001306 memset(mask, 0xFF, sizeof(struct ipt_entry));
1307 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001308
Harald Welteae1ff9f2000-12-01 14:26:20 +00001309 for (m = iptables_matches; m && m->used; m = m->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001310 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001311 IPT_ALIGN(sizeof(struct ipt_entry_match))
1312 + m->userspacesize);
1313 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001314 }
1315
Rusty Russell73f72f52000-07-03 10:17:57 +00001316 memset(mptr, 0xFF,
1317 IPT_ALIGN(sizeof(struct ipt_entry_target))
1318 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001319
1320 return mask;
1321}
1322
Marc Bouchere6869a82000-03-20 06:03:29 +00001323static int
1324delete_entry(const ipt_chainlabel chain,
1325 struct ipt_entry *fw,
1326 unsigned int nsaddrs,
1327 const struct in_addr saddrs[],
1328 unsigned int ndaddrs,
1329 const struct in_addr daddrs[],
1330 int verbose,
1331 iptc_handle_t *handle)
1332{
1333 unsigned int i, j;
1334 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001335 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001336
Rusty Russell2e0a3212000-04-19 11:23:18 +00001337 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 for (i = 0; i < nsaddrs; i++) {
1339 fw->ip.src.s_addr = saddrs[i].s_addr;
1340 for (j = 0; j < ndaddrs; j++) {
1341 fw->ip.dst.s_addr = daddrs[j].s_addr;
1342 if (verbose)
1343 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001344 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001345 }
1346 }
1347 return ret;
1348}
1349
1350static int
1351check_packet(const ipt_chainlabel chain,
1352 struct ipt_entry *fw,
1353 unsigned int nsaddrs,
1354 const struct in_addr saddrs[],
1355 unsigned int ndaddrs,
1356 const struct in_addr daddrs[],
1357 int verbose,
1358 iptc_handle_t *handle)
1359{
1360 int ret = 1;
1361 unsigned int i, j;
1362 const char *msg;
1363
1364 for (i = 0; i < nsaddrs; i++) {
1365 fw->ip.src.s_addr = saddrs[i].s_addr;
1366 for (j = 0; j < ndaddrs; j++) {
1367 fw->ip.dst.s_addr = daddrs[j].s_addr;
1368 if (verbose)
1369 print_firewall_line(fw, *handle);
1370 msg = iptc_check_packet(chain, fw, handle);
1371 if (!msg) ret = 0;
1372 else printf("%s\n", msg);
1373 }
1374 }
1375
1376 return ret;
1377}
1378
Harald Welteae1ff9f2000-12-01 14:26:20 +00001379int
Marc Bouchere6869a82000-03-20 06:03:29 +00001380for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001381 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001382{
1383 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001384 const char *chain;
1385 char *chains;
1386 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001387
Rusty Russell9e1d2142000-04-23 09:11:12 +00001388 chain = iptc_first_chain(handle);
1389 while (chain) {
1390 chaincount++;
1391 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001392 }
1393
Rusty Russell9e1d2142000-04-23 09:11:12 +00001394 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1395 i = 0;
1396 chain = iptc_first_chain(handle);
1397 while (chain) {
1398 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1399 i++;
1400 chain = iptc_next_chain(handle);
1401 }
1402
1403 for (i = 0; i < chaincount; i++) {
1404 if (!builtinstoo
1405 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1406 *handle))
1407 continue;
1408 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1409 }
1410
1411 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001412 return ret;
1413}
1414
Harald Welteae1ff9f2000-12-01 14:26:20 +00001415int
Marc Bouchere6869a82000-03-20 06:03:29 +00001416flush_entries(const ipt_chainlabel chain, int verbose,
1417 iptc_handle_t *handle)
1418{
1419 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001420 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001421
1422 if (verbose)
1423 fprintf(stdout, "Flushing chain `%s'\n", chain);
1424 return iptc_flush_entries(chain, handle);
1425}
Marc Bouchere6869a82000-03-20 06:03:29 +00001426
1427static int
1428zero_entries(const ipt_chainlabel chain, int verbose,
1429 iptc_handle_t *handle)
1430{
1431 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001432 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001433
Marc Bouchere6869a82000-03-20 06:03:29 +00001434 if (verbose)
1435 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1436 return iptc_zero_entries(chain, handle);
1437}
1438
Harald Welteae1ff9f2000-12-01 14:26:20 +00001439int
Marc Bouchere6869a82000-03-20 06:03:29 +00001440delete_chain(const ipt_chainlabel chain, int verbose,
1441 iptc_handle_t *handle)
1442{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001443 if (!chain)
1444 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001445
1446 if (verbose)
1447 fprintf(stdout, "Deleting chain `%s'\n", chain);
1448 return iptc_delete_chain(chain, handle);
1449}
1450
1451static int
1452list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1453 int expanded, int linenumbers, iptc_handle_t *handle)
1454{
1455 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001456 unsigned int format;
1457 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001458
1459 format = FMT_OPTIONS;
1460 if (!verbose)
1461 format |= FMT_NOCOUNTS;
1462 else
1463 format |= FMT_VIA;
1464
1465 if (numeric)
1466 format |= FMT_NUMERIC;
1467
1468 if (!expanded)
1469 format |= FMT_KILOMEGAGIGA;
1470
1471 if (linenumbers)
1472 format |= FMT_LINENUMBERS;
1473
Rusty Russell9e1d2142000-04-23 09:11:12 +00001474 for (this = iptc_first_chain(handle);
1475 this;
1476 this = iptc_next_chain(handle)) {
1477 const struct ipt_entry *i;
1478 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001479
Marc Bouchere6869a82000-03-20 06:03:29 +00001480 if (chain && strcmp(chain, this) != 0)
1481 continue;
1482
1483 if (found) printf("\n");
1484
1485 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001486 i = iptc_first_rule(this, handle);
1487
1488 num = 0;
1489 while (i) {
1490 print_firewall(i,
1491 iptc_get_target(i, handle),
1492 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001493 format,
1494 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001495 i = iptc_next_rule(i, handle);
1496 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001497 found = 1;
1498 }
1499
1500 errno = ENOENT;
1501 return found;
1502}
1503
1504static struct ipt_entry *
1505generate_entry(const struct ipt_entry *fw,
1506 struct iptables_match *matches,
1507 struct ipt_entry_target *target)
1508{
1509 unsigned int size;
1510 struct iptables_match *m;
1511 struct ipt_entry *e;
1512
1513 size = sizeof(struct ipt_entry);
Harald Welteae1ff9f2000-12-01 14:26:20 +00001514 for (m = matches; m && m->used; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001515 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001516
Rusty Russell228e98d2000-04-27 10:28:06 +00001517 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001518 *e = *fw;
1519 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001520 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001521
1522 size = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001523 for (m = matches; m && m->used; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001524 memcpy(e->elems + size, m->m, m->m->u.match_size);
1525 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001526 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001527 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001528
1529 return e;
1530}
1531
1532int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1533{
1534 struct ipt_entry fw, *e = NULL;
1535 int invert = 0;
1536 unsigned int nsaddrs = 0, ndaddrs = 0;
1537 struct in_addr *saddrs = NULL, *daddrs = NULL;
1538
1539 int c, verbose = 0;
1540 const char *chain = NULL;
1541 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1542 const char *policy = NULL, *newname = NULL;
1543 unsigned int rulenum = 0, options = 0, command = 0;
1544 int ret = 1;
1545 struct iptables_match *m;
1546 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001547 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001548 const char *jumpto = "";
1549 char *protocol = NULL;
1550
1551 memset(&fw, 0, sizeof(fw));
1552
Harald Welteae1ff9f2000-12-01 14:26:20 +00001553 /* re-set optind to 0 in case do_command gets called
1554 * a second time */
1555 optind = 0;
1556
1557 /* clear mflags in case do_command gets called a second time
1558 * (we clear the global list of all matches for security)*/
1559 for (m = iptables_matches; m; m = m->next) {
1560 m->mflags = 0;
1561 m->used = 0;
1562 }
1563
1564 for (t = iptables_targets; t; t = t->next) {
1565 t->tflags = 0;
1566 t->used = 0;
1567 }
1568
Marc Bouchere6869a82000-03-20 06:03:29 +00001569 /* Suppress error messages: we may add new options if we
1570 demand-load a protocol. */
1571 opterr = 0;
1572
1573 while ((c = getopt_long(argc, argv,
1574 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1575 opts, NULL)) != -1) {
1576 switch (c) {
1577 /*
1578 * Command selection
1579 */
1580 case 'A':
1581 add_command(&command, CMD_APPEND, CMD_NONE,
1582 invert);
1583 chain = optarg;
1584 break;
1585
1586 case 'D':
1587 add_command(&command, CMD_DELETE, CMD_NONE,
1588 invert);
1589 chain = optarg;
1590 if (optind < argc && argv[optind][0] != '-'
1591 && argv[optind][0] != '!') {
1592 rulenum = parse_rulenumber(argv[optind++]);
1593 command = CMD_DELETE_NUM;
1594 }
1595 break;
1596
1597 case 'C':
1598 add_command(&command, CMD_CHECK, CMD_NONE,
1599 invert);
1600 chain = optarg;
1601 break;
1602
1603 case 'R':
1604 add_command(&command, CMD_REPLACE, CMD_NONE,
1605 invert);
1606 chain = optarg;
1607 if (optind < argc && argv[optind][0] != '-'
1608 && argv[optind][0] != '!')
1609 rulenum = parse_rulenumber(argv[optind++]);
1610 else
1611 exit_error(PARAMETER_PROBLEM,
1612 "-%c requires a rule number",
1613 cmd2char(CMD_REPLACE));
1614 break;
1615
1616 case 'I':
1617 add_command(&command, CMD_INSERT, CMD_NONE,
1618 invert);
1619 chain = optarg;
1620 if (optind < argc && argv[optind][0] != '-'
1621 && argv[optind][0] != '!')
1622 rulenum = parse_rulenumber(argv[optind++]);
1623 else rulenum = 1;
1624 break;
1625
1626 case 'L':
1627 add_command(&command, CMD_LIST, CMD_ZERO,
1628 invert);
1629 if (optarg) chain = optarg;
1630 else if (optind < argc && argv[optind][0] != '-'
1631 && argv[optind][0] != '!')
1632 chain = argv[optind++];
1633 break;
1634
1635 case 'F':
1636 add_command(&command, CMD_FLUSH, CMD_NONE,
1637 invert);
1638 if (optarg) chain = optarg;
1639 else if (optind < argc && argv[optind][0] != '-'
1640 && argv[optind][0] != '!')
1641 chain = argv[optind++];
1642 break;
1643
1644 case 'Z':
1645 add_command(&command, CMD_ZERO, CMD_LIST,
1646 invert);
1647 if (optarg) chain = optarg;
1648 else if (optind < argc && argv[optind][0] != '-'
1649 && argv[optind][0] != '!')
1650 chain = argv[optind++];
1651 break;
1652
1653 case 'N':
1654 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1655 invert);
1656 chain = optarg;
1657 break;
1658
1659 case 'X':
1660 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1661 invert);
1662 if (optarg) chain = optarg;
1663 else if (optind < argc && argv[optind][0] != '-'
1664 && argv[optind][0] != '!')
1665 chain = argv[optind++];
1666 break;
1667
1668 case 'E':
1669 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1670 invert);
1671 chain = optarg;
1672 if (optind < argc && argv[optind][0] != '-'
1673 && argv[optind][0] != '!')
1674 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001675 else
1676 exit_error(PARAMETER_PROBLEM,
1677 "-%c requires old-chain-name and "
1678 "new-chain-name",
1679 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 break;
1681
1682 case 'P':
1683 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1684 invert);
1685 chain = optarg;
1686 if (optind < argc && argv[optind][0] != '-'
1687 && argv[optind][0] != '!')
1688 policy = argv[optind++];
1689 else
1690 exit_error(PARAMETER_PROBLEM,
1691 "-%c requires a chain and a policy",
1692 cmd2char(CMD_SET_POLICY));
1693 break;
1694
1695 case 'h':
1696 if (!optarg)
1697 optarg = argv[optind];
1698
Rusty Russell2e0a3212000-04-19 11:23:18 +00001699 /* iptables -p icmp -h */
1700 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001701 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001702
Marc Bouchere6869a82000-03-20 06:03:29 +00001703 exit_printhelp();
1704
1705 /*
1706 * Option selection
1707 */
1708 case 'p':
1709 if (check_inverse(optarg, &invert))
1710 optind++;
1711 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1712 invert);
1713
1714 /* Canonicalize into lower case */
1715 for (protocol = argv[optind-1]; *protocol; protocol++)
1716 *protocol = tolower(*protocol);
1717
1718 protocol = argv[optind-1];
1719 fw.ip.proto = parse_protocol(protocol);
1720
1721 if (fw.ip.proto == 0
1722 && (fw.ip.invflags & IPT_INV_PROTO))
1723 exit_error(PARAMETER_PROBLEM,
1724 "rule would never match protocol");
1725 fw.nfcache |= NFC_IP_PROTO;
1726 break;
1727
1728 case 's':
1729 if (check_inverse(optarg, &invert))
1730 optind++;
1731 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1732 invert);
1733 shostnetworkmask = argv[optind-1];
1734 fw.nfcache |= NFC_IP_SRC;
1735 break;
1736
1737 case 'd':
1738 if (check_inverse(optarg, &invert))
1739 optind++;
1740 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1741 invert);
1742 dhostnetworkmask = argv[optind-1];
1743 fw.nfcache |= NFC_IP_DST;
1744 break;
1745
1746 case 'j':
1747 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1748 invert);
1749 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001750 /* TRY_LOAD (may be chain name) */
1751 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001752
1753 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001754 size_t size;
1755
Rusty Russell73f72f52000-07-03 10:17:57 +00001756 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1757 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001758
Rusty Russell2e0a3212000-04-19 11:23:18 +00001759 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001760 target->t->u.target_size = size;
1761 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001762 target->init(target->t, &fw.nfcache);
1763 }
1764 break;
1765
1766
1767 case 'i':
1768 if (check_inverse(optarg, &invert))
1769 optind++;
1770 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1771 invert);
1772 parse_interface(argv[optind-1],
1773 fw.ip.iniface,
1774 fw.ip.iniface_mask);
1775 fw.nfcache |= NFC_IP_IF_IN;
1776 break;
1777
1778 case 'o':
1779 if (check_inverse(optarg, &invert))
1780 optind++;
1781 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1782 invert);
1783 parse_interface(argv[optind-1],
1784 fw.ip.outiface,
1785 fw.ip.outiface_mask);
1786 fw.nfcache |= NFC_IP_IF_OUT;
1787 break;
1788
1789 case 'f':
1790 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1791 invert);
1792 fw.ip.flags |= IPT_F_FRAG;
1793 fw.nfcache |= NFC_IP_FRAG;
1794 break;
1795
1796 case 'v':
1797 if (!verbose)
1798 set_option(&options, OPT_VERBOSE,
1799 &fw.ip.invflags, invert);
1800 verbose++;
1801 break;
1802
Rusty Russell52a51492000-05-02 16:44:29 +00001803 case 'm': {
1804 size_t size;
1805
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 if (invert)
1807 exit_error(PARAMETER_PROBLEM,
1808 "unexpected ! flag before --match");
1809
Rusty Russell52a51492000-05-02 16:44:29 +00001810 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001811 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1812 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001813 m->m = fw_calloc(1, size);
1814 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001815 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001816 m->init(m->m, &fw.nfcache);
1817 }
1818 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001819
1820 case 'n':
1821 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1822 invert);
1823 break;
1824
1825 case 't':
1826 if (invert)
1827 exit_error(PARAMETER_PROBLEM,
1828 "unexpected ! flag before --table");
1829 *table = argv[optind-1];
1830 break;
1831
1832 case 'x':
1833 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1834 invert);
1835 break;
1836
1837 case 'V':
1838 if (invert)
1839 printf("Not %s ;-)\n", program_version);
1840 else
1841 printf("%s v%s\n",
1842 program_name, program_version);
1843 exit(0);
1844
1845 case '0':
1846 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1847 invert);
1848 break;
1849
1850 case 1: /* non option */
1851 if (optarg[0] == '!' && optarg[1] == '\0') {
1852 if (invert)
1853 exit_error(PARAMETER_PROBLEM,
1854 "multiple consecutive ! not"
1855 " allowed");
1856 invert = TRUE;
1857 optarg[0] = '\0';
1858 continue;
1859 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001860 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001861 exit_tryhelp(2);
1862
1863 default:
1864 /* FIXME: This scheme doesn't allow two of the same
1865 matches --RR */
1866 if (!target
1867 || !(target->parse(c - target->option_offset,
1868 argv, invert,
1869 &target->tflags,
1870 &fw, &target->t))) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001871 for (m = iptables_matches; m && m->used; m = m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001872 if (m->parse(c - m->option_offset,
1873 argv, invert,
1874 &m->mflags,
1875 &fw,
1876 &fw.nfcache,
1877 &m->m))
1878 break;
1879 }
1880
1881 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001882 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001883 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001884 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001885 && !find_proto(protocol, DONT_LOAD,
1886 options&OPT_NUMERIC)
1887 && (m = find_proto(protocol, TRY_LOAD,
1888 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001889 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001890 size_t size;
1891
Rusty Russell73f72f52000-07-03 10:17:57 +00001892 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1893 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001894
Rusty Russell2e0a3212000-04-19 11:23:18 +00001895 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001896 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001897 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001898 m->init(m->m, &fw.nfcache);
1899
1900 optind--;
1901 continue;
1902 }
1903 if (!m)
1904 exit_error(PARAMETER_PROBLEM,
1905 "Unknown arg `%s'",
1906 argv[optind-1]);
1907 }
1908 }
1909 invert = FALSE;
1910 }
1911
Harald Welteae1ff9f2000-12-01 14:26:20 +00001912 for (m = iptables_matches; m && m->used; m = m->next)
Marc Bouchere6869a82000-03-20 06:03:29 +00001913 m->final_check(m->mflags);
1914 if (target)
1915 target->final_check(target->tflags);
1916
1917 /* Fix me: must put inverse options checking here --MN */
1918
1919 if (optind < argc)
1920 exit_error(PARAMETER_PROBLEM,
1921 "unknown arguments found on commandline");
1922 if (!command)
1923 exit_error(PARAMETER_PROBLEM, "no command specified");
1924 if (invert)
1925 exit_error(PARAMETER_PROBLEM,
1926 "nothing appropriate following !");
1927
Marc Boucher744bd022000-04-22 22:36:10 +00001928 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1929 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001930 if (!(options & OPT_DESTINATION))
1931 dhostnetworkmask = "0.0.0.0/0";
1932 if (!(options & OPT_SOURCE))
1933 shostnetworkmask = "0.0.0.0/0";
1934 }
1935
1936 if (shostnetworkmask)
1937 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1938 &(fw.ip.smsk), &nsaddrs);
1939
1940 if (dhostnetworkmask)
1941 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1942 &(fw.ip.dmsk), &ndaddrs);
1943
1944 if ((nsaddrs > 1 || ndaddrs > 1) &&
1945 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1946 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1947 " source or destination IP addresses");
1948
1949 if (command == CMD_CHECK && fw.ip.invflags != 0)
1950 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1951 cmd2char(CMD_CHECK));
1952
1953 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1954 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1955 "specify a unique address");
1956
1957 generic_opt_check(command, options);
1958
1959 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1960 exit_error(PARAMETER_PROBLEM,
1961 "chain name `%s' too long (must be under %i chars)",
1962 chain, IPT_FUNCTION_MAXNAMELEN);
1963
Harald Welteae1ff9f2000-12-01 14:26:20 +00001964 /* only allocate handle if we weren't called with a handle */
1965 if (!*handle)
1966 *handle = iptc_init(*table);
1967
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 if (!*handle)
1969 exit_error(VERSION_PROBLEM,
1970 "can't initialize iptables table `%s': %s",
1971 *table, iptc_strerror(errno));
1972
Marc Boucher744bd022000-04-22 22:36:10 +00001973 if (command == CMD_CHECK
1974 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001975 || command == CMD_DELETE
1976 || command == CMD_INSERT
1977 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001978 if (strcmp(chain, "PREROUTING") == 0
1979 || strcmp(chain, "INPUT") == 0) {
1980 /* -o not valid with incoming packets. */
1981 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001982 exit_error(PARAMETER_PROBLEM,
1983 "Can't use -%c with %s\n",
1984 opt2char(OPT_VIANAMEOUT),
1985 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001986 /* -i required with -C */
1987 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1988 exit_error(PARAMETER_PROBLEM,
1989 "Need -%c with %s\n",
1990 opt2char(OPT_VIANAMEIN),
1991 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001992 }
1993
Rusty Russella4860fd2000-06-17 16:13:02 +00001994 if (strcmp(chain, "POSTROUTING") == 0
1995 || strcmp(chain, "OUTPUT") == 0) {
1996 /* -i not valid with outgoing packets */
1997 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 exit_error(PARAMETER_PROBLEM,
1999 "Can't use -%c with %s\n",
2000 opt2char(OPT_VIANAMEIN),
2001 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002002 /* -o required with -C */
2003 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2004 exit_error(PARAMETER_PROBLEM,
2005 "Need -%c with %s\n",
2006 opt2char(OPT_VIANAMEOUT),
2007 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 }
2009
2010 if (target && iptc_is_chain(jumpto, *handle)) {
2011 printf("Warning: using chain %s, not extension\n",
2012 jumpto);
2013
2014 target = NULL;
2015 }
2016
2017 /* If they didn't specify a target, or it's a chain
2018 name, use standard. */
2019 if (!target
2020 && (strlen(jumpto) == 0
2021 || iptc_is_chain(jumpto, *handle))) {
2022 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002023
Rusty Russell52a51492000-05-02 16:44:29 +00002024 target = find_target(IPT_STANDARD_TARGET,
2025 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026
2027 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002028 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002029 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002030 target->t->u.target_size = size;
2031 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002032 target->init(target->t, &fw.nfcache);
2033 }
2034
Rusty Russell7e53bf92000-03-20 07:03:28 +00002035 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002036 /* it is no chain, and we can't load a plugin.
2037 * We cannot know if the plugin is corrupt, non
2038 * existant OR if the user just misspelled a
2039 * chain. */
2040 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 } else {
2042 e = generate_entry(&fw, iptables_matches, target->t);
2043 }
2044 }
2045
2046 switch (command) {
2047 case CMD_APPEND:
2048 ret = append_entry(chain, e,
2049 nsaddrs, saddrs, ndaddrs, daddrs,
2050 options&OPT_VERBOSE,
2051 handle);
2052 break;
2053 case CMD_CHECK:
2054 ret = check_packet(chain, e,
2055 nsaddrs, saddrs, ndaddrs, daddrs,
2056 options&OPT_VERBOSE, handle);
2057 break;
2058 case CMD_DELETE:
2059 ret = delete_entry(chain, e,
2060 nsaddrs, saddrs, ndaddrs, daddrs,
2061 options&OPT_VERBOSE,
2062 handle);
2063 break;
2064 case CMD_DELETE_NUM:
2065 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2066 break;
2067 case CMD_REPLACE:
2068 ret = replace_entry(chain, e, rulenum - 1,
2069 saddrs, daddrs, options&OPT_VERBOSE,
2070 handle);
2071 break;
2072 case CMD_INSERT:
2073 ret = insert_entry(chain, e, rulenum - 1,
2074 nsaddrs, saddrs, ndaddrs, daddrs,
2075 options&OPT_VERBOSE,
2076 handle);
2077 break;
2078 case CMD_LIST:
2079 ret = list_entries(chain,
2080 options&OPT_VERBOSE,
2081 options&OPT_NUMERIC,
2082 options&OPT_EXPANDED,
2083 options&OPT_LINENUMBERS,
2084 handle);
2085 break;
2086 case CMD_FLUSH:
2087 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2088 break;
2089 case CMD_ZERO:
2090 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2091 break;
2092 case CMD_LIST|CMD_ZERO:
2093 ret = list_entries(chain,
2094 options&OPT_VERBOSE,
2095 options&OPT_NUMERIC,
2096 options&OPT_EXPANDED,
2097 options&OPT_LINENUMBERS,
2098 handle);
2099 if (ret)
2100 ret = zero_entries(chain,
2101 options&OPT_VERBOSE, handle);
2102 break;
2103 case CMD_NEW_CHAIN:
2104 ret = iptc_create_chain(chain, handle);
2105 break;
2106 case CMD_DELETE_CHAIN:
2107 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2108 break;
2109 case CMD_RENAME_CHAIN:
2110 ret = iptc_rename_chain(chain, newname, handle);
2111 break;
2112 case CMD_SET_POLICY:
2113 ret = iptc_set_policy(chain, policy, handle);
2114 break;
2115 default:
2116 /* We should never reach this... */
2117 exit_tryhelp(2);
2118 }
2119
2120 if (verbose > 1)
2121 dump_entries(*handle);
2122
2123 return ret;
2124}