blob: d974814ca33945c53f0cfc011544281fdcb92b5f [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,
640 "Couldn't load match `%s'\n", name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000641 }
642
643 return ptr;
644}
645
Rusty Russell28381a42000-05-10 00:19:50 +0000646/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
647static struct iptables_match *
648find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
649{
650 int proto;
651
652 proto = string_to_number(pname, 0, 255);
653 if (proto != -1)
654 return find_match(proto_to_name(proto, nolookup), tryload);
655
656 return find_match(pname, tryload);
657}
658
Marc Bouchere6869a82000-03-20 06:03:29 +0000659static u_int16_t
660parse_protocol(const char *s)
661{
Rusty Russell28381a42000-05-10 00:19:50 +0000662 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000663
664 if (proto == -1) {
665 struct protoent *pent;
666
667 if ((pent = getprotobyname(s)))
668 proto = pent->p_proto;
669 else {
670 unsigned int i;
671 for (i = 0;
672 i < sizeof(chain_protos)/sizeof(struct pprot);
673 i++) {
674 if (strcmp(s, chain_protos[i].name) == 0) {
675 proto = chain_protos[i].num;
676 break;
677 }
678 }
679 if (i == sizeof(chain_protos)/sizeof(struct pprot))
680 exit_error(PARAMETER_PROBLEM,
681 "unknown protocol `%s' specified",
682 s);
683 }
684 }
685
686 return (u_int16_t)proto;
687}
688
689static void
690parse_interface(const char *arg, char *vianame, unsigned char *mask)
691{
692 int vialen = strlen(arg);
693 unsigned int i;
694
695 memset(mask, 0, IFNAMSIZ);
696 memset(vianame, 0, IFNAMSIZ);
697
698 if (vialen + 1 > IFNAMSIZ)
699 exit_error(PARAMETER_PROBLEM,
700 "interface name `%s' must be shorter than IFNAMSIZ"
701 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000702
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 strcpy(vianame, arg);
704 if (vialen == 0)
705 memset(mask, 0, IFNAMSIZ);
706 else if (vianame[vialen - 1] == '+') {
707 memset(mask, 0xFF, vialen - 1);
708 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
709 /* Remove `+' */
710 vianame[vialen - 1] = '\0';
711 } else {
712 /* Include nul-terminator in match */
713 memset(mask, 0xFF, vialen + 1);
714 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
715 }
716 for (i = 0; vianame[i]; i++) {
717 if (!isalnum(vianame[i])) {
718 printf("Warning: wierd character in interface"
719 " `%s' (No aliases, :, ! or *).\n",
720 vianame);
721 break;
722 }
723 }
724}
725
726/* Can't be zero. */
727static int
728parse_rulenumber(const char *rule)
729{
730 int rulenum = string_to_number(rule, 1, INT_MAX);
731
732 if (rulenum == -1)
733 exit_error(PARAMETER_PROBLEM,
734 "Invalid rule number `%s'", rule);
735
736 return rulenum;
737}
738
739static const char *
740parse_target(const char *targetname)
741{
742 const char *ptr;
743
744 if (strlen(targetname) < 1)
745 exit_error(PARAMETER_PROBLEM,
746 "Invalid target name (too short)");
747
748 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
749 exit_error(PARAMETER_PROBLEM,
750 "Invalid target name `%s' (%i chars max)",
751 targetname, sizeof(ipt_chainlabel)-1);
752
753 for (ptr = targetname; *ptr; ptr++)
754 if (isspace(*ptr))
755 exit_error(PARAMETER_PROBLEM,
756 "Invalid target name `%s'", targetname);
757 return targetname;
758}
759
760static char *
761addr_to_network(const struct in_addr *addr)
762{
763 struct netent *net;
764
765 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
766 return (char *) net->n_name;
767
768 return (char *) NULL;
769}
770
771char *
772addr_to_dotted(const struct in_addr *addrp)
773{
774 static char buf[20];
775 const unsigned char *bytep;
776
777 bytep = (const unsigned char *) &(addrp->s_addr);
778 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
779 return buf;
780}
781static char *
782addr_to_anyname(const struct in_addr *addr)
783{
784 char *name;
785
786 if ((name = addr_to_host(addr)) != NULL ||
787 (name = addr_to_network(addr)) != NULL)
788 return name;
789
790 return addr_to_dotted(addr);
791}
792
793static char *
794mask_to_dotted(const struct in_addr *mask)
795{
796 int i;
797 static char buf[20];
798 u_int32_t maskaddr, bits;
799
800 maskaddr = ntohl(mask->s_addr);
801
802 if (maskaddr == 0xFFFFFFFFL)
803 /* we don't want to see "/32" */
804 return "";
805
806 i = 32;
807 bits = 0xFFFFFFFEL;
808 while (--i >= 0 && maskaddr != bits)
809 bits <<= 1;
810 if (i >= 0)
811 sprintf(buf, "/%d", i);
812 else
813 /* mask was not a decent combination of 1's and 0's */
814 sprintf(buf, "/%s", addr_to_dotted(mask));
815
816 return buf;
817}
818
819int
820string_to_number(const char *s, int min, int max)
821{
822 int number;
823 char *end;
824
825 /* Handle hex, octal, etc. */
826 number = (int)strtol(s, &end, 0);
827 if (*end == '\0' && end != s) {
828 /* we parsed a number, let's see if we want this */
829 if (min <= number && number <= max)
830 return number;
831 }
832 return -1;
833}
834
835static void
836set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
837 int invert)
838{
839 if (*options & option)
840 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
841 opt2char(option));
842 *options |= option;
843
844 if (invert) {
845 unsigned int i;
846 for (i = 0; 1 << i != option; i++);
847
848 if (!inverse_for_options[i])
849 exit_error(PARAMETER_PROBLEM,
850 "cannot have ! before -%c",
851 opt2char(option));
852 *invflg |= inverse_for_options[i];
853 }
854}
855
856struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000857find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000858{
859 struct iptables_target *ptr;
860
861 /* Standard target? */
862 if (strcmp(name, "") == 0
863 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
864 || strcmp(name, IPTC_LABEL_DROP) == 0
865 || strcmp(name, IPTC_LABEL_QUEUE) == 0
866 || strcmp(name, IPTC_LABEL_RETURN) == 0)
867 name = "standard";
868
869 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
870 if (strcmp(name, ptr->name) == 0)
871 break;
872 }
873
Rusty Russell52a51492000-05-02 16:44:29 +0000874 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000875 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
876 + strlen(name)];
877 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000878 if (dlopen(path, RTLD_NOW)) {
879 /* Found library. If it didn't register itself,
880 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000881 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000882 if (!ptr)
883 exit_error(PARAMETER_PROBLEM,
884 "Couldn't load target `%s'\n",
885 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000886 } else if (tryload == LOAD_MUST_SUCCEED)
887 exit_error(PARAMETER_PROBLEM,
888 "Couldn't load target `%s'\n", name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000889 }
890
891 return ptr;
892}
893
894static struct option *
895merge_options(struct option *oldopts, struct option *newopts,
896 unsigned int *option_offset)
897{
898 unsigned int num_old, num_new, i;
899 struct option *merge;
900
901 for (num_old = 0; oldopts[num_old].name; num_old++);
902 for (num_new = 0; newopts[num_new].name; num_new++);
903
904 global_option_offset += OPTION_OFFSET;
905 *option_offset = global_option_offset;
906
907 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
908 memcpy(merge, oldopts, num_old * sizeof(struct option));
909 for (i = 0; i < num_new; i++) {
910 merge[num_old + i] = newopts[i];
911 merge[num_old + i].val += *option_offset;
912 }
913 memset(merge + num_old + num_new, 0, sizeof(struct option));
914
915 return merge;
916}
917
918void
919register_match(struct iptables_match *me)
920{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000921 struct iptables_match **i;
922
Marc Bouchere6869a82000-03-20 06:03:29 +0000923 if (strcmp(me->version, program_version) != 0) {
924 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
925 program_name, me->name, me->version, program_version);
926 exit(1);
927 }
928
Rusty Russell52a51492000-05-02 16:44:29 +0000929 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000930 fprintf(stderr, "%s: match `%s' already registered.\n",
931 program_name, me->name);
932 exit(1);
933 }
934
Rusty Russell73f72f52000-07-03 10:17:57 +0000935 if (me->size != IPT_ALIGN(me->size)) {
936 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
937 program_name, me->name, me->size);
938 exit(1);
939 }
940
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000941 /* Append to list. */
942 for (i = &iptables_matches; *i; i = &(*i)->next);
943 me->next = NULL;
944 *i = me;
945
Marc Bouchere6869a82000-03-20 06:03:29 +0000946 me->m = NULL;
947 me->mflags = 0;
948
949 opts = merge_options(opts, me->extra_opts, &me->option_offset);
950}
951
952void
953register_target(struct iptables_target *me)
954{
955 if (strcmp(me->version, program_version) != 0) {
956 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
957 program_name, me->name, me->version, program_version);
958 exit(1);
959 }
960
Rusty Russell52a51492000-05-02 16:44:29 +0000961 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000962 fprintf(stderr, "%s: target `%s' already registered.\n",
963 program_name, me->name);
964 exit(1);
965 }
966
Rusty Russell73f72f52000-07-03 10:17:57 +0000967 if (me->size != IPT_ALIGN(me->size)) {
968 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
969 program_name, me->name, me->size);
970 exit(1);
971 }
972
Marc Bouchere6869a82000-03-20 06:03:29 +0000973 /* Prepend to list. */
974 me->next = iptables_targets;
975 iptables_targets = me;
976 me->t = NULL;
977 me->tflags = 0;
978
979 opts = merge_options(opts, me->extra_opts, &me->option_offset);
980}
981
982static void
983print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
984{
985 struct ipt_counters counters;
986 const char *pol = iptc_get_policy(chain, &counters, handle);
987 printf("Chain %s", chain);
988 if (pol) {
989 printf(" (policy %s", pol);
990 if (!(format & FMT_NOCOUNTS))
991 printf(" %llu packets, %llu bytes",
992 counters.pcnt, counters.bcnt);
993 printf(")\n");
994 } else {
995 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000996 if (!iptc_get_references(&refs, chain, handle))
997 printf(" (ERROR obtaining refs)\n");
998 else
999 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001000 }
1001
1002 if (format & FMT_LINENUMBERS)
1003 printf(FMT("%-4s ", "%s "), "num");
1004 if (!(format & FMT_NOCOUNTS)) {
1005 if (format & FMT_KILOMEGAGIGA) {
1006 printf(FMT("%5s ","%s "), "pkts");
1007 printf(FMT("%5s ","%s "), "bytes");
1008 } else {
1009 printf(FMT("%8s ","%s "), "pkts");
1010 printf(FMT("%10s ","%s "), "bytes");
1011 }
1012 }
1013 if (!(format & FMT_NOTARGET))
1014 printf(FMT("%-9s ","%s "), "target");
1015 fputs(" prot ", stdout);
1016 if (format & FMT_OPTIONS)
1017 fputs("opt", stdout);
1018 if (format & FMT_VIA) {
1019 printf(FMT(" %-6s ","%s "), "in");
1020 printf(FMT("%-6s ","%s "), "out");
1021 }
1022 printf(FMT(" %-19s ","%s "), "source");
1023 printf(FMT(" %-19s "," %s "), "destination");
1024 printf("\n");
1025}
1026
1027static void
1028print_num(u_int64_t number, unsigned int format)
1029{
1030 if (format & FMT_KILOMEGAGIGA) {
1031 if (number > 99999) {
1032 number = (number + 500) / 1000;
1033 if (number > 9999) {
1034 number = (number + 500) / 1000;
1035 if (number > 9999) {
1036 number = (number + 500) / 1000;
1037 printf(FMT("%4lluG ","%lluG "),number);
1038 }
1039 else printf(FMT("%4lluM ","%lluM "), number);
1040 } else
1041 printf(FMT("%4lluK ","%lluK "), number);
1042 } else
1043 printf(FMT("%5llu ","%llu "), number);
1044 } else
1045 printf(FMT("%8llu ","%llu "), number);
1046}
1047
1048static int
1049print_match(const struct ipt_entry_match *m,
1050 const struct ipt_ip *ip,
1051 int numeric)
1052{
Rusty Russell52a51492000-05-02 16:44:29 +00001053 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001054
1055 if (match) {
1056 if (match->print)
1057 match->print(ip, m, numeric);
1058 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001059 if (m->u.user.name[0])
1060 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001061 }
1062 /* Don't stop iterating. */
1063 return 0;
1064}
1065
1066/* e is called `fw' here for hysterical raisins */
1067static void
1068print_firewall(const struct ipt_entry *fw,
1069 const char *targname,
1070 unsigned int num,
1071 unsigned int format,
1072 const iptc_handle_t handle)
1073{
1074 struct iptables_target *target = NULL;
1075 const struct ipt_entry_target *t;
1076 u_int8_t flags;
1077 char buf[BUFSIZ];
1078
1079 /* User creates a chain called "REJECT": this overrides the
1080 `REJECT' target module. Keep feeding them rope until the
1081 revolution... Bwahahahahah */
1082 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001083 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001084 else
Rusty Russell52a51492000-05-02 16:44:29 +00001085 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001086
1087 t = ipt_get_target((struct ipt_entry *)fw);
1088 flags = fw->ip.flags;
1089
1090 if (format & FMT_LINENUMBERS)
1091 printf(FMT("%-4u ", "%u "), num+1);
1092
1093 if (!(format & FMT_NOCOUNTS)) {
1094 print_num(fw->counters.pcnt, format);
1095 print_num(fw->counters.bcnt, format);
1096 }
1097
1098 if (!(format & FMT_NOTARGET))
1099 printf(FMT("%-9s ", "%s "), targname);
1100
1101 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1102 {
Rusty Russell28381a42000-05-10 00:19:50 +00001103 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001104 if (pname)
1105 printf(FMT("%-5s", "%s "), pname);
1106 else
1107 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1108 }
1109
1110 if (format & FMT_OPTIONS) {
1111 if (format & FMT_NOTABLE)
1112 fputs("opt ", stdout);
1113 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1114 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1115 fputc(' ', stdout);
1116 }
1117
1118 if (format & FMT_VIA) {
1119 char iface[IFNAMSIZ+2];
1120
1121 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1122 iface[0] = '!';
1123 iface[1] = '\0';
1124 }
1125 else iface[0] = '\0';
1126
1127 if (fw->ip.iniface[0] != '\0') {
1128 strcat(iface, fw->ip.iniface);
1129 /* If it doesn't compare the nul-term, it's a
1130 wildcard. */
1131 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1132 strcat(iface, "+");
1133 }
1134 else if (format & FMT_NUMERIC) strcat(iface, "*");
1135 else strcat(iface, "any");
1136 printf(FMT(" %-6s ","in %s "), iface);
1137
1138 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1139 iface[0] = '!';
1140 iface[1] = '\0';
1141 }
1142 else iface[0] = '\0';
1143
1144 if (fw->ip.outiface[0] != '\0') {
1145 strcat(iface, fw->ip.outiface);
1146 /* If it doesn't compare the nul-term, it's a
1147 wildcard. */
1148 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1149 strcat(iface, "+");
1150 }
1151 else if (format & FMT_NUMERIC) strcat(iface, "*");
1152 else strcat(iface, "any");
1153 printf(FMT("%-6s ","out %s "), iface);
1154 }
1155
1156 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1157 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1158 printf(FMT("%-19s ","%s "), "anywhere");
1159 else {
1160 if (format & FMT_NUMERIC)
1161 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1162 else
1163 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1164 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1165 printf(FMT("%-19s ","%s "), buf);
1166 }
1167
1168 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1169 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1170 printf(FMT("%-19s","-> %s"), "anywhere");
1171 else {
1172 if (format & FMT_NUMERIC)
1173 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1174 else
1175 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1176 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1177 printf(FMT("%-19s","-> %s"), buf);
1178 }
1179
1180 if (format & FMT_NOTABLE)
1181 fputs(" ", stdout);
1182
1183 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1184
1185 if (target) {
1186 if (target->print)
1187 /* Print the target information. */
1188 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001189 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001191 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001192
1193 if (!(format & FMT_NONEWLINE))
1194 fputc('\n', stdout);
1195}
1196
1197static void
1198print_firewall_line(const struct ipt_entry *fw,
1199 const iptc_handle_t h)
1200{
1201 struct ipt_entry_target *t;
1202
1203 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001204 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001205}
1206
1207static int
1208append_entry(const ipt_chainlabel chain,
1209 struct ipt_entry *fw,
1210 unsigned int nsaddrs,
1211 const struct in_addr saddrs[],
1212 unsigned int ndaddrs,
1213 const struct in_addr daddrs[],
1214 int verbose,
1215 iptc_handle_t *handle)
1216{
1217 unsigned int i, j;
1218 int ret = 1;
1219
1220 for (i = 0; i < nsaddrs; i++) {
1221 fw->ip.src.s_addr = saddrs[i].s_addr;
1222 for (j = 0; j < ndaddrs; j++) {
1223 fw->ip.dst.s_addr = daddrs[j].s_addr;
1224 if (verbose)
1225 print_firewall_line(fw, *handle);
1226 ret &= iptc_append_entry(chain, fw, handle);
1227 }
1228 }
1229
1230 return ret;
1231}
1232
1233static int
1234replace_entry(const ipt_chainlabel chain,
1235 struct ipt_entry *fw,
1236 unsigned int rulenum,
1237 const struct in_addr *saddr,
1238 const struct in_addr *daddr,
1239 int verbose,
1240 iptc_handle_t *handle)
1241{
1242 fw->ip.src.s_addr = saddr->s_addr;
1243 fw->ip.dst.s_addr = daddr->s_addr;
1244
1245 if (verbose)
1246 print_firewall_line(fw, *handle);
1247 return iptc_replace_entry(chain, fw, rulenum, handle);
1248}
1249
1250static int
1251insert_entry(const ipt_chainlabel chain,
1252 struct ipt_entry *fw,
1253 unsigned int rulenum,
1254 unsigned int nsaddrs,
1255 const struct in_addr saddrs[],
1256 unsigned int ndaddrs,
1257 const struct in_addr daddrs[],
1258 int verbose,
1259 iptc_handle_t *handle)
1260{
1261 unsigned int i, j;
1262 int ret = 1;
1263
1264 for (i = 0; i < nsaddrs; i++) {
1265 fw->ip.src.s_addr = saddrs[i].s_addr;
1266 for (j = 0; j < ndaddrs; j++) {
1267 fw->ip.dst.s_addr = daddrs[j].s_addr;
1268 if (verbose)
1269 print_firewall_line(fw, *handle);
1270 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1271 }
1272 }
1273
1274 return ret;
1275}
1276
Rusty Russell2e0a3212000-04-19 11:23:18 +00001277static unsigned char *
1278make_delete_mask(struct ipt_entry *fw)
1279{
1280 /* Establish mask for comparison */
1281 unsigned int size;
1282 struct iptables_match *m;
1283 unsigned char *mask, *mptr;
1284
1285 size = sizeof(struct ipt_entry);
1286 for (m = iptables_matches; m; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001287 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001288
Rusty Russell9e1d2142000-04-23 09:11:12 +00001289 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001290 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001291 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001292
Rusty Russell9e1d2142000-04-23 09:11:12 +00001293 memset(mask, 0xFF, sizeof(struct ipt_entry));
1294 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001295
1296 for (m = iptables_matches; m; m = m->next) {
1297 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001298 IPT_ALIGN(sizeof(struct ipt_entry_match))
1299 + m->userspacesize);
1300 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001301 }
1302
Rusty Russell73f72f52000-07-03 10:17:57 +00001303 memset(mptr, 0xFF,
1304 IPT_ALIGN(sizeof(struct ipt_entry_target))
1305 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001306
1307 return mask;
1308}
1309
Marc Bouchere6869a82000-03-20 06:03:29 +00001310static int
1311delete_entry(const ipt_chainlabel chain,
1312 struct ipt_entry *fw,
1313 unsigned int nsaddrs,
1314 const struct in_addr saddrs[],
1315 unsigned int ndaddrs,
1316 const struct in_addr daddrs[],
1317 int verbose,
1318 iptc_handle_t *handle)
1319{
1320 unsigned int i, j;
1321 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001322 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001323
Rusty Russell2e0a3212000-04-19 11:23:18 +00001324 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001325 for (i = 0; i < nsaddrs; i++) {
1326 fw->ip.src.s_addr = saddrs[i].s_addr;
1327 for (j = 0; j < ndaddrs; j++) {
1328 fw->ip.dst.s_addr = daddrs[j].s_addr;
1329 if (verbose)
1330 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001331 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 }
1333 }
1334 return ret;
1335}
1336
1337static int
1338check_packet(const ipt_chainlabel chain,
1339 struct ipt_entry *fw,
1340 unsigned int nsaddrs,
1341 const struct in_addr saddrs[],
1342 unsigned int ndaddrs,
1343 const struct in_addr daddrs[],
1344 int verbose,
1345 iptc_handle_t *handle)
1346{
1347 int ret = 1;
1348 unsigned int i, j;
1349 const char *msg;
1350
1351 for (i = 0; i < nsaddrs; i++) {
1352 fw->ip.src.s_addr = saddrs[i].s_addr;
1353 for (j = 0; j < ndaddrs; j++) {
1354 fw->ip.dst.s_addr = daddrs[j].s_addr;
1355 if (verbose)
1356 print_firewall_line(fw, *handle);
1357 msg = iptc_check_packet(chain, fw, handle);
1358 if (!msg) ret = 0;
1359 else printf("%s\n", msg);
1360 }
1361 }
1362
1363 return ret;
1364}
1365
1366static int
1367for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001368 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001369{
1370 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001371 const char *chain;
1372 char *chains;
1373 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001374
Rusty Russell9e1d2142000-04-23 09:11:12 +00001375 chain = iptc_first_chain(handle);
1376 while (chain) {
1377 chaincount++;
1378 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001379 }
1380
Rusty Russell9e1d2142000-04-23 09:11:12 +00001381 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1382 i = 0;
1383 chain = iptc_first_chain(handle);
1384 while (chain) {
1385 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1386 i++;
1387 chain = iptc_next_chain(handle);
1388 }
1389
1390 for (i = 0; i < chaincount; i++) {
1391 if (!builtinstoo
1392 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1393 *handle))
1394 continue;
1395 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1396 }
1397
1398 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001399 return ret;
1400}
1401
1402static int
1403flush_entries(const ipt_chainlabel chain, int verbose,
1404 iptc_handle_t *handle)
1405{
1406 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001407 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001408
1409 if (verbose)
1410 fprintf(stdout, "Flushing chain `%s'\n", chain);
1411 return iptc_flush_entries(chain, handle);
1412}
Marc Bouchere6869a82000-03-20 06:03:29 +00001413
1414static int
1415zero_entries(const ipt_chainlabel chain, int verbose,
1416 iptc_handle_t *handle)
1417{
1418 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001419 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001420
Marc Bouchere6869a82000-03-20 06:03:29 +00001421 if (verbose)
1422 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1423 return iptc_zero_entries(chain, handle);
1424}
1425
1426static int
1427delete_chain(const ipt_chainlabel chain, int verbose,
1428 iptc_handle_t *handle)
1429{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001430 if (!chain)
1431 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001432
1433 if (verbose)
1434 fprintf(stdout, "Deleting chain `%s'\n", chain);
1435 return iptc_delete_chain(chain, handle);
1436}
1437
1438static int
1439list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1440 int expanded, int linenumbers, iptc_handle_t *handle)
1441{
1442 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001443 unsigned int format;
1444 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001445
1446 format = FMT_OPTIONS;
1447 if (!verbose)
1448 format |= FMT_NOCOUNTS;
1449 else
1450 format |= FMT_VIA;
1451
1452 if (numeric)
1453 format |= FMT_NUMERIC;
1454
1455 if (!expanded)
1456 format |= FMT_KILOMEGAGIGA;
1457
1458 if (linenumbers)
1459 format |= FMT_LINENUMBERS;
1460
Rusty Russell9e1d2142000-04-23 09:11:12 +00001461 for (this = iptc_first_chain(handle);
1462 this;
1463 this = iptc_next_chain(handle)) {
1464 const struct ipt_entry *i;
1465 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
Marc Bouchere6869a82000-03-20 06:03:29 +00001467 if (chain && strcmp(chain, this) != 0)
1468 continue;
1469
1470 if (found) printf("\n");
1471
1472 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001473 i = iptc_first_rule(this, handle);
1474
1475 num = 0;
1476 while (i) {
1477 print_firewall(i,
1478 iptc_get_target(i, handle),
1479 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001480 format,
1481 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001482 i = iptc_next_rule(i, handle);
1483 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 found = 1;
1485 }
1486
1487 errno = ENOENT;
1488 return found;
1489}
1490
1491static struct ipt_entry *
1492generate_entry(const struct ipt_entry *fw,
1493 struct iptables_match *matches,
1494 struct ipt_entry_target *target)
1495{
1496 unsigned int size;
1497 struct iptables_match *m;
1498 struct ipt_entry *e;
1499
1500 size = sizeof(struct ipt_entry);
1501 for (m = matches; m; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001502 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001503
Rusty Russell228e98d2000-04-27 10:28:06 +00001504 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001505 *e = *fw;
1506 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001507 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001508
1509 size = 0;
1510 for (m = matches; m; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001511 memcpy(e->elems + size, m->m, m->m->u.match_size);
1512 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001513 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001514 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001515
1516 return e;
1517}
1518
1519int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1520{
1521 struct ipt_entry fw, *e = NULL;
1522 int invert = 0;
1523 unsigned int nsaddrs = 0, ndaddrs = 0;
1524 struct in_addr *saddrs = NULL, *daddrs = NULL;
1525
1526 int c, verbose = 0;
1527 const char *chain = NULL;
1528 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1529 const char *policy = NULL, *newname = NULL;
1530 unsigned int rulenum = 0, options = 0, command = 0;
1531 int ret = 1;
1532 struct iptables_match *m;
1533 struct iptables_target *target = NULL;
1534 const char *jumpto = "";
1535 char *protocol = NULL;
1536
1537 memset(&fw, 0, sizeof(fw));
1538
1539 /* Suppress error messages: we may add new options if we
1540 demand-load a protocol. */
1541 opterr = 0;
1542
1543 while ((c = getopt_long(argc, argv,
1544 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1545 opts, NULL)) != -1) {
1546 switch (c) {
1547 /*
1548 * Command selection
1549 */
1550 case 'A':
1551 add_command(&command, CMD_APPEND, CMD_NONE,
1552 invert);
1553 chain = optarg;
1554 break;
1555
1556 case 'D':
1557 add_command(&command, CMD_DELETE, CMD_NONE,
1558 invert);
1559 chain = optarg;
1560 if (optind < argc && argv[optind][0] != '-'
1561 && argv[optind][0] != '!') {
1562 rulenum = parse_rulenumber(argv[optind++]);
1563 command = CMD_DELETE_NUM;
1564 }
1565 break;
1566
1567 case 'C':
1568 add_command(&command, CMD_CHECK, CMD_NONE,
1569 invert);
1570 chain = optarg;
1571 break;
1572
1573 case 'R':
1574 add_command(&command, CMD_REPLACE, CMD_NONE,
1575 invert);
1576 chain = optarg;
1577 if (optind < argc && argv[optind][0] != '-'
1578 && argv[optind][0] != '!')
1579 rulenum = parse_rulenumber(argv[optind++]);
1580 else
1581 exit_error(PARAMETER_PROBLEM,
1582 "-%c requires a rule number",
1583 cmd2char(CMD_REPLACE));
1584 break;
1585
1586 case 'I':
1587 add_command(&command, CMD_INSERT, 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 else rulenum = 1;
1594 break;
1595
1596 case 'L':
1597 add_command(&command, CMD_LIST, CMD_ZERO,
1598 invert);
1599 if (optarg) chain = optarg;
1600 else if (optind < argc && argv[optind][0] != '-'
1601 && argv[optind][0] != '!')
1602 chain = argv[optind++];
1603 break;
1604
1605 case 'F':
1606 add_command(&command, CMD_FLUSH, CMD_NONE,
1607 invert);
1608 if (optarg) chain = optarg;
1609 else if (optind < argc && argv[optind][0] != '-'
1610 && argv[optind][0] != '!')
1611 chain = argv[optind++];
1612 break;
1613
1614 case 'Z':
1615 add_command(&command, CMD_ZERO, CMD_LIST,
1616 invert);
1617 if (optarg) chain = optarg;
1618 else if (optind < argc && argv[optind][0] != '-'
1619 && argv[optind][0] != '!')
1620 chain = argv[optind++];
1621 break;
1622
1623 case 'N':
1624 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1625 invert);
1626 chain = optarg;
1627 break;
1628
1629 case 'X':
1630 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1631 invert);
1632 if (optarg) chain = optarg;
1633 else if (optind < argc && argv[optind][0] != '-'
1634 && argv[optind][0] != '!')
1635 chain = argv[optind++];
1636 break;
1637
1638 case 'E':
1639 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1640 invert);
1641 chain = optarg;
1642 if (optind < argc && argv[optind][0] != '-'
1643 && argv[optind][0] != '!')
1644 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001645 else
1646 exit_error(PARAMETER_PROBLEM,
1647 "-%c requires old-chain-name and "
1648 "new-chain-name",
1649 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001650 break;
1651
1652 case 'P':
1653 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1654 invert);
1655 chain = optarg;
1656 if (optind < argc && argv[optind][0] != '-'
1657 && argv[optind][0] != '!')
1658 policy = argv[optind++];
1659 else
1660 exit_error(PARAMETER_PROBLEM,
1661 "-%c requires a chain and a policy",
1662 cmd2char(CMD_SET_POLICY));
1663 break;
1664
1665 case 'h':
1666 if (!optarg)
1667 optarg = argv[optind];
1668
Rusty Russell2e0a3212000-04-19 11:23:18 +00001669 /* iptables -p icmp -h */
1670 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001671 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001672
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 exit_printhelp();
1674
1675 /*
1676 * Option selection
1677 */
1678 case 'p':
1679 if (check_inverse(optarg, &invert))
1680 optind++;
1681 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1682 invert);
1683
1684 /* Canonicalize into lower case */
1685 for (protocol = argv[optind-1]; *protocol; protocol++)
1686 *protocol = tolower(*protocol);
1687
1688 protocol = argv[optind-1];
1689 fw.ip.proto = parse_protocol(protocol);
1690
1691 if (fw.ip.proto == 0
1692 && (fw.ip.invflags & IPT_INV_PROTO))
1693 exit_error(PARAMETER_PROBLEM,
1694 "rule would never match protocol");
1695 fw.nfcache |= NFC_IP_PROTO;
1696 break;
1697
1698 case 's':
1699 if (check_inverse(optarg, &invert))
1700 optind++;
1701 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1702 invert);
1703 shostnetworkmask = argv[optind-1];
1704 fw.nfcache |= NFC_IP_SRC;
1705 break;
1706
1707 case 'd':
1708 if (check_inverse(optarg, &invert))
1709 optind++;
1710 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1711 invert);
1712 dhostnetworkmask = argv[optind-1];
1713 fw.nfcache |= NFC_IP_DST;
1714 break;
1715
1716 case 'j':
1717 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1718 invert);
1719 jumpto = parse_target(optarg);
Rusty Russell52a51492000-05-02 16:44:29 +00001720 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001721
1722 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001723 size_t size;
1724
Rusty Russell73f72f52000-07-03 10:17:57 +00001725 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1726 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001727
Rusty Russell2e0a3212000-04-19 11:23:18 +00001728 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001729 target->t->u.target_size = size;
1730 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001731 target->init(target->t, &fw.nfcache);
1732 }
1733 break;
1734
1735
1736 case 'i':
1737 if (check_inverse(optarg, &invert))
1738 optind++;
1739 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1740 invert);
1741 parse_interface(argv[optind-1],
1742 fw.ip.iniface,
1743 fw.ip.iniface_mask);
1744 fw.nfcache |= NFC_IP_IF_IN;
1745 break;
1746
1747 case 'o':
1748 if (check_inverse(optarg, &invert))
1749 optind++;
1750 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1751 invert);
1752 parse_interface(argv[optind-1],
1753 fw.ip.outiface,
1754 fw.ip.outiface_mask);
1755 fw.nfcache |= NFC_IP_IF_OUT;
1756 break;
1757
1758 case 'f':
1759 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1760 invert);
1761 fw.ip.flags |= IPT_F_FRAG;
1762 fw.nfcache |= NFC_IP_FRAG;
1763 break;
1764
1765 case 'v':
1766 if (!verbose)
1767 set_option(&options, OPT_VERBOSE,
1768 &fw.ip.invflags, invert);
1769 verbose++;
1770 break;
1771
Rusty Russell52a51492000-05-02 16:44:29 +00001772 case 'm': {
1773 size_t size;
1774
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 if (invert)
1776 exit_error(PARAMETER_PROBLEM,
1777 "unexpected ! flag before --match");
1778
Rusty Russell52a51492000-05-02 16:44:29 +00001779 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001780 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1781 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001782 m->m = fw_calloc(1, size);
1783 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001784 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001785 m->init(m->m, &fw.nfcache);
1786 }
1787 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001788
1789 case 'n':
1790 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1791 invert);
1792 break;
1793
1794 case 't':
1795 if (invert)
1796 exit_error(PARAMETER_PROBLEM,
1797 "unexpected ! flag before --table");
1798 *table = argv[optind-1];
1799 break;
1800
1801 case 'x':
1802 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1803 invert);
1804 break;
1805
1806 case 'V':
1807 if (invert)
1808 printf("Not %s ;-)\n", program_version);
1809 else
1810 printf("%s v%s\n",
1811 program_name, program_version);
1812 exit(0);
1813
1814 case '0':
1815 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1816 invert);
1817 break;
1818
1819 case 1: /* non option */
1820 if (optarg[0] == '!' && optarg[1] == '\0') {
1821 if (invert)
1822 exit_error(PARAMETER_PROBLEM,
1823 "multiple consecutive ! not"
1824 " allowed");
1825 invert = TRUE;
1826 optarg[0] = '\0';
1827 continue;
1828 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001829 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001830 exit_tryhelp(2);
1831
1832 default:
1833 /* FIXME: This scheme doesn't allow two of the same
1834 matches --RR */
1835 if (!target
1836 || !(target->parse(c - target->option_offset,
1837 argv, invert,
1838 &target->tflags,
1839 &fw, &target->t))) {
1840 for (m = iptables_matches; m; m = m->next) {
1841 if (m->parse(c - m->option_offset,
1842 argv, invert,
1843 &m->mflags,
1844 &fw,
1845 &fw.nfcache,
1846 &m->m))
1847 break;
1848 }
1849
1850 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001851 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001852 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001853 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001854 && !find_proto(protocol, DONT_LOAD,
1855 options&OPT_NUMERIC)
1856 && (m = find_proto(protocol, TRY_LOAD,
1857 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001859 size_t size;
1860
Rusty Russell73f72f52000-07-03 10:17:57 +00001861 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1862 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001863
Rusty Russell2e0a3212000-04-19 11:23:18 +00001864 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001865 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001866 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001867 m->init(m->m, &fw.nfcache);
1868
1869 optind--;
1870 continue;
1871 }
1872 if (!m)
1873 exit_error(PARAMETER_PROBLEM,
1874 "Unknown arg `%s'",
1875 argv[optind-1]);
1876 }
1877 }
1878 invert = FALSE;
1879 }
1880
1881 for (m = iptables_matches; m; m = m->next)
1882 m->final_check(m->mflags);
1883 if (target)
1884 target->final_check(target->tflags);
1885
1886 /* Fix me: must put inverse options checking here --MN */
1887
1888 if (optind < argc)
1889 exit_error(PARAMETER_PROBLEM,
1890 "unknown arguments found on commandline");
1891 if (!command)
1892 exit_error(PARAMETER_PROBLEM, "no command specified");
1893 if (invert)
1894 exit_error(PARAMETER_PROBLEM,
1895 "nothing appropriate following !");
1896
Marc Boucher744bd022000-04-22 22:36:10 +00001897 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1898 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 if (!(options & OPT_DESTINATION))
1900 dhostnetworkmask = "0.0.0.0/0";
1901 if (!(options & OPT_SOURCE))
1902 shostnetworkmask = "0.0.0.0/0";
1903 }
1904
1905 if (shostnetworkmask)
1906 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1907 &(fw.ip.smsk), &nsaddrs);
1908
1909 if (dhostnetworkmask)
1910 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1911 &(fw.ip.dmsk), &ndaddrs);
1912
1913 if ((nsaddrs > 1 || ndaddrs > 1) &&
1914 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1915 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1916 " source or destination IP addresses");
1917
1918 if (command == CMD_CHECK && fw.ip.invflags != 0)
1919 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1920 cmd2char(CMD_CHECK));
1921
1922 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1923 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1924 "specify a unique address");
1925
1926 generic_opt_check(command, options);
1927
1928 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1929 exit_error(PARAMETER_PROBLEM,
1930 "chain name `%s' too long (must be under %i chars)",
1931 chain, IPT_FUNCTION_MAXNAMELEN);
1932
1933 *handle = iptc_init(*table);
1934 if (!*handle)
1935 exit_error(VERSION_PROBLEM,
1936 "can't initialize iptables table `%s': %s",
1937 *table, iptc_strerror(errno));
1938
Marc Boucher744bd022000-04-22 22:36:10 +00001939 if (command == CMD_CHECK
1940 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001941 || command == CMD_DELETE
1942 || command == CMD_INSERT
1943 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001944 if (strcmp(chain, "PREROUTING") == 0
1945 || strcmp(chain, "INPUT") == 0) {
1946 /* -o not valid with incoming packets. */
1947 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001948 exit_error(PARAMETER_PROBLEM,
1949 "Can't use -%c with %s\n",
1950 opt2char(OPT_VIANAMEOUT),
1951 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001952 /* -i required with -C */
1953 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1954 exit_error(PARAMETER_PROBLEM,
1955 "Need -%c with %s\n",
1956 opt2char(OPT_VIANAMEIN),
1957 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001958 }
1959
Rusty Russella4860fd2000-06-17 16:13:02 +00001960 if (strcmp(chain, "POSTROUTING") == 0
1961 || strcmp(chain, "OUTPUT") == 0) {
1962 /* -i not valid with outgoing packets */
1963 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001964 exit_error(PARAMETER_PROBLEM,
1965 "Can't use -%c with %s\n",
1966 opt2char(OPT_VIANAMEIN),
1967 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001968 /* -o required with -C */
1969 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1970 exit_error(PARAMETER_PROBLEM,
1971 "Need -%c with %s\n",
1972 opt2char(OPT_VIANAMEOUT),
1973 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001974 }
1975
1976 if (target && iptc_is_chain(jumpto, *handle)) {
1977 printf("Warning: using chain %s, not extension\n",
1978 jumpto);
1979
1980 target = NULL;
1981 }
1982
1983 /* If they didn't specify a target, or it's a chain
1984 name, use standard. */
1985 if (!target
1986 && (strlen(jumpto) == 0
1987 || iptc_is_chain(jumpto, *handle))) {
1988 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001989
Rusty Russell52a51492000-05-02 16:44:29 +00001990 target = find_target(IPT_STANDARD_TARGET,
1991 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001992
1993 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001994 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001995 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001996 target->t->u.target_size = size;
1997 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 target->init(target->t, &fw.nfcache);
1999 }
2000
Rusty Russell7e53bf92000-03-20 07:03:28 +00002001 if (!target) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002002 struct ipt_entry_target unknown_target;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002003
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 /* Don't know it. Must be extension with no
2005 options? */
Rusty Russell228e98d2000-04-27 10:28:06 +00002006 unknown_target.u.target_size = sizeof(unknown_target);
2007 strcpy(unknown_target.u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008
2009 e = generate_entry(&fw, iptables_matches,
2010 &unknown_target);
2011 } else {
2012 e = generate_entry(&fw, iptables_matches, target->t);
2013 }
2014 }
2015
2016 switch (command) {
2017 case CMD_APPEND:
2018 ret = append_entry(chain, e,
2019 nsaddrs, saddrs, ndaddrs, daddrs,
2020 options&OPT_VERBOSE,
2021 handle);
2022 break;
2023 case CMD_CHECK:
2024 ret = check_packet(chain, e,
2025 nsaddrs, saddrs, ndaddrs, daddrs,
2026 options&OPT_VERBOSE, handle);
2027 break;
2028 case CMD_DELETE:
2029 ret = delete_entry(chain, e,
2030 nsaddrs, saddrs, ndaddrs, daddrs,
2031 options&OPT_VERBOSE,
2032 handle);
2033 break;
2034 case CMD_DELETE_NUM:
2035 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2036 break;
2037 case CMD_REPLACE:
2038 ret = replace_entry(chain, e, rulenum - 1,
2039 saddrs, daddrs, options&OPT_VERBOSE,
2040 handle);
2041 break;
2042 case CMD_INSERT:
2043 ret = insert_entry(chain, e, rulenum - 1,
2044 nsaddrs, saddrs, ndaddrs, daddrs,
2045 options&OPT_VERBOSE,
2046 handle);
2047 break;
2048 case CMD_LIST:
2049 ret = list_entries(chain,
2050 options&OPT_VERBOSE,
2051 options&OPT_NUMERIC,
2052 options&OPT_EXPANDED,
2053 options&OPT_LINENUMBERS,
2054 handle);
2055 break;
2056 case CMD_FLUSH:
2057 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2058 break;
2059 case CMD_ZERO:
2060 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2061 break;
2062 case CMD_LIST|CMD_ZERO:
2063 ret = list_entries(chain,
2064 options&OPT_VERBOSE,
2065 options&OPT_NUMERIC,
2066 options&OPT_EXPANDED,
2067 options&OPT_LINENUMBERS,
2068 handle);
2069 if (ret)
2070 ret = zero_entries(chain,
2071 options&OPT_VERBOSE, handle);
2072 break;
2073 case CMD_NEW_CHAIN:
2074 ret = iptc_create_chain(chain, handle);
2075 break;
2076 case CMD_DELETE_CHAIN:
2077 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2078 break;
2079 case CMD_RENAME_CHAIN:
2080 ret = iptc_rename_chain(chain, newname, handle);
2081 break;
2082 case CMD_SET_POLICY:
2083 ret = iptc_set_policy(chain, policy, handle);
2084 break;
2085 default:
2086 /* We should never reach this... */
2087 exit_tryhelp(2);
2088 }
2089
2090 if (verbose > 1)
2091 dump_entries(*handle);
2092
2093 return ret;
2094}