blob: 92f0175dc076add73965b604adef21897cec63a5 [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{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000823 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000824 char *end;
825
826 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000827 errno = 0;
828 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000829 if (*end == '\0' && end != s) {
830 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000831 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000832 return number;
833 }
834 return -1;
835}
836
837static void
838set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
839 int invert)
840{
841 if (*options & option)
842 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
843 opt2char(option));
844 *options |= option;
845
846 if (invert) {
847 unsigned int i;
848 for (i = 0; 1 << i != option; i++);
849
850 if (!inverse_for_options[i])
851 exit_error(PARAMETER_PROBLEM,
852 "cannot have ! before -%c",
853 opt2char(option));
854 *invflg |= inverse_for_options[i];
855 }
856}
857
858struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000859find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000860{
861 struct iptables_target *ptr;
862
863 /* Standard target? */
864 if (strcmp(name, "") == 0
865 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
866 || strcmp(name, IPTC_LABEL_DROP) == 0
867 || strcmp(name, IPTC_LABEL_QUEUE) == 0
868 || strcmp(name, IPTC_LABEL_RETURN) == 0)
869 name = "standard";
870
871 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
872 if (strcmp(name, ptr->name) == 0)
873 break;
874 }
875
Rusty Russell52a51492000-05-02 16:44:29 +0000876 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000877 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
878 + strlen(name)];
879 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000880 if (dlopen(path, RTLD_NOW)) {
881 /* Found library. If it didn't register itself,
882 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000883 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000884 if (!ptr)
885 exit_error(PARAMETER_PROBLEM,
886 "Couldn't load target `%s'\n",
887 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000888 } else if (tryload == LOAD_MUST_SUCCEED)
889 exit_error(PARAMETER_PROBLEM,
Harald Welteaa204722000-08-11 14:02:27 +0000890 "Couldn't load target `%s':%s\n",
891 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000892 }
893
894 return ptr;
895}
896
897static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000898merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000899 unsigned int *option_offset)
900{
901 unsigned int num_old, num_new, i;
902 struct option *merge;
903
904 for (num_old = 0; oldopts[num_old].name; num_old++);
905 for (num_new = 0; newopts[num_new].name; num_new++);
906
907 global_option_offset += OPTION_OFFSET;
908 *option_offset = global_option_offset;
909
910 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
911 memcpy(merge, oldopts, num_old * sizeof(struct option));
912 for (i = 0; i < num_new; i++) {
913 merge[num_old + i] = newopts[i];
914 merge[num_old + i].val += *option_offset;
915 }
916 memset(merge + num_old + num_new, 0, sizeof(struct option));
917
918 return merge;
919}
920
921void
922register_match(struct iptables_match *me)
923{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000924 struct iptables_match **i;
925
Marc Bouchere6869a82000-03-20 06:03:29 +0000926 if (strcmp(me->version, program_version) != 0) {
927 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
928 program_name, me->name, me->version, program_version);
929 exit(1);
930 }
931
Rusty Russell52a51492000-05-02 16:44:29 +0000932 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000933 fprintf(stderr, "%s: match `%s' already registered.\n",
934 program_name, me->name);
935 exit(1);
936 }
937
Rusty Russell73f72f52000-07-03 10:17:57 +0000938 if (me->size != IPT_ALIGN(me->size)) {
939 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
940 program_name, me->name, me->size);
941 exit(1);
942 }
943
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000944 /* Append to list. */
945 for (i = &iptables_matches; *i; i = &(*i)->next);
946 me->next = NULL;
947 *i = me;
948
Marc Bouchere6869a82000-03-20 06:03:29 +0000949 me->m = NULL;
950 me->mflags = 0;
951
952 opts = merge_options(opts, me->extra_opts, &me->option_offset);
953}
954
955void
956register_target(struct iptables_target *me)
957{
958 if (strcmp(me->version, program_version) != 0) {
959 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
960 program_name, me->name, me->version, program_version);
961 exit(1);
962 }
963
Rusty Russell52a51492000-05-02 16:44:29 +0000964 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000965 fprintf(stderr, "%s: target `%s' already registered.\n",
966 program_name, me->name);
967 exit(1);
968 }
969
Rusty Russell73f72f52000-07-03 10:17:57 +0000970 if (me->size != IPT_ALIGN(me->size)) {
971 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
972 program_name, me->name, me->size);
973 exit(1);
974 }
975
Marc Bouchere6869a82000-03-20 06:03:29 +0000976 /* Prepend to list. */
977 me->next = iptables_targets;
978 iptables_targets = me;
979 me->t = NULL;
980 me->tflags = 0;
981
982 opts = merge_options(opts, me->extra_opts, &me->option_offset);
983}
984
985static void
986print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
987{
988 struct ipt_counters counters;
989 const char *pol = iptc_get_policy(chain, &counters, handle);
990 printf("Chain %s", chain);
991 if (pol) {
992 printf(" (policy %s", pol);
993 if (!(format & FMT_NOCOUNTS))
994 printf(" %llu packets, %llu bytes",
995 counters.pcnt, counters.bcnt);
996 printf(")\n");
997 } else {
998 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000999 if (!iptc_get_references(&refs, chain, handle))
1000 printf(" (ERROR obtaining refs)\n");
1001 else
1002 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001003 }
1004
1005 if (format & FMT_LINENUMBERS)
1006 printf(FMT("%-4s ", "%s "), "num");
1007 if (!(format & FMT_NOCOUNTS)) {
1008 if (format & FMT_KILOMEGAGIGA) {
1009 printf(FMT("%5s ","%s "), "pkts");
1010 printf(FMT("%5s ","%s "), "bytes");
1011 } else {
1012 printf(FMT("%8s ","%s "), "pkts");
1013 printf(FMT("%10s ","%s "), "bytes");
1014 }
1015 }
1016 if (!(format & FMT_NOTARGET))
1017 printf(FMT("%-9s ","%s "), "target");
1018 fputs(" prot ", stdout);
1019 if (format & FMT_OPTIONS)
1020 fputs("opt", stdout);
1021 if (format & FMT_VIA) {
1022 printf(FMT(" %-6s ","%s "), "in");
1023 printf(FMT("%-6s ","%s "), "out");
1024 }
1025 printf(FMT(" %-19s ","%s "), "source");
1026 printf(FMT(" %-19s "," %s "), "destination");
1027 printf("\n");
1028}
1029
1030static void
1031print_num(u_int64_t number, unsigned int format)
1032{
1033 if (format & FMT_KILOMEGAGIGA) {
1034 if (number > 99999) {
1035 number = (number + 500) / 1000;
1036 if (number > 9999) {
1037 number = (number + 500) / 1000;
1038 if (number > 9999) {
1039 number = (number + 500) / 1000;
1040 printf(FMT("%4lluG ","%lluG "),number);
1041 }
1042 else printf(FMT("%4lluM ","%lluM "), number);
1043 } else
1044 printf(FMT("%4lluK ","%lluK "), number);
1045 } else
1046 printf(FMT("%5llu ","%llu "), number);
1047 } else
1048 printf(FMT("%8llu ","%llu "), number);
1049}
1050
1051static int
1052print_match(const struct ipt_entry_match *m,
1053 const struct ipt_ip *ip,
1054 int numeric)
1055{
Rusty Russell52a51492000-05-02 16:44:29 +00001056 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001057
1058 if (match) {
1059 if (match->print)
1060 match->print(ip, m, numeric);
1061 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001062 if (m->u.user.name[0])
1063 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001064 }
1065 /* Don't stop iterating. */
1066 return 0;
1067}
1068
1069/* e is called `fw' here for hysterical raisins */
1070static void
1071print_firewall(const struct ipt_entry *fw,
1072 const char *targname,
1073 unsigned int num,
1074 unsigned int format,
1075 const iptc_handle_t handle)
1076{
1077 struct iptables_target *target = NULL;
1078 const struct ipt_entry_target *t;
1079 u_int8_t flags;
1080 char buf[BUFSIZ];
1081
1082 /* User creates a chain called "REJECT": this overrides the
1083 `REJECT' target module. Keep feeding them rope until the
1084 revolution... Bwahahahahah */
1085 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001086 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001087 else
Rusty Russell52a51492000-05-02 16:44:29 +00001088 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001089
1090 t = ipt_get_target((struct ipt_entry *)fw);
1091 flags = fw->ip.flags;
1092
1093 if (format & FMT_LINENUMBERS)
1094 printf(FMT("%-4u ", "%u "), num+1);
1095
1096 if (!(format & FMT_NOCOUNTS)) {
1097 print_num(fw->counters.pcnt, format);
1098 print_num(fw->counters.bcnt, format);
1099 }
1100
1101 if (!(format & FMT_NOTARGET))
1102 printf(FMT("%-9s ", "%s "), targname);
1103
1104 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1105 {
Rusty Russell28381a42000-05-10 00:19:50 +00001106 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001107 if (pname)
1108 printf(FMT("%-5s", "%s "), pname);
1109 else
1110 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1111 }
1112
1113 if (format & FMT_OPTIONS) {
1114 if (format & FMT_NOTABLE)
1115 fputs("opt ", stdout);
1116 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1117 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1118 fputc(' ', stdout);
1119 }
1120
1121 if (format & FMT_VIA) {
1122 char iface[IFNAMSIZ+2];
1123
1124 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1125 iface[0] = '!';
1126 iface[1] = '\0';
1127 }
1128 else iface[0] = '\0';
1129
1130 if (fw->ip.iniface[0] != '\0') {
1131 strcat(iface, fw->ip.iniface);
1132 /* If it doesn't compare the nul-term, it's a
1133 wildcard. */
1134 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1135 strcat(iface, "+");
1136 }
1137 else if (format & FMT_NUMERIC) strcat(iface, "*");
1138 else strcat(iface, "any");
1139 printf(FMT(" %-6s ","in %s "), iface);
1140
1141 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1142 iface[0] = '!';
1143 iface[1] = '\0';
1144 }
1145 else iface[0] = '\0';
1146
1147 if (fw->ip.outiface[0] != '\0') {
1148 strcat(iface, fw->ip.outiface);
1149 /* If it doesn't compare the nul-term, it's a
1150 wildcard. */
1151 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1152 strcat(iface, "+");
1153 }
1154 else if (format & FMT_NUMERIC) strcat(iface, "*");
1155 else strcat(iface, "any");
1156 printf(FMT("%-6s ","out %s "), iface);
1157 }
1158
1159 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1160 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1161 printf(FMT("%-19s ","%s "), "anywhere");
1162 else {
1163 if (format & FMT_NUMERIC)
1164 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1165 else
1166 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1167 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1168 printf(FMT("%-19s ","%s "), buf);
1169 }
1170
1171 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1172 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1173 printf(FMT("%-19s","-> %s"), "anywhere");
1174 else {
1175 if (format & FMT_NUMERIC)
1176 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1177 else
1178 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1179 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1180 printf(FMT("%-19s","-> %s"), buf);
1181 }
1182
1183 if (format & FMT_NOTABLE)
1184 fputs(" ", stdout);
1185
1186 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1187
1188 if (target) {
1189 if (target->print)
1190 /* Print the target information. */
1191 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001192 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001193 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001194 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
1196 if (!(format & FMT_NONEWLINE))
1197 fputc('\n', stdout);
1198}
1199
1200static void
1201print_firewall_line(const struct ipt_entry *fw,
1202 const iptc_handle_t h)
1203{
1204 struct ipt_entry_target *t;
1205
1206 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001207 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001208}
1209
1210static int
1211append_entry(const ipt_chainlabel chain,
1212 struct ipt_entry *fw,
1213 unsigned int nsaddrs,
1214 const struct in_addr saddrs[],
1215 unsigned int ndaddrs,
1216 const struct in_addr daddrs[],
1217 int verbose,
1218 iptc_handle_t *handle)
1219{
1220 unsigned int i, j;
1221 int ret = 1;
1222
1223 for (i = 0; i < nsaddrs; i++) {
1224 fw->ip.src.s_addr = saddrs[i].s_addr;
1225 for (j = 0; j < ndaddrs; j++) {
1226 fw->ip.dst.s_addr = daddrs[j].s_addr;
1227 if (verbose)
1228 print_firewall_line(fw, *handle);
1229 ret &= iptc_append_entry(chain, fw, handle);
1230 }
1231 }
1232
1233 return ret;
1234}
1235
1236static int
1237replace_entry(const ipt_chainlabel chain,
1238 struct ipt_entry *fw,
1239 unsigned int rulenum,
1240 const struct in_addr *saddr,
1241 const struct in_addr *daddr,
1242 int verbose,
1243 iptc_handle_t *handle)
1244{
1245 fw->ip.src.s_addr = saddr->s_addr;
1246 fw->ip.dst.s_addr = daddr->s_addr;
1247
1248 if (verbose)
1249 print_firewall_line(fw, *handle);
1250 return iptc_replace_entry(chain, fw, rulenum, handle);
1251}
1252
1253static int
1254insert_entry(const ipt_chainlabel chain,
1255 struct ipt_entry *fw,
1256 unsigned int rulenum,
1257 unsigned int nsaddrs,
1258 const struct in_addr saddrs[],
1259 unsigned int ndaddrs,
1260 const struct in_addr daddrs[],
1261 int verbose,
1262 iptc_handle_t *handle)
1263{
1264 unsigned int i, j;
1265 int ret = 1;
1266
1267 for (i = 0; i < nsaddrs; i++) {
1268 fw->ip.src.s_addr = saddrs[i].s_addr;
1269 for (j = 0; j < ndaddrs; j++) {
1270 fw->ip.dst.s_addr = daddrs[j].s_addr;
1271 if (verbose)
1272 print_firewall_line(fw, *handle);
1273 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1274 }
1275 }
1276
1277 return ret;
1278}
1279
Rusty Russell2e0a3212000-04-19 11:23:18 +00001280static unsigned char *
1281make_delete_mask(struct ipt_entry *fw)
1282{
1283 /* Establish mask for comparison */
1284 unsigned int size;
1285 struct iptables_match *m;
1286 unsigned char *mask, *mptr;
1287
1288 size = sizeof(struct ipt_entry);
1289 for (m = iptables_matches; m; m = m->next)
Rusty Russell73f72f52000-07-03 10:17:57 +00001290 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001291
Rusty Russell9e1d2142000-04-23 09:11:12 +00001292 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001293 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001294 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001295
Rusty Russell9e1d2142000-04-23 09:11:12 +00001296 memset(mask, 0xFF, sizeof(struct ipt_entry));
1297 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001298
1299 for (m = iptables_matches; m; m = m->next) {
1300 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001301 IPT_ALIGN(sizeof(struct ipt_entry_match))
1302 + m->userspacesize);
1303 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001304 }
1305
Rusty Russell73f72f52000-07-03 10:17:57 +00001306 memset(mptr, 0xFF,
1307 IPT_ALIGN(sizeof(struct ipt_entry_target))
1308 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001309
1310 return mask;
1311}
1312
Marc Bouchere6869a82000-03-20 06:03:29 +00001313static int
1314delete_entry(const ipt_chainlabel chain,
1315 struct ipt_entry *fw,
1316 unsigned int nsaddrs,
1317 const struct in_addr saddrs[],
1318 unsigned int ndaddrs,
1319 const struct in_addr daddrs[],
1320 int verbose,
1321 iptc_handle_t *handle)
1322{
1323 unsigned int i, j;
1324 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001325 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001326
Rusty Russell2e0a3212000-04-19 11:23:18 +00001327 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 for (i = 0; i < nsaddrs; i++) {
1329 fw->ip.src.s_addr = saddrs[i].s_addr;
1330 for (j = 0; j < ndaddrs; j++) {
1331 fw->ip.dst.s_addr = daddrs[j].s_addr;
1332 if (verbose)
1333 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001334 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 }
1336 }
1337 return ret;
1338}
1339
1340static int
1341check_packet(const ipt_chainlabel chain,
1342 struct ipt_entry *fw,
1343 unsigned int nsaddrs,
1344 const struct in_addr saddrs[],
1345 unsigned int ndaddrs,
1346 const struct in_addr daddrs[],
1347 int verbose,
1348 iptc_handle_t *handle)
1349{
1350 int ret = 1;
1351 unsigned int i, j;
1352 const char *msg;
1353
1354 for (i = 0; i < nsaddrs; i++) {
1355 fw->ip.src.s_addr = saddrs[i].s_addr;
1356 for (j = 0; j < ndaddrs; j++) {
1357 fw->ip.dst.s_addr = daddrs[j].s_addr;
1358 if (verbose)
1359 print_firewall_line(fw, *handle);
1360 msg = iptc_check_packet(chain, fw, handle);
1361 if (!msg) ret = 0;
1362 else printf("%s\n", msg);
1363 }
1364 }
1365
1366 return ret;
1367}
1368
1369static int
1370for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001371 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001372{
1373 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001374 const char *chain;
1375 char *chains;
1376 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001377
Rusty Russell9e1d2142000-04-23 09:11:12 +00001378 chain = iptc_first_chain(handle);
1379 while (chain) {
1380 chaincount++;
1381 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001382 }
1383
Rusty Russell9e1d2142000-04-23 09:11:12 +00001384 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1385 i = 0;
1386 chain = iptc_first_chain(handle);
1387 while (chain) {
1388 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1389 i++;
1390 chain = iptc_next_chain(handle);
1391 }
1392
1393 for (i = 0; i < chaincount; i++) {
1394 if (!builtinstoo
1395 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1396 *handle))
1397 continue;
1398 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1399 }
1400
1401 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001402 return ret;
1403}
1404
1405static int
1406flush_entries(const ipt_chainlabel chain, int verbose,
1407 iptc_handle_t *handle)
1408{
1409 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001410 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001411
1412 if (verbose)
1413 fprintf(stdout, "Flushing chain `%s'\n", chain);
1414 return iptc_flush_entries(chain, handle);
1415}
Marc Bouchere6869a82000-03-20 06:03:29 +00001416
1417static int
1418zero_entries(const ipt_chainlabel chain, int verbose,
1419 iptc_handle_t *handle)
1420{
1421 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001422 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001423
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 if (verbose)
1425 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1426 return iptc_zero_entries(chain, handle);
1427}
1428
1429static int
1430delete_chain(const ipt_chainlabel chain, int verbose,
1431 iptc_handle_t *handle)
1432{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001433 if (!chain)
1434 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001435
1436 if (verbose)
1437 fprintf(stdout, "Deleting chain `%s'\n", chain);
1438 return iptc_delete_chain(chain, handle);
1439}
1440
1441static int
1442list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1443 int expanded, int linenumbers, iptc_handle_t *handle)
1444{
1445 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001446 unsigned int format;
1447 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001448
1449 format = FMT_OPTIONS;
1450 if (!verbose)
1451 format |= FMT_NOCOUNTS;
1452 else
1453 format |= FMT_VIA;
1454
1455 if (numeric)
1456 format |= FMT_NUMERIC;
1457
1458 if (!expanded)
1459 format |= FMT_KILOMEGAGIGA;
1460
1461 if (linenumbers)
1462 format |= FMT_LINENUMBERS;
1463
Rusty Russell9e1d2142000-04-23 09:11:12 +00001464 for (this = iptc_first_chain(handle);
1465 this;
1466 this = iptc_next_chain(handle)) {
1467 const struct ipt_entry *i;
1468 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001469
Marc Bouchere6869a82000-03-20 06:03:29 +00001470 if (chain && strcmp(chain, this) != 0)
1471 continue;
1472
1473 if (found) printf("\n");
1474
1475 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001476 i = iptc_first_rule(this, handle);
1477
1478 num = 0;
1479 while (i) {
1480 print_firewall(i,
1481 iptc_get_target(i, handle),
1482 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001483 format,
1484 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001485 i = iptc_next_rule(i, handle);
1486 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001487 found = 1;
1488 }
1489
1490 errno = ENOENT;
1491 return found;
1492}
1493
1494static struct ipt_entry *
1495generate_entry(const struct ipt_entry *fw,
1496 struct iptables_match *matches,
1497 struct ipt_entry_target *target)
1498{
1499 unsigned int size;
1500 struct iptables_match *m;
1501 struct ipt_entry *e;
1502
1503 size = sizeof(struct ipt_entry);
1504 for (m = matches; m; m = m->next)
Rusty Russell228e98d2000-04-27 10:28:06 +00001505 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001506
Rusty Russell228e98d2000-04-27 10:28:06 +00001507 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001508 *e = *fw;
1509 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001510 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001511
1512 size = 0;
1513 for (m = matches; m; m = m->next) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001514 memcpy(e->elems + size, m->m, m->m->u.match_size);
1515 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001516 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001517 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001518
1519 return e;
1520}
1521
1522int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1523{
1524 struct ipt_entry fw, *e = NULL;
1525 int invert = 0;
1526 unsigned int nsaddrs = 0, ndaddrs = 0;
1527 struct in_addr *saddrs = NULL, *daddrs = NULL;
1528
1529 int c, verbose = 0;
1530 const char *chain = NULL;
1531 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1532 const char *policy = NULL, *newname = NULL;
1533 unsigned int rulenum = 0, options = 0, command = 0;
1534 int ret = 1;
1535 struct iptables_match *m;
1536 struct iptables_target *target = NULL;
1537 const char *jumpto = "";
1538 char *protocol = NULL;
1539
1540 memset(&fw, 0, sizeof(fw));
1541
1542 /* Suppress error messages: we may add new options if we
1543 demand-load a protocol. */
1544 opterr = 0;
1545
1546 while ((c = getopt_long(argc, argv,
1547 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1548 opts, NULL)) != -1) {
1549 switch (c) {
1550 /*
1551 * Command selection
1552 */
1553 case 'A':
1554 add_command(&command, CMD_APPEND, CMD_NONE,
1555 invert);
1556 chain = optarg;
1557 break;
1558
1559 case 'D':
1560 add_command(&command, CMD_DELETE, CMD_NONE,
1561 invert);
1562 chain = optarg;
1563 if (optind < argc && argv[optind][0] != '-'
1564 && argv[optind][0] != '!') {
1565 rulenum = parse_rulenumber(argv[optind++]);
1566 command = CMD_DELETE_NUM;
1567 }
1568 break;
1569
1570 case 'C':
1571 add_command(&command, CMD_CHECK, CMD_NONE,
1572 invert);
1573 chain = optarg;
1574 break;
1575
1576 case 'R':
1577 add_command(&command, CMD_REPLACE, CMD_NONE,
1578 invert);
1579 chain = optarg;
1580 if (optind < argc && argv[optind][0] != '-'
1581 && argv[optind][0] != '!')
1582 rulenum = parse_rulenumber(argv[optind++]);
1583 else
1584 exit_error(PARAMETER_PROBLEM,
1585 "-%c requires a rule number",
1586 cmd2char(CMD_REPLACE));
1587 break;
1588
1589 case 'I':
1590 add_command(&command, CMD_INSERT, CMD_NONE,
1591 invert);
1592 chain = optarg;
1593 if (optind < argc && argv[optind][0] != '-'
1594 && argv[optind][0] != '!')
1595 rulenum = parse_rulenumber(argv[optind++]);
1596 else rulenum = 1;
1597 break;
1598
1599 case 'L':
1600 add_command(&command, CMD_LIST, CMD_ZERO,
1601 invert);
1602 if (optarg) chain = optarg;
1603 else if (optind < argc && argv[optind][0] != '-'
1604 && argv[optind][0] != '!')
1605 chain = argv[optind++];
1606 break;
1607
1608 case 'F':
1609 add_command(&command, CMD_FLUSH, CMD_NONE,
1610 invert);
1611 if (optarg) chain = optarg;
1612 else if (optind < argc && argv[optind][0] != '-'
1613 && argv[optind][0] != '!')
1614 chain = argv[optind++];
1615 break;
1616
1617 case 'Z':
1618 add_command(&command, CMD_ZERO, CMD_LIST,
1619 invert);
1620 if (optarg) chain = optarg;
1621 else if (optind < argc && argv[optind][0] != '-'
1622 && argv[optind][0] != '!')
1623 chain = argv[optind++];
1624 break;
1625
1626 case 'N':
1627 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1628 invert);
1629 chain = optarg;
1630 break;
1631
1632 case 'X':
1633 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1634 invert);
1635 if (optarg) chain = optarg;
1636 else if (optind < argc && argv[optind][0] != '-'
1637 && argv[optind][0] != '!')
1638 chain = argv[optind++];
1639 break;
1640
1641 case 'E':
1642 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1643 invert);
1644 chain = optarg;
1645 if (optind < argc && argv[optind][0] != '-'
1646 && argv[optind][0] != '!')
1647 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001648 else
1649 exit_error(PARAMETER_PROBLEM,
1650 "-%c requires old-chain-name and "
1651 "new-chain-name",
1652 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001653 break;
1654
1655 case 'P':
1656 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1657 invert);
1658 chain = optarg;
1659 if (optind < argc && argv[optind][0] != '-'
1660 && argv[optind][0] != '!')
1661 policy = argv[optind++];
1662 else
1663 exit_error(PARAMETER_PROBLEM,
1664 "-%c requires a chain and a policy",
1665 cmd2char(CMD_SET_POLICY));
1666 break;
1667
1668 case 'h':
1669 if (!optarg)
1670 optarg = argv[optind];
1671
Rusty Russell2e0a3212000-04-19 11:23:18 +00001672 /* iptables -p icmp -h */
1673 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001674 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001675
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 exit_printhelp();
1677
1678 /*
1679 * Option selection
1680 */
1681 case 'p':
1682 if (check_inverse(optarg, &invert))
1683 optind++;
1684 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1685 invert);
1686
1687 /* Canonicalize into lower case */
1688 for (protocol = argv[optind-1]; *protocol; protocol++)
1689 *protocol = tolower(*protocol);
1690
1691 protocol = argv[optind-1];
1692 fw.ip.proto = parse_protocol(protocol);
1693
1694 if (fw.ip.proto == 0
1695 && (fw.ip.invflags & IPT_INV_PROTO))
1696 exit_error(PARAMETER_PROBLEM,
1697 "rule would never match protocol");
1698 fw.nfcache |= NFC_IP_PROTO;
1699 break;
1700
1701 case 's':
1702 if (check_inverse(optarg, &invert))
1703 optind++;
1704 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1705 invert);
1706 shostnetworkmask = argv[optind-1];
1707 fw.nfcache |= NFC_IP_SRC;
1708 break;
1709
1710 case 'd':
1711 if (check_inverse(optarg, &invert))
1712 optind++;
1713 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1714 invert);
1715 dhostnetworkmask = argv[optind-1];
1716 fw.nfcache |= NFC_IP_DST;
1717 break;
1718
1719 case 'j':
1720 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1721 invert);
1722 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001723 /* TRY_LOAD (may be chain name) */
1724 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001725
1726 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001727 size_t size;
1728
Rusty Russell73f72f52000-07-03 10:17:57 +00001729 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1730 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001731
Rusty Russell2e0a3212000-04-19 11:23:18 +00001732 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001733 target->t->u.target_size = size;
1734 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001735 target->init(target->t, &fw.nfcache);
1736 }
1737 break;
1738
1739
1740 case 'i':
1741 if (check_inverse(optarg, &invert))
1742 optind++;
1743 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1744 invert);
1745 parse_interface(argv[optind-1],
1746 fw.ip.iniface,
1747 fw.ip.iniface_mask);
1748 fw.nfcache |= NFC_IP_IF_IN;
1749 break;
1750
1751 case 'o':
1752 if (check_inverse(optarg, &invert))
1753 optind++;
1754 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1755 invert);
1756 parse_interface(argv[optind-1],
1757 fw.ip.outiface,
1758 fw.ip.outiface_mask);
1759 fw.nfcache |= NFC_IP_IF_OUT;
1760 break;
1761
1762 case 'f':
1763 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1764 invert);
1765 fw.ip.flags |= IPT_F_FRAG;
1766 fw.nfcache |= NFC_IP_FRAG;
1767 break;
1768
1769 case 'v':
1770 if (!verbose)
1771 set_option(&options, OPT_VERBOSE,
1772 &fw.ip.invflags, invert);
1773 verbose++;
1774 break;
1775
Rusty Russell52a51492000-05-02 16:44:29 +00001776 case 'm': {
1777 size_t size;
1778
Marc Bouchere6869a82000-03-20 06:03:29 +00001779 if (invert)
1780 exit_error(PARAMETER_PROBLEM,
1781 "unexpected ! flag before --match");
1782
Rusty Russell52a51492000-05-02 16:44:29 +00001783 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001784 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1785 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001786 m->m = fw_calloc(1, size);
1787 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001788 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001789 m->init(m->m, &fw.nfcache);
1790 }
1791 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792
1793 case 'n':
1794 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1795 invert);
1796 break;
1797
1798 case 't':
1799 if (invert)
1800 exit_error(PARAMETER_PROBLEM,
1801 "unexpected ! flag before --table");
1802 *table = argv[optind-1];
1803 break;
1804
1805 case 'x':
1806 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1807 invert);
1808 break;
1809
1810 case 'V':
1811 if (invert)
1812 printf("Not %s ;-)\n", program_version);
1813 else
1814 printf("%s v%s\n",
1815 program_name, program_version);
1816 exit(0);
1817
1818 case '0':
1819 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1820 invert);
1821 break;
1822
1823 case 1: /* non option */
1824 if (optarg[0] == '!' && optarg[1] == '\0') {
1825 if (invert)
1826 exit_error(PARAMETER_PROBLEM,
1827 "multiple consecutive ! not"
1828 " allowed");
1829 invert = TRUE;
1830 optarg[0] = '\0';
1831 continue;
1832 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001833 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001834 exit_tryhelp(2);
1835
1836 default:
1837 /* FIXME: This scheme doesn't allow two of the same
1838 matches --RR */
1839 if (!target
1840 || !(target->parse(c - target->option_offset,
1841 argv, invert,
1842 &target->tflags,
1843 &fw, &target->t))) {
1844 for (m = iptables_matches; m; m = m->next) {
1845 if (m->parse(c - m->option_offset,
1846 argv, invert,
1847 &m->mflags,
1848 &fw,
1849 &fw.nfcache,
1850 &m->m))
1851 break;
1852 }
1853
1854 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00001855 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001856 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001857 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00001858 && !find_proto(protocol, DONT_LOAD,
1859 options&OPT_NUMERIC)
1860 && (m = find_proto(protocol, TRY_LOAD,
1861 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00001863 size_t size;
1864
Rusty Russell73f72f52000-07-03 10:17:57 +00001865 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1866 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001867
Rusty Russell2e0a3212000-04-19 11:23:18 +00001868 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001869 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001870 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001871 m->init(m->m, &fw.nfcache);
1872
1873 optind--;
1874 continue;
1875 }
1876 if (!m)
1877 exit_error(PARAMETER_PROBLEM,
1878 "Unknown arg `%s'",
1879 argv[optind-1]);
1880 }
1881 }
1882 invert = FALSE;
1883 }
1884
1885 for (m = iptables_matches; m; m = m->next)
1886 m->final_check(m->mflags);
1887 if (target)
1888 target->final_check(target->tflags);
1889
1890 /* Fix me: must put inverse options checking here --MN */
1891
1892 if (optind < argc)
1893 exit_error(PARAMETER_PROBLEM,
1894 "unknown arguments found on commandline");
1895 if (!command)
1896 exit_error(PARAMETER_PROBLEM, "no command specified");
1897 if (invert)
1898 exit_error(PARAMETER_PROBLEM,
1899 "nothing appropriate following !");
1900
Marc Boucher744bd022000-04-22 22:36:10 +00001901 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1902 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001903 if (!(options & OPT_DESTINATION))
1904 dhostnetworkmask = "0.0.0.0/0";
1905 if (!(options & OPT_SOURCE))
1906 shostnetworkmask = "0.0.0.0/0";
1907 }
1908
1909 if (shostnetworkmask)
1910 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1911 &(fw.ip.smsk), &nsaddrs);
1912
1913 if (dhostnetworkmask)
1914 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1915 &(fw.ip.dmsk), &ndaddrs);
1916
1917 if ((nsaddrs > 1 || ndaddrs > 1) &&
1918 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1919 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1920 " source or destination IP addresses");
1921
1922 if (command == CMD_CHECK && fw.ip.invflags != 0)
1923 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1924 cmd2char(CMD_CHECK));
1925
1926 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1927 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1928 "specify a unique address");
1929
1930 generic_opt_check(command, options);
1931
1932 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1933 exit_error(PARAMETER_PROBLEM,
1934 "chain name `%s' too long (must be under %i chars)",
1935 chain, IPT_FUNCTION_MAXNAMELEN);
1936
1937 *handle = iptc_init(*table);
1938 if (!*handle)
1939 exit_error(VERSION_PROBLEM,
1940 "can't initialize iptables table `%s': %s",
1941 *table, iptc_strerror(errno));
1942
Marc Boucher744bd022000-04-22 22:36:10 +00001943 if (command == CMD_CHECK
1944 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001945 || command == CMD_DELETE
1946 || command == CMD_INSERT
1947 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001948 if (strcmp(chain, "PREROUTING") == 0
1949 || strcmp(chain, "INPUT") == 0) {
1950 /* -o not valid with incoming packets. */
1951 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001952 exit_error(PARAMETER_PROBLEM,
1953 "Can't use -%c with %s\n",
1954 opt2char(OPT_VIANAMEOUT),
1955 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001956 /* -i required with -C */
1957 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1958 exit_error(PARAMETER_PROBLEM,
1959 "Need -%c with %s\n",
1960 opt2char(OPT_VIANAMEIN),
1961 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001962 }
1963
Rusty Russella4860fd2000-06-17 16:13:02 +00001964 if (strcmp(chain, "POSTROUTING") == 0
1965 || strcmp(chain, "OUTPUT") == 0) {
1966 /* -i not valid with outgoing packets */
1967 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 exit_error(PARAMETER_PROBLEM,
1969 "Can't use -%c with %s\n",
1970 opt2char(OPT_VIANAMEIN),
1971 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00001972 /* -o required with -C */
1973 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1974 exit_error(PARAMETER_PROBLEM,
1975 "Need -%c with %s\n",
1976 opt2char(OPT_VIANAMEOUT),
1977 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001978 }
1979
1980 if (target && iptc_is_chain(jumpto, *handle)) {
1981 printf("Warning: using chain %s, not extension\n",
1982 jumpto);
1983
1984 target = NULL;
1985 }
1986
1987 /* If they didn't specify a target, or it's a chain
1988 name, use standard. */
1989 if (!target
1990 && (strlen(jumpto) == 0
1991 || iptc_is_chain(jumpto, *handle))) {
1992 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001993
Rusty Russell52a51492000-05-02 16:44:29 +00001994 target = find_target(IPT_STANDARD_TARGET,
1995 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001996
1997 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001998 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001999 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002000 target->t->u.target_size = size;
2001 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002002 target->init(target->t, &fw.nfcache);
2003 }
2004
Rusty Russell7e53bf92000-03-20 07:03:28 +00002005 if (!target) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 struct ipt_entry_target unknown_target;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002007
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 /* Don't know it. Must be extension with no
2009 options? */
Rusty Russell228e98d2000-04-27 10:28:06 +00002010 unknown_target.u.target_size = sizeof(unknown_target);
2011 strcpy(unknown_target.u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
2013 e = generate_entry(&fw, iptables_matches,
2014 &unknown_target);
2015 } else {
2016 e = generate_entry(&fw, iptables_matches, target->t);
2017 }
2018 }
2019
2020 switch (command) {
2021 case CMD_APPEND:
2022 ret = append_entry(chain, e,
2023 nsaddrs, saddrs, ndaddrs, daddrs,
2024 options&OPT_VERBOSE,
2025 handle);
2026 break;
2027 case CMD_CHECK:
2028 ret = check_packet(chain, e,
2029 nsaddrs, saddrs, ndaddrs, daddrs,
2030 options&OPT_VERBOSE, handle);
2031 break;
2032 case CMD_DELETE:
2033 ret = delete_entry(chain, e,
2034 nsaddrs, saddrs, ndaddrs, daddrs,
2035 options&OPT_VERBOSE,
2036 handle);
2037 break;
2038 case CMD_DELETE_NUM:
2039 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2040 break;
2041 case CMD_REPLACE:
2042 ret = replace_entry(chain, e, rulenum - 1,
2043 saddrs, daddrs, options&OPT_VERBOSE,
2044 handle);
2045 break;
2046 case CMD_INSERT:
2047 ret = insert_entry(chain, e, rulenum - 1,
2048 nsaddrs, saddrs, ndaddrs, daddrs,
2049 options&OPT_VERBOSE,
2050 handle);
2051 break;
2052 case CMD_LIST:
2053 ret = list_entries(chain,
2054 options&OPT_VERBOSE,
2055 options&OPT_NUMERIC,
2056 options&OPT_EXPANDED,
2057 options&OPT_LINENUMBERS,
2058 handle);
2059 break;
2060 case CMD_FLUSH:
2061 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2062 break;
2063 case CMD_ZERO:
2064 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2065 break;
2066 case CMD_LIST|CMD_ZERO:
2067 ret = list_entries(chain,
2068 options&OPT_VERBOSE,
2069 options&OPT_NUMERIC,
2070 options&OPT_EXPANDED,
2071 options&OPT_LINENUMBERS,
2072 handle);
2073 if (ret)
2074 ret = zero_entries(chain,
2075 options&OPT_VERBOSE, handle);
2076 break;
2077 case CMD_NEW_CHAIN:
2078 ret = iptc_create_chain(chain, handle);
2079 break;
2080 case CMD_DELETE_CHAIN:
2081 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2082 break;
2083 case CMD_RENAME_CHAIN:
2084 ret = iptc_rename_chain(chain, newname, handle);
2085 break;
2086 case CMD_SET_POLICY:
2087 ret = iptc_set_policy(chain, policy, handle);
2088 break;
2089 default:
2090 /* We should never reach this... */
2091 exit_tryhelp(2);
2092 }
2093
2094 if (verbose > 1)
2095 dump_entries(*handle);
2096
2097 return ret;
2098}