blob: 5e7db06fdcbf5a4d8f47d86ad42551f0ea5cbb62 [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>
Harald Welte82dd2ec2000-12-19 05:18:15 +000031#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000032#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000033#include <fcntl.h>
34#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035
36#ifndef TRUE
37#define TRUE 1
38#endif
39#ifndef FALSE
40#define FALSE 0
41#endif
42
43#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte82dd2ec2000-12-19 05:18:15 +000047#ifndef PROC_SYS_MODPROBE
48#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
49#endif
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051#define FMT_NUMERIC 0x0001
52#define FMT_NOCOUNTS 0x0002
53#define FMT_KILOMEGAGIGA 0x0004
54#define FMT_OPTIONS 0x0008
55#define FMT_NOTABLE 0x0010
56#define FMT_NOTARGET 0x0020
57#define FMT_VIA 0x0040
58#define FMT_NONEWLINE 0x0080
59#define FMT_LINENUMBERS 0x0100
60
61#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
62 | FMT_NUMERIC | FMT_NOTABLE)
63#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
64
65
66#define CMD_NONE 0x0000U
67#define CMD_INSERT 0x0001U
68#define CMD_DELETE 0x0002U
69#define CMD_DELETE_NUM 0x0004U
70#define CMD_REPLACE 0x0008U
71#define CMD_APPEND 0x0010U
72#define CMD_LIST 0x0020U
73#define CMD_FLUSH 0x0040U
74#define CMD_ZERO 0x0080U
75#define CMD_NEW_CHAIN 0x0100U
76#define CMD_DELETE_CHAIN 0x0200U
77#define CMD_SET_POLICY 0x0400U
78#define CMD_CHECK 0x0800U
79#define CMD_RENAME_CHAIN 0x1000U
80#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
82 'N', 'X', 'P', 'C', 'E' };
83
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
104 { "append", 1, 0, 'A' },
105 { "delete", 1, 0, 'D' },
106 { "insert", 1, 0, 'I' },
107 { "replace", 1, 0, 'R' },
108 { "list", 2, 0, 'L' },
109 { "flush", 2, 0, 'F' },
110 { "zero", 2, 0, 'Z' },
111 { "check", 1, 0, 'C' },
112 { "new-chain", 1, 0, 'N' },
113 { "delete-chain", 2, 0, 'X' },
114 { "rename-chain", 2, 0, 'E' },
115 { "policy", 1, 0, 'P' },
116 { "source", 1, 0, 's' },
117 { "destination", 1, 0, 'd' },
118 { "src", 1, 0, 's' }, /* synonym */
119 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000120 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 { "in-interface", 1, 0, 'i' },
122 { "jump", 1, 0, 'j' },
123 { "table", 1, 0, 't' },
124 { "match", 1, 0, 'm' },
125 { "numeric", 0, 0, 'n' },
126 { "out-interface", 1, 0, 'o' },
127 { "verbose", 0, 0, 'v' },
128 { "exact", 0, 0, 'x' },
129 { "fragments", 0, 0, 'f' },
130 { "version", 0, 0, 'V' },
131 { "help", 2, 0, 'h' },
132 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000133 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000134 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 { 0 }
136};
137
Rusty Russell4e242f82000-05-31 06:33:50 +0000138#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000139struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000140ipt_get_target(struct ipt_entry *e)
141{
142 return (void *)e + e->target_offset;
143}
144#endif
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
Rusty Russell2e0a3212000-04-19 11:23:18 +0000195/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000196struct iptables_match *iptables_matches = NULL;
197struct iptables_target *iptables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries(const iptc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000209/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000210/* defined in netinet/in.h */
211#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
223 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "all", 0 },
227};
228
229static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000230proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int i;
233
Rusty Russell28381a42000-05-10 00:19:50 +0000234 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247struct in_addr *
248dotted_to_addr(const char *dotted)
249{
250 static struct in_addr addr;
251 unsigned char *addrp;
252 char *p, *q;
253 int onebyte, i;
254 char buf[20];
255
256 /* copy dotted string, because we need to modify it */
257 strncpy(buf, dotted, sizeof(buf) - 1);
258 addrp = (unsigned char *) &(addr.s_addr);
259
260 p = buf;
261 for (i = 0; i < 3; i++) {
262 if ((q = strchr(p, '.')) == NULL)
263 return (struct in_addr *) NULL;
264
265 *q = '\0';
266 if ((onebyte = string_to_number(p, 0, 255)) == -1)
267 return (struct in_addr *) NULL;
268
269 addrp[i] = (unsigned char) onebyte;
270 p = q + 1;
271 }
272
273 /* we've checked 3 bytes, now we check the last one */
274 if ((onebyte = string_to_number(p, 0, 255)) == -1)
275 return (struct in_addr *) NULL;
276
277 addrp[3] = (unsigned char) onebyte;
278
279 return &addr;
280}
281
282static struct in_addr *
283network_to_addr(const char *name)
284{
285 struct netent *net;
286 static struct in_addr addr;
287
288 if ((net = getnetbyname(name)) != NULL) {
289 if (net->n_addrtype != AF_INET)
290 return (struct in_addr *) NULL;
291 addr.s_addr = htonl((unsigned long) net->n_net);
292 return &addr;
293 }
294
295 return (struct in_addr *) NULL;
296}
297
298static void
299inaddrcpy(struct in_addr *dst, struct in_addr *src)
300{
301 /* memcpy(dst, src, sizeof(struct in_addr)); */
302 dst->s_addr = src->s_addr;
303}
304
305void
306exit_error(enum exittype status, char *msg, ...)
307{
308 va_list args;
309
310 va_start(args, msg);
311 fprintf(stderr, "%s v%s: ", program_name, program_version);
312 vfprintf(stderr, msg, args);
313 va_end(args);
314 fprintf(stderr, "\n");
315 if (status == PARAMETER_PROBLEM)
316 exit_tryhelp(status);
317 if (status == VERSION_PROBLEM)
318 fprintf(stderr,
319 "Perhaps iptables or your kernel needs to be upgraded.\n");
320 exit(status);
321}
322
323void
324exit_tryhelp(int status)
325{
326 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
327 program_name, program_name );
328 exit(status);
329}
330
331void
332exit_printhelp(void)
333{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000334 struct iptables_match *m = NULL;
335 struct iptables_target *t = NULL;
336
Marc Bouchere6869a82000-03-20 06:03:29 +0000337 printf("%s v%s\n\n"
338"Usage: %s -[ADC] chain rule-specification [options]\n"
339" %s -[RI] chain rulenum rule-specification [options]\n"
340" %s -D chain rulenum [options]\n"
341" %s -[LFZ] [chain] [options]\n"
342" %s -[NX] chain\n"
343" %s -E old-chain-name new-chain-name\n"
344" %s -P chain target [options]\n"
345" %s -h (print this help information)\n\n",
346 program_name, program_version, program_name, program_name,
347 program_name, program_name, program_name, program_name,
348 program_name, program_name);
349
350 printf(
351"Commands:\n"
352"Either long or short options are allowed.\n"
353" --append -A chain Append to chain\n"
354" --delete -D chain Delete matching rule from chain\n"
355" --delete -D chain rulenum\n"
356" Delete rule rulenum (1 = first) from chain\n"
357" --insert -I chain [rulenum]\n"
358" Insert in chain as rulenum (default 1=first)\n"
359" --replace -R chain rulenum\n"
360" Replace rule rulenum (1 = first) in chain\n"
361" --list -L [chain] List the rules in a chain or all chains\n"
362" --flush -F [chain] Delete all rules in chain or all chains\n"
363" --zero -Z [chain] Zero counters in chain or all chains\n"
364" --check -C chain Test this packet on chain\n"
365" --new -N chain Create a new user-defined chain\n"
366" --delete-chain\n"
367" -X [chain] Delete a user-defined chain\n"
368" --policy -P chain target\n"
369" Change policy on chain to target\n"
370" --rename-chain\n"
371" -E old-chain new-chain\n"
372" Change chain name, (moving any references)\n"
373
374"Options:\n"
375" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
376" --source -s [!] address[/mask]\n"
377" source specification\n"
378" --destination -d [!] address[/mask]\n"
379" destination specification\n"
380" --in-interface -i [!] input name[+]\n"
381" network interface name ([+] for wildcard)\n"
382" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000383" target for rule (may load target extension)\n"
384" --match -m match\n"
385" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000386" --numeric -n numeric output of addresses and ports\n"
387" --out-interface -o [!] output name[+]\n"
388" network interface name ([+] for wildcard)\n"
389" --table -t table table to manipulate (default: `filter')\n"
390" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000391" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000392" --exact -x expand numbers (display exact values)\n"
393"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000394" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000395" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000396"[!] --version -V print package version.\n");
397
Rusty Russell363112d2000-08-11 13:49:26 +0000398 /* Print out any special helps. A user might like to be able
399 to add a --help to the commandline, and see expected
400 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000401 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000403 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000404 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000405 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000406 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000407 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000408 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000409 exit(0);
410}
411
412static void
413generic_opt_check(int command, int options)
414{
415 int i, j, legal = 0;
416
417 /* Check that commands are valid with options. Complicated by the
418 * fact that if an option is legal with *any* command given, it is
419 * legal overall (ie. -z and -l).
420 */
421 for (i = 0; i < NUMBER_OF_OPT; i++) {
422 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
423
424 for (j = 0; j < NUMBER_OF_CMD; j++) {
425 if (!(command & (1<<j)))
426 continue;
427
428 if (!(options & (1<<i))) {
429 if (commands_v_options[j][i] == '+')
430 exit_error(PARAMETER_PROBLEM,
431 "You need to supply the `-%c' "
432 "option for this command\n",
433 optflags[i]);
434 } else {
435 if (commands_v_options[j][i] != 'x')
436 legal = 1;
437 else if (legal == 0)
438 legal = -1;
439 }
440 }
441 if (legal == -1)
442 exit_error(PARAMETER_PROBLEM,
443 "Illegal option `-%c' with this command\n",
444 optflags[i]);
445 }
446}
447
448static char
449opt2char(int option)
450{
451 const char *ptr;
452 for (ptr = optflags; option > 1; option >>= 1, ptr++);
453
454 return *ptr;
455}
456
457static char
458cmd2char(int option)
459{
460 const char *ptr;
461 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
462
463 return *ptr;
464}
465
466static void
467add_command(int *cmd, const int newcmd, const int othercmds, int invert)
468{
469 if (invert)
470 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
471 if (*cmd & (~othercmds))
472 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
473 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
474 *cmd |= newcmd;
475}
476
477int
478check_inverse(const char option[], int *invert)
479{
480 if (option && strcmp(option, "!") == 0) {
481 if (*invert)
482 exit_error(PARAMETER_PROBLEM,
483 "Multiple `!' flags not allowed");
484
485 *invert = TRUE;
486 return TRUE;
487 }
488 return FALSE;
489}
490
491static void *
492fw_calloc(size_t count, size_t size)
493{
494 void *p;
495
496 if ((p = calloc(count, size)) == NULL) {
497 perror("iptables: calloc failed");
498 exit(1);
499 }
500 return p;
501}
502
503static void *
504fw_malloc(size_t size)
505{
506 void *p;
507
508 if ((p = malloc(size)) == NULL) {
509 perror("iptables: malloc failed");
510 exit(1);
511 }
512 return p;
513}
514
515static struct in_addr *
516host_to_addr(const char *name, unsigned int *naddr)
517{
518 struct hostent *host;
519 struct in_addr *addr;
520 unsigned int i;
521
522 *naddr = 0;
523 if ((host = gethostbyname(name)) != NULL) {
524 if (host->h_addrtype != AF_INET ||
525 host->h_length != sizeof(struct in_addr))
526 return (struct in_addr *) NULL;
527
528 while (host->h_addr_list[*naddr] != (char *) NULL)
529 (*naddr)++;
530 addr = fw_calloc(*naddr, sizeof(struct in_addr));
531 for (i = 0; i < *naddr; i++)
532 inaddrcpy(&(addr[i]),
533 (struct in_addr *) host->h_addr_list[i]);
534 return addr;
535 }
536
537 return (struct in_addr *) NULL;
538}
539
540static char *
541addr_to_host(const struct in_addr *addr)
542{
543 struct hostent *host;
544
545 if ((host = gethostbyaddr((char *) addr,
546 sizeof(struct in_addr), AF_INET)) != NULL)
547 return (char *) host->h_name;
548
549 return (char *) NULL;
550}
551
552/*
553 * All functions starting with "parse" should succeed, otherwise
554 * the program fails.
555 * Most routines return pointers to static data that may change
556 * between calls to the same or other routines with a few exceptions:
557 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
558 * return global static data.
559*/
560
561static struct in_addr *
562parse_hostnetwork(const char *name, unsigned int *naddrs)
563{
564 struct in_addr *addrp, *addrptmp;
565
566 if ((addrptmp = dotted_to_addr(name)) != NULL ||
567 (addrptmp = network_to_addr(name)) != NULL) {
568 addrp = fw_malloc(sizeof(struct in_addr));
569 inaddrcpy(addrp, addrptmp);
570 *naddrs = 1;
571 return addrp;
572 }
573 if ((addrp = host_to_addr(name, naddrs)) != NULL)
574 return addrp;
575
576 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
577}
578
579static struct in_addr *
580parse_mask(char *mask)
581{
582 static struct in_addr maskaddr;
583 struct in_addr *addrp;
584 int bits;
585
586 if (mask == NULL) {
587 /* no mask at all defaults to 32 bits */
588 maskaddr.s_addr = 0xFFFFFFFF;
589 return &maskaddr;
590 }
591 if ((addrp = dotted_to_addr(mask)) != NULL)
592 /* dotted_to_addr already returns a network byte order addr */
593 return addrp;
594 if ((bits = string_to_number(mask, 0, 32)) == -1)
595 exit_error(PARAMETER_PROBLEM,
596 "invalid mask `%s' specified", mask);
597 if (bits != 0) {
598 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
599 return &maskaddr;
600 }
601
602 maskaddr.s_addr = 0L;
603 return &maskaddr;
604}
605
606static void
607parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
608 struct in_addr *maskp, unsigned int *naddrs)
609{
610 struct in_addr *addrp;
611 char buf[256];
612 char *p;
613 int i, j, k, n;
614
615 strncpy(buf, name, sizeof(buf) - 1);
616 if ((p = strrchr(buf, '/')) != NULL) {
617 *p = '\0';
618 addrp = parse_mask(p + 1);
619 } else
620 addrp = parse_mask(NULL);
621 inaddrcpy(maskp, addrp);
622
623 /* if a null mask is given, the name is ignored, like in "any/0" */
624 if (maskp->s_addr == 0L)
625 strcpy(buf, "0.0.0.0");
626
627 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
628 n = *naddrs;
629 for (i = 0, j = 0; i < n; i++) {
630 addrp[j++].s_addr &= maskp->s_addr;
631 for (k = 0; k < j - 1; k++) {
632 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
633 (*naddrs)--;
634 j--;
635 break;
636 }
637 }
638 }
639}
640
641struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000642find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000643{
644 struct iptables_match *ptr;
645
646 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
647 if (strcmp(name, ptr->name) == 0)
648 break;
649 }
650
Rusty Russell52a51492000-05-02 16:44:29 +0000651 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000652 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
653 + strlen(name)];
654 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000655 if (dlopen(path, RTLD_NOW)) {
656 /* Found library. If it didn't register itself,
657 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000658 ptr = find_match(name, DONT_LOAD);
659
Rusty Russell9e1d2142000-04-23 09:11:12 +0000660 if (!ptr)
661 exit_error(PARAMETER_PROBLEM,
662 "Couldn't load match `%s'\n",
663 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000664 } else if (tryload == LOAD_MUST_SUCCEED)
665 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000666 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000667 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000668 }
669
Harald Welteae1ff9f2000-12-01 14:26:20 +0000670 if (ptr)
671 ptr->used = 1;
672
Marc Bouchere6869a82000-03-20 06:03:29 +0000673 return ptr;
674}
675
Rusty Russell28381a42000-05-10 00:19:50 +0000676/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
677static struct iptables_match *
678find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
679{
680 int proto;
681
682 proto = string_to_number(pname, 0, 255);
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000683 if (proto != -1)
Rusty Russell28381a42000-05-10 00:19:50 +0000684 return find_match(proto_to_name(proto, nolookup), tryload);
685
686 return find_match(pname, tryload);
687}
688
Marc Bouchere6869a82000-03-20 06:03:29 +0000689static u_int16_t
690parse_protocol(const char *s)
691{
Rusty Russell28381a42000-05-10 00:19:50 +0000692 int proto = string_to_number(s, 0, 255);
Marc Bouchere6869a82000-03-20 06:03:29 +0000693
694 if (proto == -1) {
695 struct protoent *pent;
696
697 if ((pent = getprotobyname(s)))
698 proto = pent->p_proto;
699 else {
700 unsigned int i;
701 for (i = 0;
702 i < sizeof(chain_protos)/sizeof(struct pprot);
703 i++) {
704 if (strcmp(s, chain_protos[i].name) == 0) {
705 proto = chain_protos[i].num;
706 break;
707 }
708 }
709 if (i == sizeof(chain_protos)/sizeof(struct pprot))
710 exit_error(PARAMETER_PROBLEM,
711 "unknown protocol `%s' specified",
712 s);
713 }
714 }
715
716 return (u_int16_t)proto;
717}
718
719static void
720parse_interface(const char *arg, char *vianame, unsigned char *mask)
721{
722 int vialen = strlen(arg);
723 unsigned int i;
724
725 memset(mask, 0, IFNAMSIZ);
726 memset(vianame, 0, IFNAMSIZ);
727
728 if (vialen + 1 > IFNAMSIZ)
729 exit_error(PARAMETER_PROBLEM,
730 "interface name `%s' must be shorter than IFNAMSIZ"
731 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000732
Marc Bouchere6869a82000-03-20 06:03:29 +0000733 strcpy(vianame, arg);
734 if (vialen == 0)
735 memset(mask, 0, IFNAMSIZ);
736 else if (vianame[vialen - 1] == '+') {
737 memset(mask, 0xFF, vialen - 1);
738 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000739 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000740 } else {
741 /* Include nul-terminator in match */
742 memset(mask, 0xFF, vialen + 1);
743 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000744 for (i = 0; vianame[i]; i++) {
745 if (!isalnum(vianame[i]) && vianame[i] != '_') {
746 printf("Warning: wierd character in interface"
747 " `%s' (No aliases, :, ! or *).\n",
748 vianame);
749 break;
750 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 }
752 }
753}
754
755/* Can't be zero. */
756static int
757parse_rulenumber(const char *rule)
758{
759 int rulenum = string_to_number(rule, 1, INT_MAX);
760
761 if (rulenum == -1)
762 exit_error(PARAMETER_PROBLEM,
763 "Invalid rule number `%s'", rule);
764
765 return rulenum;
766}
767
768static const char *
769parse_target(const char *targetname)
770{
771 const char *ptr;
772
773 if (strlen(targetname) < 1)
774 exit_error(PARAMETER_PROBLEM,
775 "Invalid target name (too short)");
776
777 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
778 exit_error(PARAMETER_PROBLEM,
779 "Invalid target name `%s' (%i chars max)",
780 targetname, sizeof(ipt_chainlabel)-1);
781
782 for (ptr = targetname; *ptr; ptr++)
783 if (isspace(*ptr))
784 exit_error(PARAMETER_PROBLEM,
785 "Invalid target name `%s'", targetname);
786 return targetname;
787}
788
789static char *
790addr_to_network(const struct in_addr *addr)
791{
792 struct netent *net;
793
794 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
795 return (char *) net->n_name;
796
797 return (char *) NULL;
798}
799
800char *
801addr_to_dotted(const struct in_addr *addrp)
802{
803 static char buf[20];
804 const unsigned char *bytep;
805
806 bytep = (const unsigned char *) &(addrp->s_addr);
807 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
808 return buf;
809}
810static char *
811addr_to_anyname(const struct in_addr *addr)
812{
813 char *name;
814
815 if ((name = addr_to_host(addr)) != NULL ||
816 (name = addr_to_network(addr)) != NULL)
817 return name;
818
819 return addr_to_dotted(addr);
820}
821
822static char *
823mask_to_dotted(const struct in_addr *mask)
824{
825 int i;
826 static char buf[20];
827 u_int32_t maskaddr, bits;
828
829 maskaddr = ntohl(mask->s_addr);
830
831 if (maskaddr == 0xFFFFFFFFL)
832 /* we don't want to see "/32" */
833 return "";
834
835 i = 32;
836 bits = 0xFFFFFFFEL;
837 while (--i >= 0 && maskaddr != bits)
838 bits <<= 1;
839 if (i >= 0)
840 sprintf(buf, "/%d", i);
841 else
842 /* mask was not a decent combination of 1's and 0's */
843 sprintf(buf, "/%s", addr_to_dotted(mask));
844
845 return buf;
846}
847
848int
849string_to_number(const char *s, int min, int max)
850{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000851 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000852 char *end;
853
854 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000855 errno = 0;
856 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000857 if (*end == '\0' && end != s) {
858 /* we parsed a number, let's see if we want this */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000859 if (errno != ERANGE && min <= number && number <= max)
Marc Bouchere6869a82000-03-20 06:03:29 +0000860 return number;
861 }
862 return -1;
863}
864
865static void
866set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
867 int invert)
868{
869 if (*options & option)
870 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
871 opt2char(option));
872 *options |= option;
873
874 if (invert) {
875 unsigned int i;
876 for (i = 0; 1 << i != option; i++);
877
878 if (!inverse_for_options[i])
879 exit_error(PARAMETER_PROBLEM,
880 "cannot have ! before -%c",
881 opt2char(option));
882 *invflg |= inverse_for_options[i];
883 }
884}
885
886struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000887find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000888{
889 struct iptables_target *ptr;
890
891 /* Standard target? */
892 if (strcmp(name, "") == 0
893 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
894 || strcmp(name, IPTC_LABEL_DROP) == 0
895 || strcmp(name, IPTC_LABEL_QUEUE) == 0
896 || strcmp(name, IPTC_LABEL_RETURN) == 0)
897 name = "standard";
898
899 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
900 if (strcmp(name, ptr->name) == 0)
901 break;
902 }
903
Rusty Russell52a51492000-05-02 16:44:29 +0000904 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000905 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
906 + strlen(name)];
907 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000908 if (dlopen(path, RTLD_NOW)) {
909 /* Found library. If it didn't register itself,
910 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000911 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000912 if (!ptr)
913 exit_error(PARAMETER_PROBLEM,
914 "Couldn't load target `%s'\n",
915 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000916 } else if (tryload == LOAD_MUST_SUCCEED)
917 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000918 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000919 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000920 }
921
Harald Welteae1ff9f2000-12-01 14:26:20 +0000922 if (ptr)
923 ptr->used = 1;
924
Marc Bouchere6869a82000-03-20 06:03:29 +0000925 return ptr;
926}
927
928static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000929merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000930 unsigned int *option_offset)
931{
932 unsigned int num_old, num_new, i;
933 struct option *merge;
934
935 for (num_old = 0; oldopts[num_old].name; num_old++);
936 for (num_new = 0; newopts[num_new].name; num_new++);
937
938 global_option_offset += OPTION_OFFSET;
939 *option_offset = global_option_offset;
940
941 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
942 memcpy(merge, oldopts, num_old * sizeof(struct option));
943 for (i = 0; i < num_new; i++) {
944 merge[num_old + i] = newopts[i];
945 merge[num_old + i].val += *option_offset;
946 }
947 memset(merge + num_old + num_new, 0, sizeof(struct option));
948
949 return merge;
950}
951
952void
953register_match(struct iptables_match *me)
954{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000955 struct iptables_match **i;
956
Marc Bouchere6869a82000-03-20 06:03:29 +0000957 if (strcmp(me->version, program_version) != 0) {
958 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
959 program_name, me->name, me->version, program_version);
960 exit(1);
961 }
962
Rusty Russell52a51492000-05-02 16:44:29 +0000963 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000964 fprintf(stderr, "%s: match `%s' already registered.\n",
965 program_name, me->name);
966 exit(1);
967 }
968
Rusty Russell73f72f52000-07-03 10:17:57 +0000969 if (me->size != IPT_ALIGN(me->size)) {
970 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
971 program_name, me->name, me->size);
972 exit(1);
973 }
974
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000975 /* Append to list. */
976 for (i = &iptables_matches; *i; i = &(*i)->next);
977 me->next = NULL;
978 *i = me;
979
Marc Bouchere6869a82000-03-20 06:03:29 +0000980 me->m = NULL;
981 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000982}
983
984void
985register_target(struct iptables_target *me)
986{
987 if (strcmp(me->version, program_version) != 0) {
988 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
989 program_name, me->name, me->version, program_version);
990 exit(1);
991 }
992
Rusty Russell52a51492000-05-02 16:44:29 +0000993 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000994 fprintf(stderr, "%s: target `%s' already registered.\n",
995 program_name, me->name);
996 exit(1);
997 }
998
Rusty Russell73f72f52000-07-03 10:17:57 +0000999 if (me->size != IPT_ALIGN(me->size)) {
1000 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1001 program_name, me->name, me->size);
1002 exit(1);
1003 }
1004
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 /* Prepend to list. */
1006 me->next = iptables_targets;
1007 iptables_targets = me;
1008 me->t = NULL;
1009 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001010}
1011
1012static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001013print_num(u_int64_t number, unsigned int format)
1014{
1015 if (format & FMT_KILOMEGAGIGA) {
1016 if (number > 99999) {
1017 number = (number + 500) / 1000;
1018 if (number > 9999) {
1019 number = (number + 500) / 1000;
1020 if (number > 9999) {
1021 number = (number + 500) / 1000;
1022 printf(FMT("%4lluG ","%lluG "),number);
1023 }
1024 else printf(FMT("%4lluM ","%lluM "), number);
1025 } else
1026 printf(FMT("%4lluK ","%lluK "), number);
1027 } else
1028 printf(FMT("%5llu ","%llu "), number);
1029 } else
1030 printf(FMT("%8llu ","%llu "), number);
1031}
1032
1033
1034static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001035print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1036{
1037 struct ipt_counters counters;
1038 const char *pol = iptc_get_policy(chain, &counters, handle);
1039 printf("Chain %s", chain);
1040 if (pol) {
1041 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001042 if (!(format & FMT_NOCOUNTS)) {
1043 fputc(' ', stdout);
1044 print_num(counters.pcnt, (format|FMT_NOTABLE));
1045 fputs("packets, ", stdout);
1046 print_num(counters.bcnt, (format|FMT_NOTABLE));
1047 fputs("bytes", stdout);
1048 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001049 printf(")\n");
1050 } else {
1051 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001052 if (!iptc_get_references(&refs, chain, handle))
1053 printf(" (ERROR obtaining refs)\n");
1054 else
1055 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001056 }
1057
1058 if (format & FMT_LINENUMBERS)
1059 printf(FMT("%-4s ", "%s "), "num");
1060 if (!(format & FMT_NOCOUNTS)) {
1061 if (format & FMT_KILOMEGAGIGA) {
1062 printf(FMT("%5s ","%s "), "pkts");
1063 printf(FMT("%5s ","%s "), "bytes");
1064 } else {
1065 printf(FMT("%8s ","%s "), "pkts");
1066 printf(FMT("%10s ","%s "), "bytes");
1067 }
1068 }
1069 if (!(format & FMT_NOTARGET))
1070 printf(FMT("%-9s ","%s "), "target");
1071 fputs(" prot ", stdout);
1072 if (format & FMT_OPTIONS)
1073 fputs("opt", stdout);
1074 if (format & FMT_VIA) {
1075 printf(FMT(" %-6s ","%s "), "in");
1076 printf(FMT("%-6s ","%s "), "out");
1077 }
1078 printf(FMT(" %-19s ","%s "), "source");
1079 printf(FMT(" %-19s "," %s "), "destination");
1080 printf("\n");
1081}
1082
Marc Bouchere6869a82000-03-20 06:03:29 +00001083
1084static int
1085print_match(const struct ipt_entry_match *m,
1086 const struct ipt_ip *ip,
1087 int numeric)
1088{
Rusty Russell52a51492000-05-02 16:44:29 +00001089 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001090
1091 if (match) {
1092 if (match->print)
1093 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001094 else
Rusty Russellb039b022000-09-01 06:04:05 +00001095 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001096 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001097 if (m->u.user.name[0])
1098 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001099 }
1100 /* Don't stop iterating. */
1101 return 0;
1102}
1103
1104/* e is called `fw' here for hysterical raisins */
1105static void
1106print_firewall(const struct ipt_entry *fw,
1107 const char *targname,
1108 unsigned int num,
1109 unsigned int format,
1110 const iptc_handle_t handle)
1111{
1112 struct iptables_target *target = NULL;
1113 const struct ipt_entry_target *t;
1114 u_int8_t flags;
1115 char buf[BUFSIZ];
1116
1117 /* User creates a chain called "REJECT": this overrides the
1118 `REJECT' target module. Keep feeding them rope until the
1119 revolution... Bwahahahahah */
1120 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001121 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001122 else
Rusty Russell52a51492000-05-02 16:44:29 +00001123 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001124
1125 t = ipt_get_target((struct ipt_entry *)fw);
1126 flags = fw->ip.flags;
1127
1128 if (format & FMT_LINENUMBERS)
1129 printf(FMT("%-4u ", "%u "), num+1);
1130
1131 if (!(format & FMT_NOCOUNTS)) {
1132 print_num(fw->counters.pcnt, format);
1133 print_num(fw->counters.bcnt, format);
1134 }
1135
1136 if (!(format & FMT_NOTARGET))
1137 printf(FMT("%-9s ", "%s "), targname);
1138
1139 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1140 {
Rusty Russell28381a42000-05-10 00:19:50 +00001141 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001142 if (pname)
1143 printf(FMT("%-5s", "%s "), pname);
1144 else
1145 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1146 }
1147
1148 if (format & FMT_OPTIONS) {
1149 if (format & FMT_NOTABLE)
1150 fputs("opt ", stdout);
1151 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1152 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1153 fputc(' ', stdout);
1154 }
1155
1156 if (format & FMT_VIA) {
1157 char iface[IFNAMSIZ+2];
1158
1159 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1160 iface[0] = '!';
1161 iface[1] = '\0';
1162 }
1163 else iface[0] = '\0';
1164
1165 if (fw->ip.iniface[0] != '\0') {
1166 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001167 }
1168 else if (format & FMT_NUMERIC) strcat(iface, "*");
1169 else strcat(iface, "any");
1170 printf(FMT(" %-6s ","in %s "), iface);
1171
1172 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1173 iface[0] = '!';
1174 iface[1] = '\0';
1175 }
1176 else iface[0] = '\0';
1177
1178 if (fw->ip.outiface[0] != '\0') {
1179 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001180 }
1181 else if (format & FMT_NUMERIC) strcat(iface, "*");
1182 else strcat(iface, "any");
1183 printf(FMT("%-6s ","out %s "), iface);
1184 }
1185
1186 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1187 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1188 printf(FMT("%-19s ","%s "), "anywhere");
1189 else {
1190 if (format & FMT_NUMERIC)
1191 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1192 else
1193 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1194 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1195 printf(FMT("%-19s ","%s "), buf);
1196 }
1197
1198 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1199 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1200 printf(FMT("%-19s","-> %s"), "anywhere");
1201 else {
1202 if (format & FMT_NUMERIC)
1203 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1204 else
1205 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1206 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1207 printf(FMT("%-19s","-> %s"), buf);
1208 }
1209
1210 if (format & FMT_NOTABLE)
1211 fputs(" ", stdout);
1212
1213 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1214
1215 if (target) {
1216 if (target->print)
1217 /* Print the target information. */
1218 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001219 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001220 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001221 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001222
1223 if (!(format & FMT_NONEWLINE))
1224 fputc('\n', stdout);
1225}
1226
1227static void
1228print_firewall_line(const struct ipt_entry *fw,
1229 const iptc_handle_t h)
1230{
1231 struct ipt_entry_target *t;
1232
1233 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001234 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001235}
1236
1237static int
1238append_entry(const ipt_chainlabel chain,
1239 struct ipt_entry *fw,
1240 unsigned int nsaddrs,
1241 const struct in_addr saddrs[],
1242 unsigned int ndaddrs,
1243 const struct in_addr daddrs[],
1244 int verbose,
1245 iptc_handle_t *handle)
1246{
1247 unsigned int i, j;
1248 int ret = 1;
1249
1250 for (i = 0; i < nsaddrs; i++) {
1251 fw->ip.src.s_addr = saddrs[i].s_addr;
1252 for (j = 0; j < ndaddrs; j++) {
1253 fw->ip.dst.s_addr = daddrs[j].s_addr;
1254 if (verbose)
1255 print_firewall_line(fw, *handle);
1256 ret &= iptc_append_entry(chain, fw, handle);
1257 }
1258 }
1259
1260 return ret;
1261}
1262
1263static int
1264replace_entry(const ipt_chainlabel chain,
1265 struct ipt_entry *fw,
1266 unsigned int rulenum,
1267 const struct in_addr *saddr,
1268 const struct in_addr *daddr,
1269 int verbose,
1270 iptc_handle_t *handle)
1271{
1272 fw->ip.src.s_addr = saddr->s_addr;
1273 fw->ip.dst.s_addr = daddr->s_addr;
1274
1275 if (verbose)
1276 print_firewall_line(fw, *handle);
1277 return iptc_replace_entry(chain, fw, rulenum, handle);
1278}
1279
1280static int
1281insert_entry(const ipt_chainlabel chain,
1282 struct ipt_entry *fw,
1283 unsigned int rulenum,
1284 unsigned int nsaddrs,
1285 const struct in_addr saddrs[],
1286 unsigned int ndaddrs,
1287 const struct in_addr daddrs[],
1288 int verbose,
1289 iptc_handle_t *handle)
1290{
1291 unsigned int i, j;
1292 int ret = 1;
1293
1294 for (i = 0; i < nsaddrs; i++) {
1295 fw->ip.src.s_addr = saddrs[i].s_addr;
1296 for (j = 0; j < ndaddrs; j++) {
1297 fw->ip.dst.s_addr = daddrs[j].s_addr;
1298 if (verbose)
1299 print_firewall_line(fw, *handle);
1300 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1301 }
1302 }
1303
1304 return ret;
1305}
1306
Rusty Russell2e0a3212000-04-19 11:23:18 +00001307static unsigned char *
1308make_delete_mask(struct ipt_entry *fw)
1309{
1310 /* Establish mask for comparison */
1311 unsigned int size;
1312 struct iptables_match *m;
1313 unsigned char *mask, *mptr;
1314
1315 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001316 for (m = iptables_matches; m; m = m->next) {
1317 if (!m->used)
1318 continue;
1319
Rusty Russell73f72f52000-07-03 10:17:57 +00001320 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001321 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001322
Rusty Russell9e1d2142000-04-23 09:11:12 +00001323 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001324 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001325 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001326
Rusty Russell9e1d2142000-04-23 09:11:12 +00001327 memset(mask, 0xFF, sizeof(struct ipt_entry));
1328 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001329
Sven Kochfb1279a2001-02-27 12:25:12 +00001330 for (m = iptables_matches; m; m = m->next) {
1331 if (!m->used)
1332 continue;
1333
Rusty Russell2e0a3212000-04-19 11:23:18 +00001334 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001335 IPT_ALIGN(sizeof(struct ipt_entry_match))
1336 + m->userspacesize);
1337 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001338 }
1339
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001340 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001341 IPT_ALIGN(sizeof(struct ipt_entry_target))
1342 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001343
1344 return mask;
1345}
1346
Marc Bouchere6869a82000-03-20 06:03:29 +00001347static int
1348delete_entry(const ipt_chainlabel chain,
1349 struct ipt_entry *fw,
1350 unsigned int nsaddrs,
1351 const struct in_addr saddrs[],
1352 unsigned int ndaddrs,
1353 const struct in_addr daddrs[],
1354 int verbose,
1355 iptc_handle_t *handle)
1356{
1357 unsigned int i, j;
1358 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001359 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001360
Rusty Russell2e0a3212000-04-19 11:23:18 +00001361 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001362 for (i = 0; i < nsaddrs; i++) {
1363 fw->ip.src.s_addr = saddrs[i].s_addr;
1364 for (j = 0; j < ndaddrs; j++) {
1365 fw->ip.dst.s_addr = daddrs[j].s_addr;
1366 if (verbose)
1367 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001368 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001369 }
1370 }
1371 return ret;
1372}
1373
1374static int
1375check_packet(const ipt_chainlabel chain,
1376 struct ipt_entry *fw,
1377 unsigned int nsaddrs,
1378 const struct in_addr saddrs[],
1379 unsigned int ndaddrs,
1380 const struct in_addr daddrs[],
1381 int verbose,
1382 iptc_handle_t *handle)
1383{
1384 int ret = 1;
1385 unsigned int i, j;
1386 const char *msg;
1387
1388 for (i = 0; i < nsaddrs; i++) {
1389 fw->ip.src.s_addr = saddrs[i].s_addr;
1390 for (j = 0; j < ndaddrs; j++) {
1391 fw->ip.dst.s_addr = daddrs[j].s_addr;
1392 if (verbose)
1393 print_firewall_line(fw, *handle);
1394 msg = iptc_check_packet(chain, fw, handle);
1395 if (!msg) ret = 0;
1396 else printf("%s\n", msg);
1397 }
1398 }
1399
1400 return ret;
1401}
1402
Harald Welteae1ff9f2000-12-01 14:26:20 +00001403int
Marc Bouchere6869a82000-03-20 06:03:29 +00001404for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001405 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001406{
1407 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001408 const char *chain;
1409 char *chains;
1410 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001411
Rusty Russell9e1d2142000-04-23 09:11:12 +00001412 chain = iptc_first_chain(handle);
1413 while (chain) {
1414 chaincount++;
1415 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001416 }
1417
Rusty Russell9e1d2142000-04-23 09:11:12 +00001418 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1419 i = 0;
1420 chain = iptc_first_chain(handle);
1421 while (chain) {
1422 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1423 i++;
1424 chain = iptc_next_chain(handle);
1425 }
1426
1427 for (i = 0; i < chaincount; i++) {
1428 if (!builtinstoo
1429 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1430 *handle))
1431 continue;
1432 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1433 }
1434
1435 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 return ret;
1437}
1438
Harald Welteae1ff9f2000-12-01 14:26:20 +00001439int
Marc Bouchere6869a82000-03-20 06:03:29 +00001440flush_entries(const ipt_chainlabel chain, int verbose,
1441 iptc_handle_t *handle)
1442{
1443 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001444 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001445
1446 if (verbose)
1447 fprintf(stdout, "Flushing chain `%s'\n", chain);
1448 return iptc_flush_entries(chain, handle);
1449}
Marc Bouchere6869a82000-03-20 06:03:29 +00001450
1451static int
1452zero_entries(const ipt_chainlabel chain, int verbose,
1453 iptc_handle_t *handle)
1454{
1455 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001456 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001457
Marc Bouchere6869a82000-03-20 06:03:29 +00001458 if (verbose)
1459 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1460 return iptc_zero_entries(chain, handle);
1461}
1462
Harald Welteae1ff9f2000-12-01 14:26:20 +00001463int
Marc Bouchere6869a82000-03-20 06:03:29 +00001464delete_chain(const ipt_chainlabel chain, int verbose,
1465 iptc_handle_t *handle)
1466{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001467 if (!chain)
1468 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001469
1470 if (verbose)
1471 fprintf(stdout, "Deleting chain `%s'\n", chain);
1472 return iptc_delete_chain(chain, handle);
1473}
1474
1475static int
1476list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1477 int expanded, int linenumbers, iptc_handle_t *handle)
1478{
1479 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001480 unsigned int format;
1481 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001482
1483 format = FMT_OPTIONS;
1484 if (!verbose)
1485 format |= FMT_NOCOUNTS;
1486 else
1487 format |= FMT_VIA;
1488
1489 if (numeric)
1490 format |= FMT_NUMERIC;
1491
1492 if (!expanded)
1493 format |= FMT_KILOMEGAGIGA;
1494
1495 if (linenumbers)
1496 format |= FMT_LINENUMBERS;
1497
Rusty Russell9e1d2142000-04-23 09:11:12 +00001498 for (this = iptc_first_chain(handle);
1499 this;
1500 this = iptc_next_chain(handle)) {
1501 const struct ipt_entry *i;
1502 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001503
Marc Bouchere6869a82000-03-20 06:03:29 +00001504 if (chain && strcmp(chain, this) != 0)
1505 continue;
1506
1507 if (found) printf("\n");
1508
1509 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001510 i = iptc_first_rule(this, handle);
1511
1512 num = 0;
1513 while (i) {
1514 print_firewall(i,
1515 iptc_get_target(i, handle),
1516 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001517 format,
1518 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001519 i = iptc_next_rule(i, handle);
1520 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001521 found = 1;
1522 }
1523
1524 errno = ENOENT;
1525 return found;
1526}
1527
Harald Welte82dd2ec2000-12-19 05:18:15 +00001528static char *get_modprobe(void)
1529{
1530 int procfile;
1531 char *ret;
1532
1533 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1534 if (procfile < 0)
1535 return NULL;
1536
1537 ret = malloc(1024);
1538 if (ret) {
1539 switch (read(procfile, ret, 1024)) {
1540 case -1: goto fail;
1541 case 1024: goto fail; /* Partial read. Wierd */
1542 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001543 if (ret[strlen(ret)-1]=='\n')
1544 ret[strlen(ret)-1]=0;
1545 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001546 return ret;
1547 }
1548 fail:
1549 free(ret);
1550 close(procfile);
1551 return NULL;
1552}
1553
1554static int iptables_insmod(const char *modname, const char *modprobe)
1555{
1556 char *buf = NULL;
1557 char *argv[3];
1558
1559 /* If they don't explicitly set it, read out of kernel */
1560 if (!modprobe) {
1561 buf = get_modprobe();
1562 if (!buf)
1563 return -1;
1564 modprobe = buf;
1565 }
1566
1567 switch (fork()) {
1568 case 0:
1569 argv[0] = (char *)modprobe;
1570 argv[1] = (char *)modname;
1571 argv[2] = NULL;
1572 execv(argv[0], argv);
1573
1574 /* not usually reached */
1575 exit(0);
1576 case -1:
1577 return -1;
1578
1579 default: /* parent */
1580 wait(NULL);
1581 }
1582
1583 free(buf);
1584 return 0;
1585}
1586
Marc Bouchere6869a82000-03-20 06:03:29 +00001587static struct ipt_entry *
1588generate_entry(const struct ipt_entry *fw,
1589 struct iptables_match *matches,
1590 struct ipt_entry_target *target)
1591{
1592 unsigned int size;
1593 struct iptables_match *m;
1594 struct ipt_entry *e;
1595
1596 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001597 for (m = matches; m; m = m->next) {
1598 if (!m->used)
1599 continue;
1600
Rusty Russell228e98d2000-04-27 10:28:06 +00001601 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001602 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001603
Rusty Russell228e98d2000-04-27 10:28:06 +00001604 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001605 *e = *fw;
1606 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001607 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001608
1609 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001610 for (m = matches; m; m = m->next) {
1611 if (!m->used)
1612 continue;
1613
Rusty Russell228e98d2000-04-27 10:28:06 +00001614 memcpy(e->elems + size, m->m, m->m->u.match_size);
1615 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001616 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001617 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001618
1619 return e;
1620}
1621
1622int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1623{
1624 struct ipt_entry fw, *e = NULL;
1625 int invert = 0;
1626 unsigned int nsaddrs = 0, ndaddrs = 0;
1627 struct in_addr *saddrs = NULL, *daddrs = NULL;
1628
1629 int c, verbose = 0;
1630 const char *chain = NULL;
1631 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1632 const char *policy = NULL, *newname = NULL;
1633 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001634 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001635 int ret = 1;
1636 struct iptables_match *m;
1637 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001638 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001639 const char *jumpto = "";
1640 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001641 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001642
1643 memset(&fw, 0, sizeof(fw));
1644
Sven Kochfb1279a2001-02-27 12:25:12 +00001645 opts = original_opts;
1646 global_option_offset = 0;
1647
Harald Welteae1ff9f2000-12-01 14:26:20 +00001648 /* re-set optind to 0 in case do_command gets called
1649 * a second time */
1650 optind = 0;
1651
1652 /* clear mflags in case do_command gets called a second time
1653 * (we clear the global list of all matches for security)*/
1654 for (m = iptables_matches; m; m = m->next) {
1655 m->mflags = 0;
1656 m->used = 0;
1657 }
1658
1659 for (t = iptables_targets; t; t = t->next) {
1660 t->tflags = 0;
1661 t->used = 0;
1662 }
1663
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 /* Suppress error messages: we may add new options if we
1665 demand-load a protocol. */
1666 opterr = 0;
1667
1668 while ((c = getopt_long(argc, argv,
Harald Welteccd49e52001-01-23 22:54:34 +00001669 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001670 opts, NULL)) != -1) {
1671 switch (c) {
1672 /*
1673 * Command selection
1674 */
1675 case 'A':
1676 add_command(&command, CMD_APPEND, CMD_NONE,
1677 invert);
1678 chain = optarg;
1679 break;
1680
1681 case 'D':
1682 add_command(&command, CMD_DELETE, CMD_NONE,
1683 invert);
1684 chain = optarg;
1685 if (optind < argc && argv[optind][0] != '-'
1686 && argv[optind][0] != '!') {
1687 rulenum = parse_rulenumber(argv[optind++]);
1688 command = CMD_DELETE_NUM;
1689 }
1690 break;
1691
1692 case 'C':
1693 add_command(&command, CMD_CHECK, CMD_NONE,
1694 invert);
1695 chain = optarg;
1696 break;
1697
1698 case 'R':
1699 add_command(&command, CMD_REPLACE, CMD_NONE,
1700 invert);
1701 chain = optarg;
1702 if (optind < argc && argv[optind][0] != '-'
1703 && argv[optind][0] != '!')
1704 rulenum = parse_rulenumber(argv[optind++]);
1705 else
1706 exit_error(PARAMETER_PROBLEM,
1707 "-%c requires a rule number",
1708 cmd2char(CMD_REPLACE));
1709 break;
1710
1711 case 'I':
1712 add_command(&command, CMD_INSERT, CMD_NONE,
1713 invert);
1714 chain = optarg;
1715 if (optind < argc && argv[optind][0] != '-'
1716 && argv[optind][0] != '!')
1717 rulenum = parse_rulenumber(argv[optind++]);
1718 else rulenum = 1;
1719 break;
1720
1721 case 'L':
1722 add_command(&command, CMD_LIST, CMD_ZERO,
1723 invert);
1724 if (optarg) chain = optarg;
1725 else if (optind < argc && argv[optind][0] != '-'
1726 && argv[optind][0] != '!')
1727 chain = argv[optind++];
1728 break;
1729
1730 case 'F':
1731 add_command(&command, CMD_FLUSH, CMD_NONE,
1732 invert);
1733 if (optarg) chain = optarg;
1734 else if (optind < argc && argv[optind][0] != '-'
1735 && argv[optind][0] != '!')
1736 chain = argv[optind++];
1737 break;
1738
1739 case 'Z':
1740 add_command(&command, CMD_ZERO, CMD_LIST,
1741 invert);
1742 if (optarg) chain = optarg;
1743 else if (optind < argc && argv[optind][0] != '-'
1744 && argv[optind][0] != '!')
1745 chain = argv[optind++];
1746 break;
1747
1748 case 'N':
1749 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1750 invert);
1751 chain = optarg;
1752 break;
1753
1754 case 'X':
1755 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1756 invert);
1757 if (optarg) chain = optarg;
1758 else if (optind < argc && argv[optind][0] != '-'
1759 && argv[optind][0] != '!')
1760 chain = argv[optind++];
1761 break;
1762
1763 case 'E':
1764 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1765 invert);
1766 chain = optarg;
1767 if (optind < argc && argv[optind][0] != '-'
1768 && argv[optind][0] != '!')
1769 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001770 else
1771 exit_error(PARAMETER_PROBLEM,
1772 "-%c requires old-chain-name and "
1773 "new-chain-name",
1774 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 break;
1776
1777 case 'P':
1778 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1779 invert);
1780 chain = optarg;
1781 if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 policy = argv[optind++];
1784 else
1785 exit_error(PARAMETER_PROBLEM,
1786 "-%c requires a chain and a policy",
1787 cmd2char(CMD_SET_POLICY));
1788 break;
1789
1790 case 'h':
1791 if (!optarg)
1792 optarg = argv[optind];
1793
Rusty Russell2e0a3212000-04-19 11:23:18 +00001794 /* iptables -p icmp -h */
1795 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001796 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001797
Marc Bouchere6869a82000-03-20 06:03:29 +00001798 exit_printhelp();
1799
1800 /*
1801 * Option selection
1802 */
1803 case 'p':
1804 if (check_inverse(optarg, &invert))
1805 optind++;
1806 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1807 invert);
1808
1809 /* Canonicalize into lower case */
1810 for (protocol = argv[optind-1]; *protocol; protocol++)
1811 *protocol = tolower(*protocol);
1812
1813 protocol = argv[optind-1];
1814 fw.ip.proto = parse_protocol(protocol);
1815
1816 if (fw.ip.proto == 0
1817 && (fw.ip.invflags & IPT_INV_PROTO))
1818 exit_error(PARAMETER_PROBLEM,
1819 "rule would never match protocol");
1820 fw.nfcache |= NFC_IP_PROTO;
1821 break;
1822
1823 case 's':
1824 if (check_inverse(optarg, &invert))
1825 optind++;
1826 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1827 invert);
1828 shostnetworkmask = argv[optind-1];
1829 fw.nfcache |= NFC_IP_SRC;
1830 break;
1831
1832 case 'd':
1833 if (check_inverse(optarg, &invert))
1834 optind++;
1835 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1836 invert);
1837 dhostnetworkmask = argv[optind-1];
1838 fw.nfcache |= NFC_IP_DST;
1839 break;
1840
1841 case 'j':
1842 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1843 invert);
1844 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001845 /* TRY_LOAD (may be chain name) */
1846 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001847
1848 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001849 size_t size;
1850
Rusty Russell73f72f52000-07-03 10:17:57 +00001851 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1852 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001853
Rusty Russell2e0a3212000-04-19 11:23:18 +00001854 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001855 target->t->u.target_size = size;
1856 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001857 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001858 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001859 }
1860 break;
1861
1862
1863 case 'i':
1864 if (check_inverse(optarg, &invert))
1865 optind++;
1866 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1867 invert);
1868 parse_interface(argv[optind-1],
1869 fw.ip.iniface,
1870 fw.ip.iniface_mask);
1871 fw.nfcache |= NFC_IP_IF_IN;
1872 break;
1873
1874 case 'o':
1875 if (check_inverse(optarg, &invert))
1876 optind++;
1877 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1878 invert);
1879 parse_interface(argv[optind-1],
1880 fw.ip.outiface,
1881 fw.ip.outiface_mask);
1882 fw.nfcache |= NFC_IP_IF_OUT;
1883 break;
1884
1885 case 'f':
1886 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1887 invert);
1888 fw.ip.flags |= IPT_F_FRAG;
1889 fw.nfcache |= NFC_IP_FRAG;
1890 break;
1891
1892 case 'v':
1893 if (!verbose)
1894 set_option(&options, OPT_VERBOSE,
1895 &fw.ip.invflags, invert);
1896 verbose++;
1897 break;
1898
Rusty Russell52a51492000-05-02 16:44:29 +00001899 case 'm': {
1900 size_t size;
1901
Marc Bouchere6869a82000-03-20 06:03:29 +00001902 if (invert)
1903 exit_error(PARAMETER_PROBLEM,
1904 "unexpected ! flag before --match");
1905
Rusty Russell52a51492000-05-02 16:44:29 +00001906 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001907 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1908 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001909 m->m = fw_calloc(1, size);
1910 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001911 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001912 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001913 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001914 }
1915 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001916
1917 case 'n':
1918 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1919 invert);
1920 break;
1921
1922 case 't':
1923 if (invert)
1924 exit_error(PARAMETER_PROBLEM,
1925 "unexpected ! flag before --table");
1926 *table = argv[optind-1];
1927 break;
1928
1929 case 'x':
1930 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1931 invert);
1932 break;
1933
1934 case 'V':
1935 if (invert)
1936 printf("Not %s ;-)\n", program_version);
1937 else
1938 printf("%s v%s\n",
1939 program_name, program_version);
1940 exit(0);
1941
1942 case '0':
1943 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1944 invert);
1945 break;
1946
Harald Welte82dd2ec2000-12-19 05:18:15 +00001947 case 'M':
1948 modprobe = optarg;
1949 break;
1950
Harald Welteccd49e52001-01-23 22:54:34 +00001951 case 'c':
1952
1953 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1954 invert);
1955 pcnt = optarg;
1956 if (optind < argc && argv[optind][0] != '-'
1957 && argv[optind][0] != '!')
1958 bcnt = argv[optind++];
1959 else
1960 exit_error(PARAMETER_PROBLEM,
1961 "-%c requires packet and byte counter",
1962 opt2char(OPT_COUNTERS));
1963
1964 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1965 exit_error(PARAMETER_PROBLEM,
1966 "-%c packet counter not numeric",
1967 opt2char(OPT_COUNTERS));
1968
1969 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1970 exit_error(PARAMETER_PROBLEM,
1971 "-%c byte counter not numeric",
1972 opt2char(OPT_COUNTERS));
1973
1974 break;
1975
1976
Marc Bouchere6869a82000-03-20 06:03:29 +00001977 case 1: /* non option */
1978 if (optarg[0] == '!' && optarg[1] == '\0') {
1979 if (invert)
1980 exit_error(PARAMETER_PROBLEM,
1981 "multiple consecutive ! not"
1982 " allowed");
1983 invert = TRUE;
1984 optarg[0] = '\0';
1985 continue;
1986 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001987 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001988 exit_tryhelp(2);
1989
1990 default:
1991 /* FIXME: This scheme doesn't allow two of the same
1992 matches --RR */
1993 if (!target
1994 || !(target->parse(c - target->option_offset,
1995 argv, invert,
1996 &target->tflags,
1997 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00001998 for (m = iptables_matches; m; m = m->next) {
1999 if (!m->used)
2000 continue;
2001
Marc Bouchere6869a82000-03-20 06:03:29 +00002002 if (m->parse(c - m->option_offset,
2003 argv, invert,
2004 &m->mflags,
2005 &fw,
2006 &fw.nfcache,
2007 &m->m))
2008 break;
2009 }
2010
2011 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00002012 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00002013 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00002014 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00002015 && !find_proto(protocol, DONT_LOAD,
2016 options&OPT_NUMERIC)
2017 && (m = find_proto(protocol, TRY_LOAD,
2018 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002019 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00002020 size_t size;
2021
Rusty Russell73f72f52000-07-03 10:17:57 +00002022 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2023 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002024
Rusty Russell2e0a3212000-04-19 11:23:18 +00002025 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002026 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002027 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002028 m->init(m->m, &fw.nfcache);
2029
Sven Kochdbe857a2001-03-02 09:41:08 +00002030 opts = merge_options(opts,
2031 m->extra_opts, &m->option_offset);
2032
Marc Bouchere6869a82000-03-20 06:03:29 +00002033 optind--;
2034 continue;
2035 }
2036 if (!m)
2037 exit_error(PARAMETER_PROBLEM,
2038 "Unknown arg `%s'",
2039 argv[optind-1]);
2040 }
2041 }
2042 invert = FALSE;
2043 }
2044
Sven Kochfb1279a2001-02-27 12:25:12 +00002045 for (m = iptables_matches; m; m = m->next) {
2046 if (!m->used)
2047 continue;
2048
Marc Bouchere6869a82000-03-20 06:03:29 +00002049 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002050 }
2051
Marc Bouchere6869a82000-03-20 06:03:29 +00002052 if (target)
2053 target->final_check(target->tflags);
2054
2055 /* Fix me: must put inverse options checking here --MN */
2056
2057 if (optind < argc)
2058 exit_error(PARAMETER_PROBLEM,
2059 "unknown arguments found on commandline");
2060 if (!command)
2061 exit_error(PARAMETER_PROBLEM, "no command specified");
2062 if (invert)
2063 exit_error(PARAMETER_PROBLEM,
2064 "nothing appropriate following !");
2065
Marc Boucher744bd022000-04-22 22:36:10 +00002066 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2067 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002068 if (!(options & OPT_DESTINATION))
2069 dhostnetworkmask = "0.0.0.0/0";
2070 if (!(options & OPT_SOURCE))
2071 shostnetworkmask = "0.0.0.0/0";
2072 }
2073
2074 if (shostnetworkmask)
2075 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2076 &(fw.ip.smsk), &nsaddrs);
2077
2078 if (dhostnetworkmask)
2079 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2080 &(fw.ip.dmsk), &ndaddrs);
2081
2082 if ((nsaddrs > 1 || ndaddrs > 1) &&
2083 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2084 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2085 " source or destination IP addresses");
2086
2087 if (command == CMD_CHECK && fw.ip.invflags != 0)
2088 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2089 cmd2char(CMD_CHECK));
2090
2091 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2092 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2093 "specify a unique address");
2094
2095 generic_opt_check(command, options);
2096
2097 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2098 exit_error(PARAMETER_PROBLEM,
2099 "chain name `%s' too long (must be under %i chars)",
2100 chain, IPT_FUNCTION_MAXNAMELEN);
2101
Harald Welteae1ff9f2000-12-01 14:26:20 +00002102 /* only allocate handle if we weren't called with a handle */
2103 if (!*handle)
2104 *handle = iptc_init(*table);
2105
Harald Welte82dd2ec2000-12-19 05:18:15 +00002106 if (!*handle) {
2107 /* try to insmod the module if iptc_init failed */
2108 iptables_insmod("ip_tables", modprobe);
2109 *handle = iptc_init(*table);
2110 }
2111
Marc Bouchere6869a82000-03-20 06:03:29 +00002112 if (!*handle)
2113 exit_error(VERSION_PROBLEM,
2114 "can't initialize iptables table `%s': %s",
2115 *table, iptc_strerror(errno));
2116
Marc Boucher744bd022000-04-22 22:36:10 +00002117 if (command == CMD_CHECK
2118 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002119 || command == CMD_DELETE
2120 || command == CMD_INSERT
2121 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002122 if (strcmp(chain, "PREROUTING") == 0
2123 || strcmp(chain, "INPUT") == 0) {
2124 /* -o not valid with incoming packets. */
2125 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 exit_error(PARAMETER_PROBLEM,
2127 "Can't use -%c with %s\n",
2128 opt2char(OPT_VIANAMEOUT),
2129 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002130 /* -i required with -C */
2131 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2132 exit_error(PARAMETER_PROBLEM,
2133 "Need -%c with %s\n",
2134 opt2char(OPT_VIANAMEIN),
2135 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002136 }
2137
Rusty Russella4860fd2000-06-17 16:13:02 +00002138 if (strcmp(chain, "POSTROUTING") == 0
2139 || strcmp(chain, "OUTPUT") == 0) {
2140 /* -i not valid with outgoing packets */
2141 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 exit_error(PARAMETER_PROBLEM,
2143 "Can't use -%c with %s\n",
2144 opt2char(OPT_VIANAMEIN),
2145 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002146 /* -o required with -C */
2147 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2148 exit_error(PARAMETER_PROBLEM,
2149 "Need -%c with %s\n",
2150 opt2char(OPT_VIANAMEOUT),
2151 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002152 }
2153
2154 if (target && iptc_is_chain(jumpto, *handle)) {
2155 printf("Warning: using chain %s, not extension\n",
2156 jumpto);
2157
2158 target = NULL;
2159 }
2160
2161 /* If they didn't specify a target, or it's a chain
2162 name, use standard. */
2163 if (!target
2164 && (strlen(jumpto) == 0
2165 || iptc_is_chain(jumpto, *handle))) {
2166 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002167
Rusty Russell52a51492000-05-02 16:44:29 +00002168 target = find_target(IPT_STANDARD_TARGET,
2169 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002170
2171 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002172 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002173 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002174 target->t->u.target_size = size;
2175 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002176 target->init(target->t, &fw.nfcache);
2177 }
2178
Rusty Russell7e53bf92000-03-20 07:03:28 +00002179 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002180 /* it is no chain, and we can't load a plugin.
2181 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002182 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002183 * chain. */
2184 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002185 } else {
2186 e = generate_entry(&fw, iptables_matches, target->t);
2187 }
2188 }
2189
2190 switch (command) {
2191 case CMD_APPEND:
2192 ret = append_entry(chain, e,
2193 nsaddrs, saddrs, ndaddrs, daddrs,
2194 options&OPT_VERBOSE,
2195 handle);
2196 break;
2197 case CMD_CHECK:
2198 ret = check_packet(chain, e,
2199 nsaddrs, saddrs, ndaddrs, daddrs,
2200 options&OPT_VERBOSE, handle);
2201 break;
2202 case CMD_DELETE:
2203 ret = delete_entry(chain, e,
2204 nsaddrs, saddrs, ndaddrs, daddrs,
2205 options&OPT_VERBOSE,
2206 handle);
2207 break;
2208 case CMD_DELETE_NUM:
2209 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2210 break;
2211 case CMD_REPLACE:
2212 ret = replace_entry(chain, e, rulenum - 1,
2213 saddrs, daddrs, options&OPT_VERBOSE,
2214 handle);
2215 break;
2216 case CMD_INSERT:
2217 ret = insert_entry(chain, e, rulenum - 1,
2218 nsaddrs, saddrs, ndaddrs, daddrs,
2219 options&OPT_VERBOSE,
2220 handle);
2221 break;
2222 case CMD_LIST:
2223 ret = list_entries(chain,
2224 options&OPT_VERBOSE,
2225 options&OPT_NUMERIC,
2226 options&OPT_EXPANDED,
2227 options&OPT_LINENUMBERS,
2228 handle);
2229 break;
2230 case CMD_FLUSH:
2231 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2232 break;
2233 case CMD_ZERO:
2234 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2235 break;
2236 case CMD_LIST|CMD_ZERO:
2237 ret = list_entries(chain,
2238 options&OPT_VERBOSE,
2239 options&OPT_NUMERIC,
2240 options&OPT_EXPANDED,
2241 options&OPT_LINENUMBERS,
2242 handle);
2243 if (ret)
2244 ret = zero_entries(chain,
2245 options&OPT_VERBOSE, handle);
2246 break;
2247 case CMD_NEW_CHAIN:
2248 ret = iptc_create_chain(chain, handle);
2249 break;
2250 case CMD_DELETE_CHAIN:
2251 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2252 break;
2253 case CMD_RENAME_CHAIN:
2254 ret = iptc_rename_chain(chain, newname, handle);
2255 break;
2256 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002257 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002258 break;
2259 default:
2260 /* We should never reach this... */
2261 exit_tryhelp(2);
2262 }
2263
2264 if (verbose > 1)
2265 dump_entries(*handle);
2266
2267 return ret;
2268}