blob: 4dfa7d04eb4d048e56ce81b4f80bf6bf38da2b2d [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 },
203 { "all", 0 },
204};
205
206static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000207proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000208{
209 unsigned int i;
210
Rusty Russell28381a42000-05-10 00:19:50 +0000211 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000212 struct protoent *pent = getprotobynumber(proto);
213 if (pent)
214 return pent->p_name;
215 }
216
217 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
218 if (chain_protos[i].num == proto)
219 return chain_protos[i].name;
220
221 return NULL;
222}
223
224struct in_addr *
225dotted_to_addr(const char *dotted)
226{
227 static struct in_addr addr;
228 unsigned char *addrp;
229 char *p, *q;
230 int onebyte, i;
231 char buf[20];
232
233 /* copy dotted string, because we need to modify it */
234 strncpy(buf, dotted, sizeof(buf) - 1);
235 addrp = (unsigned char *) &(addr.s_addr);
236
237 p = buf;
238 for (i = 0; i < 3; i++) {
239 if ((q = strchr(p, '.')) == NULL)
240 return (struct in_addr *) NULL;
241
242 *q = '\0';
243 if ((onebyte = string_to_number(p, 0, 255)) == -1)
244 return (struct in_addr *) NULL;
245
246 addrp[i] = (unsigned char) onebyte;
247 p = q + 1;
248 }
249
250 /* we've checked 3 bytes, now we check the last one */
251 if ((onebyte = string_to_number(p, 0, 255)) == -1)
252 return (struct in_addr *) NULL;
253
254 addrp[3] = (unsigned char) onebyte;
255
256 return &addr;
257}
258
259static struct in_addr *
260network_to_addr(const char *name)
261{
262 struct netent *net;
263 static struct in_addr addr;
264
265 if ((net = getnetbyname(name)) != NULL) {
266 if (net->n_addrtype != AF_INET)
267 return (struct in_addr *) NULL;
268 addr.s_addr = htonl((unsigned long) net->n_net);
269 return &addr;
270 }
271
272 return (struct in_addr *) NULL;
273}
274
275static void
276inaddrcpy(struct in_addr *dst, struct in_addr *src)
277{
278 /* memcpy(dst, src, sizeof(struct in_addr)); */
279 dst->s_addr = src->s_addr;
280}
281
282void
283exit_error(enum exittype status, char *msg, ...)
284{
285 va_list args;
286
287 va_start(args, msg);
288 fprintf(stderr, "%s v%s: ", program_name, program_version);
289 vfprintf(stderr, msg, args);
290 va_end(args);
291 fprintf(stderr, "\n");
292 if (status == PARAMETER_PROBLEM)
293 exit_tryhelp(status);
294 if (status == VERSION_PROBLEM)
295 fprintf(stderr,
296 "Perhaps iptables or your kernel needs to be upgraded.\n");
297 exit(status);
298}
299
300void
301exit_tryhelp(int status)
302{
303 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
304 program_name, program_name );
305 exit(status);
306}
307
308void
309exit_printhelp(void)
310{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000311 struct iptables_match *m = NULL;
312 struct iptables_target *t = NULL;
313
Marc Bouchere6869a82000-03-20 06:03:29 +0000314 printf("%s v%s\n\n"
315"Usage: %s -[ADC] chain rule-specification [options]\n"
316" %s -[RI] chain rulenum rule-specification [options]\n"
317" %s -D chain rulenum [options]\n"
318" %s -[LFZ] [chain] [options]\n"
319" %s -[NX] chain\n"
320" %s -E old-chain-name new-chain-name\n"
321" %s -P chain target [options]\n"
322" %s -h (print this help information)\n\n",
323 program_name, program_version, program_name, program_name,
324 program_name, program_name, program_name, program_name,
325 program_name, program_name);
326
327 printf(
328"Commands:\n"
329"Either long or short options are allowed.\n"
330" --append -A chain Append to chain\n"
331" --delete -D chain Delete matching rule from chain\n"
332" --delete -D chain rulenum\n"
333" Delete rule rulenum (1 = first) from chain\n"
334" --insert -I chain [rulenum]\n"
335" Insert in chain as rulenum (default 1=first)\n"
336" --replace -R chain rulenum\n"
337" Replace rule rulenum (1 = first) in chain\n"
338" --list -L [chain] List the rules in a chain or all chains\n"
339" --flush -F [chain] Delete all rules in chain or all chains\n"
340" --zero -Z [chain] Zero counters in chain or all chains\n"
341" --check -C chain Test this packet on chain\n"
342" --new -N chain Create a new user-defined chain\n"
343" --delete-chain\n"
344" -X [chain] Delete a user-defined chain\n"
345" --policy -P chain target\n"
346" Change policy on chain to target\n"
347" --rename-chain\n"
348" -E old-chain new-chain\n"
349" Change chain name, (moving any references)\n"
350
351"Options:\n"
352" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
353" --source -s [!] address[/mask]\n"
354" source specification\n"
355" --destination -d [!] address[/mask]\n"
356" destination specification\n"
357" --in-interface -i [!] input name[+]\n"
358" network interface name ([+] for wildcard)\n"
359" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000360" target for rule (may load target extension)\n"
361" --match -m match\n"
362" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000363" --numeric -n numeric output of addresses and ports\n"
364" --out-interface -o [!] output name[+]\n"
365" network interface name ([+] for wildcard)\n"
366" --table -t table table to manipulate (default: `filter')\n"
367" --verbose -v verbose mode\n"
368" --exact -x expand numbers (display exact values)\n"
369"[!] --fragment -f match second or further fragments only\n"
370"[!] --version -V print package version.\n");
371
Rusty Russell363112d2000-08-11 13:49:26 +0000372 /* Print out any special helps. A user might like to be able
373 to add a --help to the commandline, and see expected
374 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000375 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000376 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000377 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000378 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000379 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000380 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000381 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000382 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000383 exit(0);
384}
385
386static void
387generic_opt_check(int command, int options)
388{
389 int i, j, legal = 0;
390
391 /* Check that commands are valid with options. Complicated by the
392 * fact that if an option is legal with *any* command given, it is
393 * legal overall (ie. -z and -l).
394 */
395 for (i = 0; i < NUMBER_OF_OPT; i++) {
396 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
397
398 for (j = 0; j < NUMBER_OF_CMD; j++) {
399 if (!(command & (1<<j)))
400 continue;
401
402 if (!(options & (1<<i))) {
403 if (commands_v_options[j][i] == '+')
404 exit_error(PARAMETER_PROBLEM,
405 "You need to supply the `-%c' "
406 "option for this command\n",
407 optflags[i]);
408 } else {
409 if (commands_v_options[j][i] != 'x')
410 legal = 1;
411 else if (legal == 0)
412 legal = -1;
413 }
414 }
415 if (legal == -1)
416 exit_error(PARAMETER_PROBLEM,
417 "Illegal option `-%c' with this command\n",
418 optflags[i]);
419 }
420}
421
422static char
423opt2char(int option)
424{
425 const char *ptr;
426 for (ptr = optflags; option > 1; option >>= 1, ptr++);
427
428 return *ptr;
429}
430
431static char
432cmd2char(int option)
433{
434 const char *ptr;
435 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
436
437 return *ptr;
438}
439
440static void
441add_command(int *cmd, const int newcmd, const int othercmds, int invert)
442{
443 if (invert)
444 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
445 if (*cmd & (~othercmds))
446 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
447 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
448 *cmd |= newcmd;
449}
450
451int
452check_inverse(const char option[], int *invert)
453{
454 if (option && strcmp(option, "!") == 0) {
455 if (*invert)
456 exit_error(PARAMETER_PROBLEM,
457 "Multiple `!' flags not allowed");
458
459 *invert = TRUE;
460 return TRUE;
461 }
462 return FALSE;
463}
464
465static void *
466fw_calloc(size_t count, size_t size)
467{
468 void *p;
469
470 if ((p = calloc(count, size)) == NULL) {
471 perror("iptables: calloc failed");
472 exit(1);
473 }
474 return p;
475}
476
477static void *
478fw_malloc(size_t size)
479{
480 void *p;
481
482 if ((p = malloc(size)) == NULL) {
483 perror("iptables: malloc failed");
484 exit(1);
485 }
486 return p;
487}
488
489static struct in_addr *
490host_to_addr(const char *name, unsigned int *naddr)
491{
492 struct hostent *host;
493 struct in_addr *addr;
494 unsigned int i;
495
496 *naddr = 0;
497 if ((host = gethostbyname(name)) != NULL) {
498 if (host->h_addrtype != AF_INET ||
499 host->h_length != sizeof(struct in_addr))
500 return (struct in_addr *) NULL;
501
502 while (host->h_addr_list[*naddr] != (char *) NULL)
503 (*naddr)++;
504 addr = fw_calloc(*naddr, sizeof(struct in_addr));
505 for (i = 0; i < *naddr; i++)
506 inaddrcpy(&(addr[i]),
507 (struct in_addr *) host->h_addr_list[i]);
508 return addr;
509 }
510
511 return (struct in_addr *) NULL;
512}
513
514static char *
515addr_to_host(const struct in_addr *addr)
516{
517 struct hostent *host;
518
519 if ((host = gethostbyaddr((char *) addr,
520 sizeof(struct in_addr), AF_INET)) != NULL)
521 return (char *) host->h_name;
522
523 return (char *) NULL;
524}
525
526/*
527 * All functions starting with "parse" should succeed, otherwise
528 * the program fails.
529 * Most routines return pointers to static data that may change
530 * between calls to the same or other routines with a few exceptions:
531 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
532 * return global static data.
533*/
534
535static struct in_addr *
536parse_hostnetwork(const char *name, unsigned int *naddrs)
537{
538 struct in_addr *addrp, *addrptmp;
539
540 if ((addrptmp = dotted_to_addr(name)) != NULL ||
541 (addrptmp = network_to_addr(name)) != NULL) {
542 addrp = fw_malloc(sizeof(struct in_addr));
543 inaddrcpy(addrp, addrptmp);
544 *naddrs = 1;
545 return addrp;
546 }
547 if ((addrp = host_to_addr(name, naddrs)) != NULL)
548 return addrp;
549
550 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
551}
552
553static struct in_addr *
554parse_mask(char *mask)
555{
556 static struct in_addr maskaddr;
557 struct in_addr *addrp;
558 int bits;
559
560 if (mask == NULL) {
561 /* no mask at all defaults to 32 bits */
562 maskaddr.s_addr = 0xFFFFFFFF;
563 return &maskaddr;
564 }
565 if ((addrp = dotted_to_addr(mask)) != NULL)
566 /* dotted_to_addr already returns a network byte order addr */
567 return addrp;
568 if ((bits = string_to_number(mask, 0, 32)) == -1)
569 exit_error(PARAMETER_PROBLEM,
570 "invalid mask `%s' specified", mask);
571 if (bits != 0) {
572 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
573 return &maskaddr;
574 }
575
576 maskaddr.s_addr = 0L;
577 return &maskaddr;
578}
579
580static void
581parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
582 struct in_addr *maskp, unsigned int *naddrs)
583{
584 struct in_addr *addrp;
585 char buf[256];
586 char *p;
587 int i, j, k, n;
588
589 strncpy(buf, name, sizeof(buf) - 1);
590 if ((p = strrchr(buf, '/')) != NULL) {
591 *p = '\0';
592 addrp = parse_mask(p + 1);
593 } else
594 addrp = parse_mask(NULL);
595 inaddrcpy(maskp, addrp);
596
597 /* if a null mask is given, the name is ignored, like in "any/0" */
598 if (maskp->s_addr == 0L)
599 strcpy(buf, "0.0.0.0");
600
601 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
602 n = *naddrs;
603 for (i = 0, j = 0; i < n; i++) {
604 addrp[j++].s_addr &= maskp->s_addr;
605 for (k = 0; k < j - 1; k++) {
606 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
607 (*naddrs)--;
608 j--;
609 break;
610 }
611 }
612 }
613}
614
615struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000616find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000617{
618 struct iptables_match *ptr;
619
620 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
621 if (strcmp(name, ptr->name) == 0)
622 break;
623 }
624
Rusty Russell52a51492000-05-02 16:44:29 +0000625 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000626 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
627 + strlen(name)];
628 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000629 if (dlopen(path, RTLD_NOW)) {
630 /* Found library. If it didn't register itself,
631 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000632 ptr = find_match(name, DONT_LOAD);
633
Rusty Russell9e1d2142000-04-23 09:11:12 +0000634 if (!ptr)
635 exit_error(PARAMETER_PROBLEM,
636 "Couldn't load match `%s'\n",
637 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000638 } else if (tryload == LOAD_MUST_SUCCEED)
639 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000640 "Couldn't load match `%s':%s\n",
641 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000642 }
643
644 return ptr;
645}
646
Rusty Russell28381a42000-05-10 00:19:50 +0000647/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
648static struct iptables_match *
649find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
650{
651 int proto;
652
653 proto = string_to_number(pname, 0, 255);
654 if (proto != -1)
655 return find_match(proto_to_name(proto, nolookup), tryload);
656
657 return find_match(pname, tryload);
658}
659
Marc Bouchere6869a82000-03-20 06:03:29 +0000660static u_int16_t
661parse_protocol(const char *s)
662{
Rusty Russell28381a42000-05-10 00:19:50 +0000663 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000664
665 if (proto == -1) {
666 struct protoent *pent;
667
668 if ((pent = getprotobyname(s)))
669 proto = pent->p_proto;
670 else {
671 unsigned int i;
672 for (i = 0;
673 i < sizeof(chain_protos)/sizeof(struct pprot);
674 i++) {
675 if (strcmp(s, chain_protos[i].name) == 0) {
676 proto = chain_protos[i].num;
677 break;
678 }
679 }
680 if (i == sizeof(chain_protos)/sizeof(struct pprot))
681 exit_error(PARAMETER_PROBLEM,
682 "unknown protocol `%s' specified",
683 s);
684 }
685 }
686
687 return (u_int16_t)proto;
688}
689
690static void
691parse_interface(const char *arg, char *vianame, unsigned char *mask)
692{
693 int vialen = strlen(arg);
694 unsigned int i;
695
696 memset(mask, 0, IFNAMSIZ);
697 memset(vianame, 0, IFNAMSIZ);
698
699 if (vialen + 1 > IFNAMSIZ)
700 exit_error(PARAMETER_PROBLEM,
701 "interface name `%s' must be shorter than IFNAMSIZ"
702 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000703
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 strcpy(vianame, arg);
705 if (vialen == 0)
706 memset(mask, 0, IFNAMSIZ);
707 else if (vianame[vialen - 1] == '+') {
708 memset(mask, 0xFF, vialen - 1);
709 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
710 /* Remove `+' */
711 vianame[vialen - 1] = '\0';
712 } else {
713 /* Include nul-terminator in match */
714 memset(mask, 0xFF, vialen + 1);
715 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
716 }
717 for (i = 0; vianame[i]; i++) {
718 if (!isalnum(vianame[i])) {
719 printf("Warning: wierd character in interface"
720 " `%s' (No aliases, :, ! or *).\n",
721 vianame);
722 break;
723 }
724 }
725}
726
727/* Can't be zero. */
728static int
729parse_rulenumber(const char *rule)
730{
731 int rulenum = string_to_number(rule, 1, INT_MAX);
732
733 if (rulenum == -1)
734 exit_error(PARAMETER_PROBLEM,
735 "Invalid rule number `%s'", rule);
736
737 return rulenum;
738}
739
740static const char *
741parse_target(const char *targetname)
742{
743 const char *ptr;
744
745 if (strlen(targetname) < 1)
746 exit_error(PARAMETER_PROBLEM,
747 "Invalid target name (too short)");
748
749 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
750 exit_error(PARAMETER_PROBLEM,
751 "Invalid target name `%s' (%i chars max)",
752 targetname, sizeof(ipt_chainlabel)-1);
753
754 for (ptr = targetname; *ptr; ptr++)
755 if (isspace(*ptr))
756 exit_error(PARAMETER_PROBLEM,
757 "Invalid target name `%s'", targetname);
758 return targetname;
759}
760
761static char *
762addr_to_network(const struct in_addr *addr)
763{
764 struct netent *net;
765
766 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
767 return (char *) net->n_name;
768
769 return (char *) NULL;
770}
771
772char *
773addr_to_dotted(const struct in_addr *addrp)
774{
775 static char buf[20];
776 const unsigned char *bytep;
777
778 bytep = (const unsigned char *) &(addrp->s_addr);
779 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
780 return buf;
781}
782static char *
783addr_to_anyname(const struct in_addr *addr)
784{
785 char *name;
786
787 if ((name = addr_to_host(addr)) != NULL ||
788 (name = addr_to_network(addr)) != NULL)
789 return name;
790
791 return addr_to_dotted(addr);
792}
793
794static char *
795mask_to_dotted(const struct in_addr *mask)
796{
797 int i;
798 static char buf[20];
799 u_int32_t maskaddr, bits;
800
801 maskaddr = ntohl(mask->s_addr);
802
803 if (maskaddr == 0xFFFFFFFFL)
804 /* we don't want to see "/32" */
805 return "";
806
807 i = 32;
808 bits = 0xFFFFFFFEL;
809 while (--i >= 0 && maskaddr != bits)
810 bits <<= 1;
811 if (i >= 0)
812 sprintf(buf, "/%d", i);
813 else
814 /* mask was not a decent combination of 1's and 0's */
815 sprintf(buf, "/%s", addr_to_dotted(mask));
816
817 return buf;
818}
819
820int
821string_to_number(const char *s, int min, int max)
822{
823 int number;
824 char *end;
825
826 /* Handle hex, octal, etc. */
827 number = (int)strtol(s, &end, 0);
828 if (*end == '\0' && end != s) {
829 /* we parsed a number, let's see if we want this */
830 if (min <= number && number <= max)
831 return number;
832 }
833 return -1;
834}
835
836static void
837set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
838 int invert)
839{
840 if (*options & option)
841 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
842 opt2char(option));
843 *options |= option;
844
845 if (invert) {
846 unsigned int i;
847 for (i = 0; 1 << i != option; i++);
848
849 if (!inverse_for_options[i])
850 exit_error(PARAMETER_PROBLEM,
851 "cannot have ! before -%c",
852 opt2char(option));
853 *invflg |= inverse_for_options[i];
854 }
855}
856
857struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000858find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000859{
860 struct iptables_target *ptr;
861
862 /* Standard target? */
863 if (strcmp(name, "") == 0
864 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
865 || strcmp(name, IPTC_LABEL_DROP) == 0
866 || strcmp(name, IPTC_LABEL_QUEUE) == 0
867 || strcmp(name, IPTC_LABEL_RETURN) == 0)
868 name = "standard";
869
870 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
871 if (strcmp(name, ptr->name) == 0)
872 break;
873 }
874
Rusty Russell52a51492000-05-02 16:44:29 +0000875 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000876 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
877 + strlen(name)];
878 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000879 if (dlopen(path, RTLD_NOW)) {
880 /* Found library. If it didn't register itself,
881 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000882 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000883 if (!ptr)
884 exit_error(PARAMETER_PROBLEM,
885 "Couldn't load target `%s'\n",
886 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000887 } else if (tryload == LOAD_MUST_SUCCEED)
888 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000889 "Couldn't load target `%s':%s\n",
890 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000891 }
892
893 return ptr;
894}
895
896static struct option *
897merge_options(struct option *oldopts, struct option *newopts,
898 unsigned int *option_offset)
899{
900 unsigned int num_old, num_new, i;
901 struct option *merge;
902
903 for (num_old = 0; oldopts[num_old].name; num_old++);
904 for (num_new = 0; newopts[num_new].name; num_new++);
905
906 global_option_offset += OPTION_OFFSET;
907 *option_offset = global_option_offset;
908
909 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
910 memcpy(merge, oldopts, num_old * sizeof(struct option));
911 for (i = 0; i < num_new; i++) {
912 merge[num_old + i] = newopts[i];
913 merge[num_old + i].val += *option_offset;
914 }
915 memset(merge + num_old + num_new, 0, sizeof(struct option));
916
917 return merge;
918}
919
920void
921register_match(struct iptables_match *me)
922{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000923 struct iptables_match **i;
924
Marc Bouchere6869a82000-03-20 06:03:29 +0000925 if (strcmp(me->version, program_version) != 0) {
926 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
927 program_name, me->name, me->version, program_version);
928 exit(1);
929 }
930
Rusty Russell52a51492000-05-02 16:44:29 +0000931 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000932 fprintf(stderr, "%s: match `%s' already registered.\n",
933 program_name, me->name);
934 exit(1);
935 }
936
Rusty Russell73f72f52000-07-03 10:17:57 +0000937 if (me->size != IPT_ALIGN(me->size)) {
938 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
939 program_name, me->name, me->size);
940 exit(1);
941 }
942
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000943 /* Append to list. */
944 for (i = &iptables_matches; *i; i = &(*i)->next);
945 me->next = NULL;
946 *i = me;
947
Marc Bouchere6869a82000-03-20 06:03:29 +0000948 me->m = NULL;
949 me->mflags = 0;
950
951 opts = merge_options(opts, me->extra_opts, &me->option_offset);
952}
953
954void
955register_target(struct iptables_target *me)
956{
957 if (strcmp(me->version, program_version) != 0) {
958 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
959 program_name, me->name, me->version, program_version);
960 exit(1);
961 }
962
Rusty Russell52a51492000-05-02 16:44:29 +0000963 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000964 fprintf(stderr, "%s: target `%s' already registered.\n",
965 program_name, me->name);
966 exit(1);
967 }
968
Rusty Russell73f72f52000-07-03 10:17:57 +0000969 if (me->size != IPT_ALIGN(me->size)) {
970 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
971 program_name, me->name, me->size);
972 exit(1);
973 }
974
Marc Bouchere6869a82000-03-20 06:03:29 +0000975 /* Prepend to list. */
976 me->next = iptables_targets;
977 iptables_targets = me;
978 me->t = NULL;
979 me->tflags = 0;
980
981 opts = merge_options(opts, me->extra_opts, &me->option_offset);
982}
983
984static void
985print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
986{
987 struct ipt_counters counters;
988 const char *pol = iptc_get_policy(chain, &counters, handle);
989 printf("Chain %s", chain);
990 if (pol) {
991 printf(" (policy %s", pol);
992 if (!(format & FMT_NOCOUNTS))
993 printf(" %llu packets, %llu bytes",
994 counters.pcnt, counters.bcnt);
995 printf(")\n");
996 } else {
997 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000998 if (!iptc_get_references(&refs, chain, handle))
999 printf(" (ERROR obtaining refs)\n");
1000 else
1001 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001002 }
1003
1004 if (format & FMT_LINENUMBERS)
1005 printf(FMT("%-4s ", "%s "), "num");
1006 if (!(format & FMT_NOCOUNTS)) {
1007 if (format & FMT_KILOMEGAGIGA) {
1008 printf(FMT("%5s ","%s "), "pkts");
1009 printf(FMT("%5s ","%s "), "bytes");
1010 } else {
1011 printf(FMT("%8s ","%s "), "pkts");
1012 printf(FMT("%10s ","%s "), "bytes");
1013 }
1014 }
1015 if (!(format & FMT_NOTARGET))
1016 printf(FMT("%-9s ","%s "), "target");
1017 fputs(" prot ", stdout);
1018 if (format & FMT_OPTIONS)
1019 fputs("opt", stdout);
1020 if (format & FMT_VIA) {
1021 printf(FMT(" %-6s ","%s "), "in");
1022 printf(FMT("%-6s ","%s "), "out");
1023 }
1024 printf(FMT(" %-19s ","%s "), "source");
1025 printf(FMT(" %-19s "," %s "), "destination");
1026 printf("\n");
1027}
1028
1029static void
1030print_num(u_int64_t number, unsigned int format)
1031{
1032 if (format & FMT_KILOMEGAGIGA) {
1033 if (number > 99999) {
1034 number = (number + 500) / 1000;
1035 if (number > 9999) {
1036 number = (number + 500) / 1000;
1037 if (number > 9999) {
1038 number = (number + 500) / 1000;
1039 printf(FMT("%4lluG ","%lluG "),number);
1040 }
1041 else printf(FMT("%4lluM ","%lluM "), number);
1042 } else
1043 printf(FMT("%4lluK ","%lluK "), number);
1044 } else
1045 printf(FMT("%5llu ","%llu "), number);
1046 } else
1047 printf(FMT("%8llu ","%llu "), number);
1048}
1049
1050static int
1051print_match(const struct ipt_entry_match *m,
1052 const struct ipt_ip *ip,
1053 int numeric)
1054{
Rusty Russell52a51492000-05-02 16:44:29 +00001055 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001056
1057 if (match) {
1058 if (match->print)
1059 match->print(ip, m, numeric);
1060 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001061 if (m->u.user.name[0])
1062 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001063 }
1064 /* Don't stop iterating. */
1065 return 0;
1066}
1067
1068/* e is called `fw' here for hysterical raisins */
1069static void
1070print_firewall(const struct ipt_entry *fw,
1071 const char *targname,
1072 unsigned int num,
1073 unsigned int format,
1074 const iptc_handle_t handle)
1075{
1076 struct iptables_target *target = NULL;
1077 const struct ipt_entry_target *t;
1078 u_int8_t flags;
1079 char buf[BUFSIZ];
1080
1081 /* User creates a chain called "REJECT": this overrides the
1082 `REJECT' target module. Keep feeding them rope until the
1083 revolution... Bwahahahahah */
1084 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001085 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001086 else
Rusty Russell52a51492000-05-02 16:44:29 +00001087 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001088
1089 t = ipt_get_target((struct ipt_entry *)fw);
1090 flags = fw->ip.flags;
1091
1092 if (format & FMT_LINENUMBERS)
1093 printf(FMT("%-4u ", "%u "), num+1);
1094
1095 if (!(format & FMT_NOCOUNTS)) {
1096 print_num(fw->counters.pcnt, format);
1097 print_num(fw->counters.bcnt, format);
1098 }
1099
1100 if (!(format & FMT_NOTARGET))
1101 printf(FMT("%-9s ", "%s "), targname);
1102
1103 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1104 {
Rusty Russell28381a42000-05-10 00:19:50 +00001105 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001106 if (pname)
1107 printf(FMT("%-5s", "%s "), pname);
1108 else
1109 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1110 }
1111
1112 if (format & FMT_OPTIONS) {
1113 if (format & FMT_NOTABLE)
1114 fputs("opt ", stdout);
1115 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1116 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1117 fputc(' ', stdout);
1118 }
1119
1120 if (format & FMT_VIA) {
1121 char iface[IFNAMSIZ+2];
1122
1123 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1124 iface[0] = '!';
1125 iface[1] = '\0';
1126 }
1127 else iface[0] = '\0';
1128
1129 if (fw->ip.iniface[0] != '\0') {
1130 strcat(iface, fw->ip.iniface);
1131 /* If it doesn't compare the nul-term, it's a
1132 wildcard. */
1133 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1134 strcat(iface, "+");
1135 }
1136 else if (format & FMT_NUMERIC) strcat(iface, "*");
1137 else strcat(iface, "any");
1138 printf(FMT(" %-6s ","in %s "), iface);
1139
1140 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1141 iface[0] = '!';
1142 iface[1] = '\0';
1143 }
1144 else iface[0] = '\0';
1145
1146 if (fw->ip.outiface[0] != '\0') {
1147 strcat(iface, fw->ip.outiface);
1148 /* If it doesn't compare the nul-term, it's a
1149 wildcard. */
1150 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1151 strcat(iface, "+");
1152 }
1153 else if (format & FMT_NUMERIC) strcat(iface, "*");
1154 else strcat(iface, "any");
1155 printf(FMT("%-6s ","out %s "), iface);
1156 }
1157
1158 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1159 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1160 printf(FMT("%-19s ","%s "), "anywhere");
1161 else {
1162 if (format & FMT_NUMERIC)
1163 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1164 else
1165 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1166 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1167 printf(FMT("%-19s ","%s "), buf);
1168 }
1169
1170 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1171 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1172 printf(FMT("%-19s","-> %s"), "anywhere");
1173 else {
1174 if (format & FMT_NUMERIC)
1175 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1176 else
1177 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1178 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1179 printf(FMT("%-19s","-> %s"), buf);
1180 }
1181
1182 if (format & FMT_NOTABLE)
1183 fputs(" ", stdout);
1184
1185 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1186
1187 if (target) {
1188 if (target->print)
1189 /* Print the target information. */
1190 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001191 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001193 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001194
1195 if (!(format & FMT_NONEWLINE))
1196 fputc('\n', stdout);
1197}
1198
1199static void
1200print_firewall_line(const struct ipt_entry *fw,
1201 const iptc_handle_t h)
1202{
1203 struct ipt_entry_target *t;
1204
1205 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001206 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001207}
1208
1209static int
1210append_entry(const ipt_chainlabel chain,
1211 struct ipt_entry *fw,
1212 unsigned int nsaddrs,
1213 const struct in_addr saddrs[],
1214 unsigned int ndaddrs,
1215 const struct in_addr daddrs[],
1216 int verbose,
1217 iptc_handle_t *handle)
1218{
1219 unsigned int i, j;
1220 int ret = 1;
1221
1222 for (i = 0; i < nsaddrs; i++) {
1223 fw->ip.src.s_addr = saddrs[i].s_addr;
1224 for (j = 0; j < ndaddrs; j++) {
1225 fw->ip.dst.s_addr = daddrs[j].s_addr;
1226 if (verbose)
1227 print_firewall_line(fw, *handle);
1228 ret &= iptc_append_entry(chain, fw, handle);
1229 }
1230 }
1231
1232 return ret;
1233}
1234
1235static int
1236replace_entry(const ipt_chainlabel chain,
1237 struct ipt_entry *fw,
1238 unsigned int rulenum,
1239 const struct in_addr *saddr,
1240 const struct in_addr *daddr,
1241 int verbose,
1242 iptc_handle_t *handle)
1243{
1244 fw->ip.src.s_addr = saddr->s_addr;
1245 fw->ip.dst.s_addr = daddr->s_addr;
1246
1247 if (verbose)
1248 print_firewall_line(fw, *handle);
1249 return iptc_replace_entry(chain, fw, rulenum, handle);
1250}
1251
1252static int
1253insert_entry(const ipt_chainlabel chain,
1254 struct ipt_entry *fw,
1255 unsigned int rulenum,
1256 unsigned int nsaddrs,
1257 const struct in_addr saddrs[],
1258 unsigned int ndaddrs,
1259 const struct in_addr daddrs[],
1260 int verbose,
1261 iptc_handle_t *handle)
1262{
1263 unsigned int i, j;
1264 int ret = 1;
1265
1266 for (i = 0; i < nsaddrs; i++) {
1267 fw->ip.src.s_addr = saddrs[i].s_addr;
1268 for (j = 0; j < ndaddrs; j++) {
1269 fw->ip.dst.s_addr = daddrs[j].s_addr;
1270 if (verbose)
1271 print_firewall_line(fw, *handle);
1272 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1273 }
1274 }
1275
1276 return ret;
1277}
1278
Rusty Russell2e0a3212000-04-19 11:23:18 +00001279static unsigned char *
1280make_delete_mask(struct ipt_entry *fw)
1281{
1282 /* Establish mask for comparison */
1283 unsigned int size;
1284 struct iptables_match *m;
1285 unsigned char *mask, *mptr;
1286
1287 size = sizeof(struct ipt_entry);
1288 for (m = iptables_matches; m; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001289 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001290
Rusty Russell9e1d2142000-04-23 09:11:12 +00001291 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001292 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001293 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001294
Rusty Russell9e1d2142000-04-23 09:11:12 +00001295 memset(mask, 0xFF, sizeof(struct ipt_entry));
1296 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001297
1298 for (m = iptables_matches; m; m = m->next) {
1299 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001300 IPT_ALIGN(sizeof(struct ipt_entry_match))
1301 + m->userspacesize);
1302 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001303 }
1304
Rusty Russell73f72f52000-07-03 10:17:57 +00001305 memset(mptr, 0xFF,
1306 IPT_ALIGN(sizeof(struct ipt_entry_target))
1307 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001308
1309 return mask;
1310}
1311
Marc Bouchere6869a82000-03-20 06:03:29 +00001312static int
1313delete_entry(const ipt_chainlabel chain,
1314 struct ipt_entry *fw,
1315 unsigned int nsaddrs,
1316 const struct in_addr saddrs[],
1317 unsigned int ndaddrs,
1318 const struct in_addr daddrs[],
1319 int verbose,
1320 iptc_handle_t *handle)
1321{
1322 unsigned int i, j;
1323 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001324 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001325
Rusty Russell2e0a3212000-04-19 11:23:18 +00001326 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 for (i = 0; i < nsaddrs; i++) {
1328 fw->ip.src.s_addr = saddrs[i].s_addr;
1329 for (j = 0; j < ndaddrs; j++) {
1330 fw->ip.dst.s_addr = daddrs[j].s_addr;
1331 if (verbose)
1332 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001333 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001334 }
1335 }
1336 return ret;
1337}
1338
1339static int
1340check_packet(const ipt_chainlabel chain,
1341 struct ipt_entry *fw,
1342 unsigned int nsaddrs,
1343 const struct in_addr saddrs[],
1344 unsigned int ndaddrs,
1345 const struct in_addr daddrs[],
1346 int verbose,
1347 iptc_handle_t *handle)
1348{
1349 int ret = 1;
1350 unsigned int i, j;
1351 const char *msg;
1352
1353 for (i = 0; i < nsaddrs; i++) {
1354 fw->ip.src.s_addr = saddrs[i].s_addr;
1355 for (j = 0; j < ndaddrs; j++) {
1356 fw->ip.dst.s_addr = daddrs[j].s_addr;
1357 if (verbose)
1358 print_firewall_line(fw, *handle);
1359 msg = iptc_check_packet(chain, fw, handle);
1360 if (!msg) ret = 0;
1361 else printf("%s\n", msg);
1362 }
1363 }
1364
1365 return ret;
1366}
1367
1368static int
1369for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001370 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001371{
1372 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001373 const char *chain;
1374 char *chains;
1375 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001376
Rusty Russell9e1d2142000-04-23 09:11:12 +00001377 chain = iptc_first_chain(handle);
1378 while (chain) {
1379 chaincount++;
1380 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001381 }
1382
Rusty Russell9e1d2142000-04-23 09:11:12 +00001383 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1384 i = 0;
1385 chain = iptc_first_chain(handle);
1386 while (chain) {
1387 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1388 i++;
1389 chain = iptc_next_chain(handle);
1390 }
1391
1392 for (i = 0; i < chaincount; i++) {
1393 if (!builtinstoo
1394 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1395 *handle))
1396 continue;
1397 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1398 }
1399
1400 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001401 return ret;
1402}
1403
1404static int
1405flush_entries(const ipt_chainlabel chain, int verbose,
1406 iptc_handle_t *handle)
1407{
1408 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001409 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001410
1411 if (verbose)
1412 fprintf(stdout, "Flushing chain `%s'\n", chain);
1413 return iptc_flush_entries(chain, handle);
1414}
Marc Bouchere6869a82000-03-20 06:03:29 +00001415
1416static int
1417zero_entries(const ipt_chainlabel chain, int verbose,
1418 iptc_handle_t *handle)
1419{
1420 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001421 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001422
Marc Bouchere6869a82000-03-20 06:03:29 +00001423 if (verbose)
1424 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1425 return iptc_zero_entries(chain, handle);
1426}
1427
1428static int
1429delete_chain(const ipt_chainlabel chain, int verbose,
1430 iptc_handle_t *handle)
1431{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001432 if (!chain)
1433 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001434
1435 if (verbose)
1436 fprintf(stdout, "Deleting chain `%s'\n", chain);
1437 return iptc_delete_chain(chain, handle);
1438}
1439
1440static int
1441list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1442 int expanded, int linenumbers, iptc_handle_t *handle)
1443{
1444 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001445 unsigned int format;
1446 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001447
1448 format = FMT_OPTIONS;
1449 if (!verbose)
1450 format |= FMT_NOCOUNTS;
1451 else
1452 format |= FMT_VIA;
1453
1454 if (numeric)
1455 format |= FMT_NUMERIC;
1456
1457 if (!expanded)
1458 format |= FMT_KILOMEGAGIGA;
1459
1460 if (linenumbers)
1461 format |= FMT_LINENUMBERS;
1462
Rusty Russell9e1d2142000-04-23 09:11:12 +00001463 for (this = iptc_first_chain(handle);
1464 this;
1465 this = iptc_next_chain(handle)) {
1466 const struct ipt_entry *i;
1467 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001468
Marc Bouchere6869a82000-03-20 06:03:29 +00001469 if (chain && strcmp(chain, this) != 0)
1470 continue;
1471
1472 if (found) printf("\n");
1473
1474 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001475 i = iptc_first_rule(this, handle);
1476
1477 num = 0;
1478 while (i) {
1479 print_firewall(i,
1480 iptc_get_target(i, handle),
1481 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 format,
1483 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001484 i = iptc_next_rule(i, handle);
1485 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001486 found = 1;
1487 }
1488
1489 errno = ENOENT;
1490 return found;
1491}
1492
1493static struct ipt_entry *
1494generate_entry(const struct ipt_entry *fw,
1495 struct iptables_match *matches,
1496 struct ipt_entry_target *target)
1497{
1498 unsigned int size;
1499 struct iptables_match *m;
1500 struct ipt_entry *e;
1501
1502 size = sizeof(struct ipt_entry);
1503 for (m = matches; m; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001504 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001505
Rusty Russell228e98d2000-04-27 10:28:06 +00001506 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001507 *e = *fw;
1508 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001509 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001510
1511 size = 0;
1512 for (m = matches; m; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001513 memcpy(e->elems + size, m->m, m->m->u.match_size);
1514 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001515 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001516 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001517
1518 return e;
1519}
1520
1521int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1522{
1523 struct ipt_entry fw, *e = NULL;
1524 int invert = 0;
1525 unsigned int nsaddrs = 0, ndaddrs = 0;
1526 struct in_addr *saddrs = NULL, *daddrs = NULL;
1527
1528 int c, verbose = 0;
1529 const char *chain = NULL;
1530 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1531 const char *policy = NULL, *newname = NULL;
1532 unsigned int rulenum = 0, options = 0, command = 0;
1533 int ret = 1;
1534 struct iptables_match *m;
1535 struct iptables_target *target = NULL;
1536 const char *jumpto = "";
1537 char *protocol = NULL;
1538
1539 memset(&fw, 0, sizeof(fw));
1540
1541 /* Suppress error messages: we may add new options if we
1542 demand-load a protocol. */
1543 opterr = 0;
1544
1545 while ((c = getopt_long(argc, argv,
1546 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1547 opts, NULL)) != -1) {
1548 switch (c) {
1549 /*
1550 * Command selection
1551 */
1552 case 'A':
1553 add_command(&command, CMD_APPEND, CMD_NONE,
1554 invert);
1555 chain = optarg;
1556 break;
1557
1558 case 'D':
1559 add_command(&command, CMD_DELETE, CMD_NONE,
1560 invert);
1561 chain = optarg;
1562 if (optind < argc && argv[optind][0] != '-'
1563 && argv[optind][0] != '!') {
1564 rulenum = parse_rulenumber(argv[optind++]);
1565 command = CMD_DELETE_NUM;
1566 }
1567 break;
1568
1569 case 'C':
1570 add_command(&command, CMD_CHECK, CMD_NONE,
1571 invert);
1572 chain = optarg;
1573 break;
1574
1575 case 'R':
1576 add_command(&command, CMD_REPLACE, CMD_NONE,
1577 invert);
1578 chain = optarg;
1579 if (optind < argc && argv[optind][0] != '-'
1580 && argv[optind][0] != '!')
1581 rulenum = parse_rulenumber(argv[optind++]);
1582 else
1583 exit_error(PARAMETER_PROBLEM,
1584 "-%c requires a rule number",
1585 cmd2char(CMD_REPLACE));
1586 break;
1587
1588 case 'I':
1589 add_command(&command, CMD_INSERT, CMD_NONE,
1590 invert);
1591 chain = optarg;
1592 if (optind < argc && argv[optind][0] != '-'
1593 && argv[optind][0] != '!')
1594 rulenum = parse_rulenumber(argv[optind++]);
1595 else rulenum = 1;
1596 break;
1597
1598 case 'L':
1599 add_command(&command, CMD_LIST, CMD_ZERO,
1600 invert);
1601 if (optarg) chain = optarg;
1602 else if (optind < argc && argv[optind][0] != '-'
1603 && argv[optind][0] != '!')
1604 chain = argv[optind++];
1605 break;
1606
1607 case 'F':
1608 add_command(&command, CMD_FLUSH, CMD_NONE,
1609 invert);
1610 if (optarg) chain = optarg;
1611 else if (optind < argc && argv[optind][0] != '-'
1612 && argv[optind][0] != '!')
1613 chain = argv[optind++];
1614 break;
1615
1616 case 'Z':
1617 add_command(&command, CMD_ZERO, CMD_LIST,
1618 invert);
1619 if (optarg) chain = optarg;
1620 else if (optind < argc && argv[optind][0] != '-'
1621 && argv[optind][0] != '!')
1622 chain = argv[optind++];
1623 break;
1624
1625 case 'N':
1626 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1627 invert);
1628 chain = optarg;
1629 break;
1630
1631 case 'X':
1632 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1633 invert);
1634 if (optarg) chain = optarg;
1635 else if (optind < argc && argv[optind][0] != '-'
1636 && argv[optind][0] != '!')
1637 chain = argv[optind++];
1638 break;
1639
1640 case 'E':
1641 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1642 invert);
1643 chain = optarg;
1644 if (optind < argc && argv[optind][0] != '-'
1645 && argv[optind][0] != '!')
1646 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001647 else
1648 exit_error(PARAMETER_PROBLEM,
1649 "-%c requires old-chain-name and "
1650 "new-chain-name",
1651 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 break;
1653
1654 case 'P':
1655 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1656 invert);
1657 chain = optarg;
1658 if (optind < argc && argv[optind][0] != '-'
1659 && argv[optind][0] != '!')
1660 policy = argv[optind++];
1661 else
1662 exit_error(PARAMETER_PROBLEM,
1663 "-%c requires a chain and a policy",
1664 cmd2char(CMD_SET_POLICY));
1665 break;
1666
1667 case 'h':
1668 if (!optarg)
1669 optarg = argv[optind];
1670
Rusty Russell2e0a3212000-04-19 11:23:18 +00001671 /* iptables -p icmp -h */
1672 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001673 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001674
Marc Bouchere6869a82000-03-20 06:03:29 +00001675 exit_printhelp();
1676
1677 /*
1678 * Option selection
1679 */
1680 case 'p':
1681 if (check_inverse(optarg, &invert))
1682 optind++;
1683 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1684 invert);
1685
1686 /* Canonicalize into lower case */
1687 for (protocol = argv[optind-1]; *protocol; protocol++)
1688 *protocol = tolower(*protocol);
1689
1690 protocol = argv[optind-1];
1691 fw.ip.proto = parse_protocol(protocol);
1692
1693 if (fw.ip.proto == 0
1694 && (fw.ip.invflags & IPT_INV_PROTO))
1695 exit_error(PARAMETER_PROBLEM,
1696 "rule would never match protocol");
1697 fw.nfcache |= NFC_IP_PROTO;
1698 break;
1699
1700 case 's':
1701 if (check_inverse(optarg, &invert))
1702 optind++;
1703 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1704 invert);
1705 shostnetworkmask = argv[optind-1];
1706 fw.nfcache |= NFC_IP_SRC;
1707 break;
1708
1709 case 'd':
1710 if (check_inverse(optarg, &invert))
1711 optind++;
1712 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1713 invert);
1714 dhostnetworkmask = argv[optind-1];
1715 fw.nfcache |= NFC_IP_DST;
1716 break;
1717
1718 case 'j':
1719 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1720 invert);
1721 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001722 /* TRY_LOAD (may be chain name) */
1723 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001724
1725 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001726 size_t size;
1727
Rusty Russell73f72f52000-07-03 10:17:57 +00001728 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1729 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001730
Rusty Russell2e0a3212000-04-19 11:23:18 +00001731 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001732 target->t->u.target_size = size;
1733 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001734 target->init(target->t, &fw.nfcache);
1735 }
1736 break;
1737
1738
1739 case 'i':
1740 if (check_inverse(optarg, &invert))
1741 optind++;
1742 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1743 invert);
1744 parse_interface(argv[optind-1],
1745 fw.ip.iniface,
1746 fw.ip.iniface_mask);
1747 fw.nfcache |= NFC_IP_IF_IN;
1748 break;
1749
1750 case 'o':
1751 if (check_inverse(optarg, &invert))
1752 optind++;
1753 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1754 invert);
1755 parse_interface(argv[optind-1],
1756 fw.ip.outiface,
1757 fw.ip.outiface_mask);
1758 fw.nfcache |= NFC_IP_IF_OUT;
1759 break;
1760
1761 case 'f':
1762 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1763 invert);
1764 fw.ip.flags |= IPT_F_FRAG;
1765 fw.nfcache |= NFC_IP_FRAG;
1766 break;
1767
1768 case 'v':
1769 if (!verbose)
1770 set_option(&options, OPT_VERBOSE,
1771 &fw.ip.invflags, invert);
1772 verbose++;
1773 break;
1774
Rusty Russell52a51492000-05-02 16:44:29 +00001775 case 'm': {
1776 size_t size;
1777
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 if (invert)
1779 exit_error(PARAMETER_PROBLEM,
1780 "unexpected ! flag before --match");
1781
Rusty Russell52a51492000-05-02 16:44:29 +00001782 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001783 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1784 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001785 m->m = fw_calloc(1, size);
1786 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001787 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001788 m->init(m->m, &fw.nfcache);
1789 }
1790 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001791
1792 case 'n':
1793 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1794 invert);
1795 break;
1796
1797 case 't':
1798 if (invert)
1799 exit_error(PARAMETER_PROBLEM,
1800 "unexpected ! flag before --table");
1801 *table = argv[optind-1];
1802 break;
1803
1804 case 'x':
1805 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1806 invert);
1807 break;
1808
1809 case 'V':
1810 if (invert)
1811 printf("Not %s ;-)\n", program_version);
1812 else
1813 printf("%s v%s\n",
1814 program_name, program_version);
1815 exit(0);
1816
1817 case '0':
1818 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1819 invert);
1820 break;
1821
1822 case 1: /* non option */
1823 if (optarg[0] == '!' && optarg[1] == '\0') {
1824 if (invert)
1825 exit_error(PARAMETER_PROBLEM,
1826 "multiple consecutive ! not"
1827 " allowed");
1828 invert = TRUE;
1829 optarg[0] = '\0';
1830 continue;
1831 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001832 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001833 exit_tryhelp(2);
1834
1835 default:
1836 /* FIXME: This scheme doesn't allow two of the same
1837 matches --RR */
1838 if (!target
1839 || !(target->parse(c - target->option_offset,
1840 argv, invert,
1841 &target->tflags,
1842 &fw, &target->t))) {
1843 for (m = iptables_matches; m; m = m->next) {
1844 if (m->parse(c - m->option_offset,
1845 argv, invert,
1846 &m->mflags,
1847 &fw,
1848 &fw.nfcache,
1849 &m->m))
1850 break;
1851 }
1852
1853 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001854 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001855 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001857 && !find_proto(protocol, DONT_LOAD,
1858 options&OPT_NUMERIC)
1859 && (m = find_proto(protocol, TRY_LOAD,
1860 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001861 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001862 size_t size;
1863
Rusty Russell73f72f52000-07-03 10:17:57 +00001864 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1865 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Rusty Russell2e0a3212000-04-19 11:23:18 +00001867 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001868 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001869 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001870 m->init(m->m, &fw.nfcache);
1871
1872 optind--;
1873 continue;
1874 }
1875 if (!m)
1876 exit_error(PARAMETER_PROBLEM,
1877 "Unknown arg `%s'",
1878 argv[optind-1]);
1879 }
1880 }
1881 invert = FALSE;
1882 }
1883
1884 for (m = iptables_matches; m; m = m->next)
1885 m->final_check(m->mflags);
1886 if (target)
1887 target->final_check(target->tflags);
1888
1889 /* Fix me: must put inverse options checking here --MN */
1890
1891 if (optind < argc)
1892 exit_error(PARAMETER_PROBLEM,
1893 "unknown arguments found on commandline");
1894 if (!command)
1895 exit_error(PARAMETER_PROBLEM, "no command specified");
1896 if (invert)
1897 exit_error(PARAMETER_PROBLEM,
1898 "nothing appropriate following !");
1899
Marc Boucher744bd022000-04-22 22:36:10 +00001900 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1901 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001902 if (!(options & OPT_DESTINATION))
1903 dhostnetworkmask = "0.0.0.0/0";
1904 if (!(options & OPT_SOURCE))
1905 shostnetworkmask = "0.0.0.0/0";
1906 }
1907
1908 if (shostnetworkmask)
1909 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1910 &(fw.ip.smsk), &nsaddrs);
1911
1912 if (dhostnetworkmask)
1913 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1914 &(fw.ip.dmsk), &ndaddrs);
1915
1916 if ((nsaddrs > 1 || ndaddrs > 1) &&
1917 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1918 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1919 " source or destination IP addresses");
1920
1921 if (command == CMD_CHECK && fw.ip.invflags != 0)
1922 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1923 cmd2char(CMD_CHECK));
1924
1925 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1926 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1927 "specify a unique address");
1928
1929 generic_opt_check(command, options);
1930
1931 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1932 exit_error(PARAMETER_PROBLEM,
1933 "chain name `%s' too long (must be under %i chars)",
1934 chain, IPT_FUNCTION_MAXNAMELEN);
1935
1936 *handle = iptc_init(*table);
1937 if (!*handle)
1938 exit_error(VERSION_PROBLEM,
1939 "can't initialize iptables table `%s': %s",
1940 *table, iptc_strerror(errno));
1941
Marc Boucher744bd022000-04-22 22:36:10 +00001942 if (command == CMD_CHECK
1943 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001944 || command == CMD_DELETE
1945 || command == CMD_INSERT
1946 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001947 if (strcmp(chain, "PREROUTING") == 0
1948 || strcmp(chain, "INPUT") == 0) {
1949 /* -o not valid with incoming packets. */
1950 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001951 exit_error(PARAMETER_PROBLEM,
1952 "Can't use -%c with %s\n",
1953 opt2char(OPT_VIANAMEOUT),
1954 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001955 /* -i required with -C */
1956 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1957 exit_error(PARAMETER_PROBLEM,
1958 "Need -%c with %s\n",
1959 opt2char(OPT_VIANAMEIN),
1960 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001961 }
1962
Rusty Russella4860fd2000-06-17 16:13:02 +00001963 if (strcmp(chain, "POSTROUTING") == 0
1964 || strcmp(chain, "OUTPUT") == 0) {
1965 /* -i not valid with outgoing packets */
1966 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001967 exit_error(PARAMETER_PROBLEM,
1968 "Can't use -%c with %s\n",
1969 opt2char(OPT_VIANAMEIN),
1970 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001971 /* -o required with -C */
1972 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1973 exit_error(PARAMETER_PROBLEM,
1974 "Need -%c with %s\n",
1975 opt2char(OPT_VIANAMEOUT),
1976 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001977 }
1978
1979 if (target && iptc_is_chain(jumpto, *handle)) {
1980 printf("Warning: using chain %s, not extension\n",
1981 jumpto);
1982
1983 target = NULL;
1984 }
1985
1986 /* If they didn't specify a target, or it's a chain
1987 name, use standard. */
1988 if (!target
1989 && (strlen(jumpto) == 0
1990 || iptc_is_chain(jumpto, *handle))) {
1991 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001992
Rusty Russell52a51492000-05-02 16:44:29 +00001993 target = find_target(IPT_STANDARD_TARGET,
1994 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001995
1996 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001997 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001998 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001999 target->t->u.target_size = size;
2000 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002001 target->init(target->t, &fw.nfcache);
2002 }
2003
Rusty Russell7e53bf92000-03-20 07:03:28 +00002004 if (!target) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002005 struct ipt_entry_target unknown_target;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002006
Marc Bouchere6869a82000-03-20 06:03:29 +00002007 /* Don't know it. Must be extension with no
2008 options? */
Rusty Russell228e98d2000-04-27 10:28:06 +00002009 unknown_target.u.target_size = sizeof(unknown_target);
2010 strcpy(unknown_target.u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002011
2012 e = generate_entry(&fw, iptables_matches,
2013 &unknown_target);
2014 } else {
2015 e = generate_entry(&fw, iptables_matches, target->t);
2016 }
2017 }
2018
2019 switch (command) {
2020 case CMD_APPEND:
2021 ret = append_entry(chain, e,
2022 nsaddrs, saddrs, ndaddrs, daddrs,
2023 options&OPT_VERBOSE,
2024 handle);
2025 break;
2026 case CMD_CHECK:
2027 ret = check_packet(chain, e,
2028 nsaddrs, saddrs, ndaddrs, daddrs,
2029 options&OPT_VERBOSE, handle);
2030 break;
2031 case CMD_DELETE:
2032 ret = delete_entry(chain, e,
2033 nsaddrs, saddrs, ndaddrs, daddrs,
2034 options&OPT_VERBOSE,
2035 handle);
2036 break;
2037 case CMD_DELETE_NUM:
2038 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2039 break;
2040 case CMD_REPLACE:
2041 ret = replace_entry(chain, e, rulenum - 1,
2042 saddrs, daddrs, options&OPT_VERBOSE,
2043 handle);
2044 break;
2045 case CMD_INSERT:
2046 ret = insert_entry(chain, e, rulenum - 1,
2047 nsaddrs, saddrs, ndaddrs, daddrs,
2048 options&OPT_VERBOSE,
2049 handle);
2050 break;
2051 case CMD_LIST:
2052 ret = list_entries(chain,
2053 options&OPT_VERBOSE,
2054 options&OPT_NUMERIC,
2055 options&OPT_EXPANDED,
2056 options&OPT_LINENUMBERS,
2057 handle);
2058 break;
2059 case CMD_FLUSH:
2060 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2061 break;
2062 case CMD_ZERO:
2063 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2064 break;
2065 case CMD_LIST|CMD_ZERO:
2066 ret = list_entries(chain,
2067 options&OPT_VERBOSE,
2068 options&OPT_NUMERIC,
2069 options&OPT_EXPANDED,
2070 options&OPT_LINENUMBERS,
2071 handle);
2072 if (ret)
2073 ret = zero_entries(chain,
2074 options&OPT_VERBOSE, handle);
2075 break;
2076 case CMD_NEW_CHAIN:
2077 ret = iptc_create_chain(chain, handle);
2078 break;
2079 case CMD_DELETE_CHAIN:
2080 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2081 break;
2082 case CMD_RENAME_CHAIN:
2083 ret = iptc_rename_chain(chain, newname, handle);
2084 break;
2085 case CMD_SET_POLICY:
2086 ret = iptc_set_policy(chain, policy, handle);
2087 break;
2088 default:
2089 /* We should never reach this... */
2090 exit_tryhelp(2);
2091 }
2092
2093 if (verbose > 1)
2094 dump_entries(*handle);
2095
2096 return ret;
2097}