Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* 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 | * |
Harald Welte | d4ab5ad | 2002-08-07 09:07:24 +0000 | [diff] [blame] | 6 | * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>: |
| 7 | * Paul 'Rusty' Russell <rusty@rustcorp.com.au> |
| 8 | * Marc Boucher <marc+nf@mbsi.ca> |
| 9 | * James Morris <jmorris@intercode.com.au> |
| 10 | * Harald Welte <laforge@gnumonks.org> |
| 11 | * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
| 12 | * |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <getopt.h> |
| 29 | #include <string.h> |
| 30 | #include <netdb.h> |
| 31 | #include <errno.h> |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 32 | #include <stdbool.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
| 36 | #include <stdarg.h> |
| 37 | #include <limits.h> |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 39 | #include <iptables.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 40 | #include <xtables.h> |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 41 | #include <fcntl.h> |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 42 | #include <sys/utsname.h> |
Jan Engelhardt | f89c171 | 2009-06-12 20:48:52 +0200 | [diff] [blame] | 43 | #include "xshared.h" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 44 | |
| 45 | #ifndef TRUE |
| 46 | #define TRUE 1 |
| 47 | #endif |
| 48 | #ifndef FALSE |
| 49 | #define FALSE 0 |
| 50 | #endif |
| 51 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 52 | #define FMT_NUMERIC 0x0001 |
| 53 | #define FMT_NOCOUNTS 0x0002 |
| 54 | #define FMT_KILOMEGAGIGA 0x0004 |
| 55 | #define FMT_OPTIONS 0x0008 |
| 56 | #define FMT_NOTABLE 0x0010 |
| 57 | #define FMT_NOTARGET 0x0020 |
| 58 | #define FMT_VIA 0x0040 |
| 59 | #define FMT_NONEWLINE 0x0080 |
| 60 | #define FMT_LINENUMBERS 0x0100 |
| 61 | |
| 62 | #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \ |
| 63 | | FMT_NUMERIC | FMT_NOTABLE) |
| 64 | #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab)) |
| 65 | |
| 66 | |
| 67 | #define CMD_NONE 0x0000U |
| 68 | #define CMD_INSERT 0x0001U |
| 69 | #define CMD_DELETE 0x0002U |
| 70 | #define CMD_DELETE_NUM 0x0004U |
| 71 | #define CMD_REPLACE 0x0008U |
| 72 | #define CMD_APPEND 0x0010U |
| 73 | #define CMD_LIST 0x0020U |
| 74 | #define CMD_FLUSH 0x0040U |
| 75 | #define CMD_ZERO 0x0080U |
| 76 | #define CMD_NEW_CHAIN 0x0100U |
| 77 | #define CMD_DELETE_CHAIN 0x0200U |
| 78 | #define CMD_SET_POLICY 0x0400U |
Harald Welte | 0eca33f | 2006-04-21 11:56:30 +0000 | [diff] [blame] | 79 | #define CMD_RENAME_CHAIN 0x0800U |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 80 | #define CMD_LIST_RULES 0x1000U |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 81 | #define CMD_ZERO_NUM 0x2000U |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 82 | #define CMD_CHECK 0x4000U |
| 83 | #define NUMBER_OF_CMD 16 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 84 | static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z', |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 85 | 'Z', 'N', 'X', 'P', 'E', 'S', 'C' }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 86 | |
Jan Engelhardt | f1e7101 | 2011-02-07 03:13:43 +0100 | [diff] [blame] | 87 | #define OPT_FRAGMENT 0x00800U |
Jan Engelhardt | f4b6e52 | 2011-02-07 03:16:14 +0100 | [diff] [blame] | 88 | #define NUMBER_OF_OPT ARRAY_SIZE(optflags) |
| 89 | static const char optflags[] |
Jan Engelhardt | f1e7101 | 2011-02-07 03:13:43 +0100 | [diff] [blame] | 90 | = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c', 'f'}; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 91 | |
| 92 | static struct option original_opts[] = { |
Gáspár Lajos | 7bc3cb7 | 2008-03-27 08:20:39 +0100 | [diff] [blame] | 93 | {.name = "append", .has_arg = 1, .val = 'A'}, |
| 94 | {.name = "delete", .has_arg = 1, .val = 'D'}, |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 95 | {.name = "check", .has_arg = 1, .val = 'C'}, |
Gáspár Lajos | 7bc3cb7 | 2008-03-27 08:20:39 +0100 | [diff] [blame] | 96 | {.name = "insert", .has_arg = 1, .val = 'I'}, |
| 97 | {.name = "replace", .has_arg = 1, .val = 'R'}, |
| 98 | {.name = "list", .has_arg = 2, .val = 'L'}, |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 99 | {.name = "list-rules", .has_arg = 2, .val = 'S'}, |
Gáspár Lajos | 7bc3cb7 | 2008-03-27 08:20:39 +0100 | [diff] [blame] | 100 | {.name = "flush", .has_arg = 2, .val = 'F'}, |
| 101 | {.name = "zero", .has_arg = 2, .val = 'Z'}, |
| 102 | {.name = "new-chain", .has_arg = 1, .val = 'N'}, |
| 103 | {.name = "delete-chain", .has_arg = 2, .val = 'X'}, |
| 104 | {.name = "rename-chain", .has_arg = 1, .val = 'E'}, |
| 105 | {.name = "policy", .has_arg = 1, .val = 'P'}, |
| 106 | {.name = "source", .has_arg = 1, .val = 's'}, |
| 107 | {.name = "destination", .has_arg = 1, .val = 'd'}, |
| 108 | {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */ |
| 109 | {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */ |
| 110 | {.name = "protocol", .has_arg = 1, .val = 'p'}, |
| 111 | {.name = "in-interface", .has_arg = 1, .val = 'i'}, |
| 112 | {.name = "jump", .has_arg = 1, .val = 'j'}, |
| 113 | {.name = "table", .has_arg = 1, .val = 't'}, |
| 114 | {.name = "match", .has_arg = 1, .val = 'm'}, |
| 115 | {.name = "numeric", .has_arg = 0, .val = 'n'}, |
| 116 | {.name = "out-interface", .has_arg = 1, .val = 'o'}, |
| 117 | {.name = "verbose", .has_arg = 0, .val = 'v'}, |
| 118 | {.name = "exact", .has_arg = 0, .val = 'x'}, |
| 119 | {.name = "fragments", .has_arg = 0, .val = 'f'}, |
| 120 | {.name = "version", .has_arg = 0, .val = 'V'}, |
| 121 | {.name = "help", .has_arg = 2, .val = 'h'}, |
| 122 | {.name = "line-numbers", .has_arg = 0, .val = '0'}, |
| 123 | {.name = "modprobe", .has_arg = 1, .val = 'M'}, |
| 124 | {.name = "set-counters", .has_arg = 1, .val = 'c'}, |
| 125 | {.name = "goto", .has_arg = 1, .val = 'g'}, |
| 126 | {NULL}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Illes Marci | 63e9063 | 2003-03-03 08:08:37 +0000 | [diff] [blame] | 129 | /* we need this for iptables-restore. iptables-restore.c sets line to the |
| 130 | * current line of the input file, in order to give a more precise error |
| 131 | * message. iptables itself doesn't need this, so it is initialized to the |
| 132 | * magic number of -1 */ |
| 133 | int line = -1; |
| 134 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 135 | void iptables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3))); |
| 136 | |
Jamal Hadi Salim | 4dcdc9b | 2009-02-11 13:03:34 +0100 | [diff] [blame] | 137 | struct xtables_globals iptables_globals = { |
| 138 | .option_offset = 0, |
| 139 | .program_version = IPTABLES_VERSION, |
Jamal Hadi Salim | 139b3fe | 2009-02-12 11:43:01 -0500 | [diff] [blame] | 140 | .orig_opts = original_opts, |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 141 | .exit_err = iptables_exit_error, |
Jamal Hadi Salim | 4dcdc9b | 2009-02-11 13:03:34 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 144 | /* Table of legal combinations of commands and options. If any of the |
| 145 | * given commands make an option legal, that option is legal (applies to |
| 146 | * CMD_LIST and CMD_ZERO only). |
| 147 | * Key: |
| 148 | * + compulsory |
| 149 | * x illegal |
| 150 | * optional |
| 151 | */ |
| 152 | |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 153 | static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 154 | /* Well, it's better than "Re: Linux vs FreeBSD" */ |
| 155 | { |
Jan Engelhardt | f1e7101 | 2011-02-07 03:13:43 +0100 | [diff] [blame] | 156 | /* -n -s -d -p -j -v -x -i -o --line -c -f */ |
| 157 | /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '}, |
| 158 | /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '}, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 159 | /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
Jan Engelhardt | f1e7101 | 2011-02-07 03:13:43 +0100 | [diff] [blame] | 160 | /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '}, |
| 161 | /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '}, |
| 162 | /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x','x'}, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 163 | /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
| 164 | /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 165 | /*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 166 | /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
| 167 | /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
Jan Engelhardt | f1e7101 | 2011-02-07 03:13:43 +0100 | [diff] [blame] | 168 | /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' ','x'}, |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 169 | /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 170 | /*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}, |
| 171 | /*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 174 | static const int inverse_for_options[NUMBER_OF_OPT] = |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 175 | { |
| 176 | /* -n */ 0, |
| 177 | /* -s */ IPT_INV_SRCIP, |
| 178 | /* -d */ IPT_INV_DSTIP, |
| 179 | /* -p */ IPT_INV_PROTO, |
| 180 | /* -j */ 0, |
| 181 | /* -v */ 0, |
| 182 | /* -x */ 0, |
| 183 | /* -i */ IPT_INV_VIA_IN, |
| 184 | /* -o */ IPT_INV_VIA_OUT, |
| 185 | /* -f */ IPT_INV_FRAG, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 186 | /*--line*/ 0, |
| 187 | /* -c */ 0, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Pablo Neira Ayuso | f503cb8 | 2009-03-03 17:46:17 +0100 | [diff] [blame] | 190 | #define opts iptables_globals.opts |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 191 | #define prog_name iptables_globals.program_name |
| 192 | #define prog_vers iptables_globals.program_version |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 193 | |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 194 | int kernel_version; |
| 195 | |
Rusty Russell | a3e6aaa | 2000-12-19 04:45:23 +0000 | [diff] [blame] | 196 | /* Primitive headers... */ |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 197 | /* defined in netinet/in.h */ |
| 198 | #if 0 |
Rusty Russell | a3e6aaa | 2000-12-19 04:45:23 +0000 | [diff] [blame] | 199 | #ifndef IPPROTO_ESP |
| 200 | #define IPPROTO_ESP 50 |
| 201 | #endif |
| 202 | #ifndef IPPROTO_AH |
| 203 | #define IPPROTO_AH 51 |
| 204 | #endif |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 205 | #endif |
Rusty Russell | a3e6aaa | 2000-12-19 04:45:23 +0000 | [diff] [blame] | 206 | |
Pablo Neira Ayuso | 267a570 | 2006-11-29 13:32:32 +0000 | [diff] [blame] | 207 | enum { |
| 208 | IPT_DOTTED_ADDR = 0, |
| 209 | IPT_DOTTED_MASK |
| 210 | }; |
| 211 | |
Dmitry V. Levin | 24bb078 | 2010-05-14 13:26:22 +0200 | [diff] [blame] | 212 | static void __attribute__((noreturn)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 213 | exit_tryhelp(int status) |
| 214 | { |
Maciej Soltysiak | edad9bb | 2003-03-31 12:11:55 +0000 | [diff] [blame] | 215 | if (line != -1) |
Harald Welte | a5bb0a6 | 2003-05-03 18:56:19 +0000 | [diff] [blame] | 216 | fprintf(stderr, "Error occurred at line: %d\n", line); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 217 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 218 | prog_name, prog_name); |
Jamal Hadi Salim | 139b3fe | 2009-02-12 11:43:01 -0500 | [diff] [blame] | 219 | xtables_free_opts(1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 220 | exit(status); |
| 221 | } |
| 222 | |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 223 | static void |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 224 | exit_printhelp(const struct xtables_rule_match *matches) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 225 | { |
| 226 | printf("%s v%s\n\n" |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 227 | "Usage: %s -[ACD] chain rule-specification [options]\n" |
Jan Engelhardt | 1791a45 | 2009-02-20 16:39:54 +0100 | [diff] [blame] | 228 | " %s -I chain [rulenum] rule-specification [options]\n" |
| 229 | " %s -R chain rulenum rule-specification [options]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 230 | " %s -D chain rulenum [options]\n" |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 231 | " %s -[LS] [chain [rulenum]] [options]\n" |
| 232 | " %s -[FZ] [chain] [options]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 233 | " %s -[NX] chain\n" |
| 234 | " %s -E old-chain-name new-chain-name\n" |
| 235 | " %s -P chain target [options]\n" |
| 236 | " %s -h (print this help information)\n\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 237 | prog_name, prog_vers, prog_name, prog_name, |
| 238 | prog_name, prog_name, prog_name, prog_name, |
Jan Engelhardt | 1791a45 | 2009-02-20 16:39:54 +0100 | [diff] [blame] | 239 | prog_name, prog_name, prog_name, prog_name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 240 | |
| 241 | printf( |
| 242 | "Commands:\n" |
| 243 | "Either long or short options are allowed.\n" |
| 244 | " --append -A chain Append to chain\n" |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 245 | " --check -C chain Check for the existence of a rule\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 246 | " --delete -D chain Delete matching rule from chain\n" |
| 247 | " --delete -D chain rulenum\n" |
| 248 | " Delete rule rulenum (1 = first) from chain\n" |
| 249 | " --insert -I chain [rulenum]\n" |
| 250 | " Insert in chain as rulenum (default 1=first)\n" |
| 251 | " --replace -R chain rulenum\n" |
| 252 | " Replace rule rulenum (1 = first) in chain\n" |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 253 | " --list -L [chain [rulenum]]\n" |
| 254 | " List the rules in a chain or all chains\n" |
| 255 | " --list-rules -S [chain [rulenum]]\n" |
| 256 | " Print the rules in a chain or all chains\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 257 | " --flush -F [chain] Delete all rules in chain or all chains\n" |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 258 | " --zero -Z [chain [rulenum]]\n" |
| 259 | " Zero counters in chain or all chains\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 260 | " --new -N chain Create a new user-defined chain\n" |
| 261 | " --delete-chain\n" |
| 262 | " -X [chain] Delete a user-defined chain\n" |
| 263 | " --policy -P chain target\n" |
| 264 | " Change policy on chain to target\n" |
| 265 | " --rename-chain\n" |
| 266 | " -E old-chain new-chain\n" |
| 267 | " Change chain name, (moving any references)\n" |
| 268 | |
| 269 | "Options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 270 | "[!] --proto -p proto protocol: by number or name, eg. `tcp'\n" |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 271 | "[!] --source -s address[/mask][...]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 272 | " source specification\n" |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 273 | "[!] --destination -d address[/mask][...]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 274 | " destination specification\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 275 | "[!] --in-interface -i input name[+]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 276 | " network interface name ([+] for wildcard)\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 277 | " --jump -j target\n" |
Rusty Russell | 363112d | 2000-08-11 13:49:26 +0000 | [diff] [blame] | 278 | " target for rule (may load target extension)\n" |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 279 | #ifdef IPT_F_GOTO |
| 280 | " --goto -g chain\n" |
| 281 | " jump to chain with no return\n" |
| 282 | #endif |
Rusty Russell | 363112d | 2000-08-11 13:49:26 +0000 | [diff] [blame] | 283 | " --match -m match\n" |
| 284 | " extended match (may load extension)\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 285 | " --numeric -n numeric output of addresses and ports\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 286 | "[!] --out-interface -o output name[+]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 287 | " network interface name ([+] for wildcard)\n" |
| 288 | " --table -t table table to manipulate (default: `filter')\n" |
| 289 | " --verbose -v verbose mode\n" |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 290 | " --line-numbers print line numbers when listing\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 291 | " --exact -x expand numbers (display exact values)\n" |
| 292 | "[!] --fragment -f match second or further fragments only\n" |
Rusty Russell | a4d3e1f | 2001-01-07 06:56:02 +0000 | [diff] [blame] | 293 | " --modprobe=<command> try to insert modules using this command\n" |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 294 | " --set-counters PKTS BYTES set the counter during insert/append\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 295 | "[!] --version -V print package version.\n"); |
| 296 | |
Jan Engelhardt | f89c171 | 2009-06-12 20:48:52 +0200 | [diff] [blame] | 297 | print_extension_helps(xtables_targets, matches); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 298 | exit(0); |
| 299 | } |
| 300 | |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 301 | void |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 302 | iptables_exit_error(enum xtables_exittype status, const char *msg, ...) |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 303 | { |
| 304 | va_list args; |
| 305 | |
| 306 | va_start(args, msg); |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 307 | fprintf(stderr, "%s v%s: ", prog_name, prog_vers); |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 308 | vfprintf(stderr, msg, args); |
| 309 | va_end(args); |
| 310 | fprintf(stderr, "\n"); |
| 311 | if (status == PARAMETER_PROBLEM) |
| 312 | exit_tryhelp(status); |
| 313 | if (status == VERSION_PROBLEM) |
| 314 | fprintf(stderr, |
| 315 | "Perhaps iptables or your kernel needs to be upgraded.\n"); |
| 316 | /* On error paths, make sure that we don't leak memory */ |
Jamal Hadi Salim | 139b3fe | 2009-02-12 11:43:01 -0500 | [diff] [blame] | 317 | xtables_free_opts(1); |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 318 | exit(status); |
| 319 | } |
| 320 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 321 | static void |
| 322 | generic_opt_check(int command, int options) |
| 323 | { |
| 324 | int i, j, legal = 0; |
| 325 | |
| 326 | /* Check that commands are valid with options. Complicated by the |
| 327 | * fact that if an option is legal with *any* command given, it is |
| 328 | * legal overall (ie. -z and -l). |
| 329 | */ |
| 330 | for (i = 0; i < NUMBER_OF_OPT; i++) { |
| 331 | legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */ |
| 332 | |
| 333 | for (j = 0; j < NUMBER_OF_CMD; j++) { |
| 334 | if (!(command & (1<<j))) |
| 335 | continue; |
| 336 | |
| 337 | if (!(options & (1<<i))) { |
| 338 | if (commands_v_options[j][i] == '+') |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 339 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 340 | "You need to supply the `-%c' " |
| 341 | "option for this command\n", |
| 342 | optflags[i]); |
| 343 | } else { |
| 344 | if (commands_v_options[j][i] != 'x') |
| 345 | legal = 1; |
| 346 | else if (legal == 0) |
| 347 | legal = -1; |
| 348 | } |
| 349 | } |
| 350 | if (legal == -1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 351 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 352 | "Illegal option `-%c' with this command\n", |
| 353 | optflags[i]); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static char |
| 358 | opt2char(int option) |
| 359 | { |
| 360 | const char *ptr; |
| 361 | for (ptr = optflags; option > 1; option >>= 1, ptr++); |
| 362 | |
| 363 | return *ptr; |
| 364 | } |
| 365 | |
| 366 | static char |
| 367 | cmd2char(int option) |
| 368 | { |
| 369 | const char *ptr; |
| 370 | for (ptr = cmdflags; option > 1; option >>= 1, ptr++); |
| 371 | |
| 372 | return *ptr; |
| 373 | } |
| 374 | |
| 375 | static void |
Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 376 | add_command(unsigned int *cmd, const int newcmd, const int othercmds, |
| 377 | int invert) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 378 | { |
| 379 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 380 | xtables_error(PARAMETER_PROBLEM, "unexpected ! flag"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 381 | if (*cmd & (~othercmds)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 382 | xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n", |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 383 | cmd2char(newcmd), cmd2char(*cmd & (~othercmds))); |
| 384 | *cmd |= newcmd; |
| 385 | } |
| 386 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 387 | /* |
| 388 | * All functions starting with "parse" should succeed, otherwise |
| 389 | * the program fails. |
| 390 | * Most routines return pointers to static data that may change |
| 391 | * between calls to the same or other routines with a few exceptions: |
| 392 | * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask" |
| 393 | * return global static data. |
| 394 | */ |
| 395 | |
Rusty Russell | 28381a4 | 2000-05-10 00:19:50 +0000 | [diff] [blame] | 396 | /* Christophe Burki wants `-p 6' to imply `-m tcp'. */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 397 | /* Can't be zero. */ |
| 398 | static int |
| 399 | parse_rulenumber(const char *rule) |
| 400 | { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 401 | unsigned int rulenum; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 402 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 403 | if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 404 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 405 | "Invalid rule number `%s'", rule); |
| 406 | |
| 407 | return rulenum; |
| 408 | } |
| 409 | |
| 410 | static const char * |
| 411 | parse_target(const char *targetname) |
| 412 | { |
| 413 | const char *ptr; |
| 414 | |
| 415 | if (strlen(targetname) < 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 416 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 417 | "Invalid target name (too short)"); |
| 418 | |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 419 | if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 420 | xtables_error(PARAMETER_PROBLEM, |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 421 | "Invalid target name `%s' (%u chars max)", |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 422 | targetname, XT_EXTENSION_MAXNAMELEN - 1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 423 | |
| 424 | for (ptr = targetname; *ptr; ptr++) |
| 425 | if (isspace(*ptr)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 426 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 427 | "Invalid target name `%s'", targetname); |
| 428 | return targetname; |
| 429 | } |
| 430 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 431 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 432 | set_option(unsigned int *options, unsigned int option, uint8_t *invflg, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 433 | int invert) |
| 434 | { |
| 435 | if (*options & option) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 436 | xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed", |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 437 | opt2char(option)); |
| 438 | *options |= option; |
| 439 | |
| 440 | if (invert) { |
| 441 | unsigned int i; |
| 442 | for (i = 0; 1 << i != option; i++); |
| 443 | |
| 444 | if (!inverse_for_options[i]) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 445 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 446 | "cannot have ! before -%c", |
| 447 | opt2char(option)); |
| 448 | *invflg |= inverse_for_options[i]; |
| 449 | } |
| 450 | } |
| 451 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 452 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 453 | print_num(uint64_t number, unsigned int format) |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 454 | { |
| 455 | if (format & FMT_KILOMEGAGIGA) { |
| 456 | if (number > 99999) { |
| 457 | number = (number + 500) / 1000; |
| 458 | if (number > 9999) { |
| 459 | number = (number + 500) / 1000; |
| 460 | if (number > 9999) { |
| 461 | number = (number + 500) / 1000; |
Rusty Russell | 5a66fe4 | 2001-08-15 11:21:59 +0000 | [diff] [blame] | 462 | if (number > 9999) { |
| 463 | number = (number + 500) / 1000; |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 464 | printf(FMT("%4lluT ","%lluT "), (unsigned long long)number); |
Rusty Russell | 5a66fe4 | 2001-08-15 11:21:59 +0000 | [diff] [blame] | 465 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 466 | else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 467 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 468 | else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 469 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 470 | printf(FMT("%4lluK ","%lluK "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 471 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 472 | printf(FMT("%5llu ","%llu "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 473 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 474 | printf(FMT("%8llu ","%llu "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
| 478 | static void |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 479 | print_header(unsigned int format, const char *chain, struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 480 | { |
| 481 | struct ipt_counters counters; |
| 482 | const char *pol = iptc_get_policy(chain, &counters, handle); |
| 483 | printf("Chain %s", chain); |
| 484 | if (pol) { |
| 485 | printf(" (policy %s", pol); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 486 | if (!(format & FMT_NOCOUNTS)) { |
| 487 | fputc(' ', stdout); |
| 488 | print_num(counters.pcnt, (format|FMT_NOTABLE)); |
| 489 | fputs("packets, ", stdout); |
| 490 | print_num(counters.bcnt, (format|FMT_NOTABLE)); |
| 491 | fputs("bytes", stdout); |
| 492 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 493 | printf(")\n"); |
| 494 | } else { |
| 495 | unsigned int refs; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 496 | if (!iptc_get_references(&refs, chain, handle)) |
| 497 | printf(" (ERROR obtaining refs)\n"); |
| 498 | else |
| 499 | printf(" (%u references)\n", refs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (format & FMT_LINENUMBERS) |
| 503 | printf(FMT("%-4s ", "%s "), "num"); |
| 504 | if (!(format & FMT_NOCOUNTS)) { |
| 505 | if (format & FMT_KILOMEGAGIGA) { |
| 506 | printf(FMT("%5s ","%s "), "pkts"); |
| 507 | printf(FMT("%5s ","%s "), "bytes"); |
| 508 | } else { |
| 509 | printf(FMT("%8s ","%s "), "pkts"); |
| 510 | printf(FMT("%10s ","%s "), "bytes"); |
| 511 | } |
| 512 | } |
| 513 | if (!(format & FMT_NOTARGET)) |
| 514 | printf(FMT("%-9s ","%s "), "target"); |
| 515 | fputs(" prot ", stdout); |
| 516 | if (format & FMT_OPTIONS) |
| 517 | fputs("opt", stdout); |
| 518 | if (format & FMT_VIA) { |
| 519 | printf(FMT(" %-6s ","%s "), "in"); |
| 520 | printf(FMT("%-6s ","%s "), "out"); |
| 521 | } |
| 522 | printf(FMT(" %-19s ","%s "), "source"); |
| 523 | printf(FMT(" %-19s "," %s "), "destination"); |
| 524 | printf("\n"); |
| 525 | } |
| 526 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 527 | |
| 528 | static int |
| 529 | print_match(const struct ipt_entry_match *m, |
| 530 | const struct ipt_ip *ip, |
| 531 | int numeric) |
| 532 | { |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 533 | const struct xtables_match *match = |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 534 | xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 535 | |
| 536 | if (match) { |
| 537 | if (match->print) |
| 538 | match->print(ip, m, numeric); |
Rusty Russell | 629149f | 2000-09-01 06:01:00 +0000 | [diff] [blame] | 539 | else |
Rusty Russell | b039b02 | 2000-09-01 06:04:05 +0000 | [diff] [blame] | 540 | printf("%s ", match->name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 541 | } else { |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 542 | if (m->u.user.name[0]) |
| 543 | printf("UNKNOWN match `%s' ", m->u.user.name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 544 | } |
| 545 | /* Don't stop iterating. */ |
| 546 | return 0; |
| 547 | } |
| 548 | |
Jan Engelhardt | 6cf172e | 2008-03-10 17:48:59 +0100 | [diff] [blame] | 549 | /* e is called `fw' here for historical reasons */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 550 | static void |
| 551 | print_firewall(const struct ipt_entry *fw, |
| 552 | const char *targname, |
| 553 | unsigned int num, |
| 554 | unsigned int format, |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 555 | struct iptc_handle *const handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 556 | { |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 557 | const struct xtables_target *target = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 558 | const struct ipt_entry_target *t; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 559 | uint8_t flags; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 560 | char buf[BUFSIZ]; |
| 561 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 562 | if (!iptc_is_chain(targname, handle)) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 563 | target = xtables_find_target(targname, XTF_TRY_LOAD); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 564 | else |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 565 | target = xtables_find_target(IPT_STANDARD_TARGET, |
| 566 | XTF_LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 567 | |
| 568 | t = ipt_get_target((struct ipt_entry *)fw); |
| 569 | flags = fw->ip.flags; |
| 570 | |
| 571 | if (format & FMT_LINENUMBERS) |
Henrik Nordstrom | 1564189 | 2008-06-13 17:57:35 +0200 | [diff] [blame] | 572 | printf(FMT("%-4u ", "%u "), num); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 573 | |
| 574 | if (!(format & FMT_NOCOUNTS)) { |
| 575 | print_num(fw->counters.pcnt, format); |
| 576 | print_num(fw->counters.bcnt, format); |
| 577 | } |
| 578 | |
| 579 | if (!(format & FMT_NOTARGET)) |
| 580 | printf(FMT("%-9s ", "%s "), targname); |
| 581 | |
| 582 | fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout); |
| 583 | { |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 584 | const char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 585 | if (pname) |
| 586 | printf(FMT("%-5s", "%s "), pname); |
| 587 | else |
| 588 | printf(FMT("%-5hu", "%hu "), fw->ip.proto); |
| 589 | } |
| 590 | |
| 591 | if (format & FMT_OPTIONS) { |
| 592 | if (format & FMT_NOTABLE) |
| 593 | fputs("opt ", stdout); |
| 594 | fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout); |
| 595 | fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout); |
| 596 | fputc(' ', stdout); |
| 597 | } |
| 598 | |
| 599 | if (format & FMT_VIA) { |
| 600 | char iface[IFNAMSIZ+2]; |
| 601 | |
| 602 | if (fw->ip.invflags & IPT_INV_VIA_IN) { |
| 603 | iface[0] = '!'; |
| 604 | iface[1] = '\0'; |
| 605 | } |
| 606 | else iface[0] = '\0'; |
| 607 | |
| 608 | if (fw->ip.iniface[0] != '\0') { |
| 609 | strcat(iface, fw->ip.iniface); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 610 | } |
| 611 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 612 | else strcat(iface, "any"); |
| 613 | printf(FMT(" %-6s ","in %s "), iface); |
| 614 | |
| 615 | if (fw->ip.invflags & IPT_INV_VIA_OUT) { |
| 616 | iface[0] = '!'; |
| 617 | iface[1] = '\0'; |
| 618 | } |
| 619 | else iface[0] = '\0'; |
| 620 | |
| 621 | if (fw->ip.outiface[0] != '\0') { |
| 622 | strcat(iface, fw->ip.outiface); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 623 | } |
| 624 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 625 | else strcat(iface, "any"); |
| 626 | printf(FMT("%-6s ","out %s "), iface); |
| 627 | } |
| 628 | |
| 629 | fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout); |
| 630 | if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC)) |
| 631 | printf(FMT("%-19s ","%s "), "anywhere"); |
| 632 | else { |
| 633 | if (format & FMT_NUMERIC) |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 634 | strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.src)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 635 | else |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 636 | strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.src)); |
| 637 | strcat(buf, xtables_ipmask_to_numeric(&fw->ip.smsk)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 638 | printf(FMT("%-19s ","%s "), buf); |
| 639 | } |
| 640 | |
| 641 | fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout); |
| 642 | if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC)) |
Harald Welte | 25fc1d7 | 2003-05-31 21:30:33 +0000 | [diff] [blame] | 643 | printf(FMT("%-19s ","-> %s"), "anywhere"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 644 | else { |
| 645 | if (format & FMT_NUMERIC) |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 646 | strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.dst)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 647 | else |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 648 | strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.dst)); |
| 649 | strcat(buf, xtables_ipmask_to_numeric(&fw->ip.dmsk)); |
Harald Welte | 25fc1d7 | 2003-05-31 21:30:33 +0000 | [diff] [blame] | 650 | printf(FMT("%-19s ","-> %s"), buf); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | if (format & FMT_NOTABLE) |
| 654 | fputs(" ", stdout); |
| 655 | |
Harald Welte | 72bd87e | 2005-11-24 17:04:05 +0000 | [diff] [blame] | 656 | #ifdef IPT_F_GOTO |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 657 | if(fw->ip.flags & IPT_F_GOTO) |
| 658 | printf("[goto] "); |
Harald Welte | 72bd87e | 2005-11-24 17:04:05 +0000 | [diff] [blame] | 659 | #endif |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 660 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 661 | IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC); |
| 662 | |
| 663 | if (target) { |
| 664 | if (target->print) |
| 665 | /* Print the target information. */ |
| 666 | target->print(&fw->ip, t, format & FMT_NUMERIC); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 667 | } else if (t->u.target_size != sizeof(*t)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 668 | printf("[%u bytes of unknown target data] ", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 669 | (unsigned int)(t->u.target_size - sizeof(*t))); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 670 | |
| 671 | if (!(format & FMT_NONEWLINE)) |
| 672 | fputc('\n', stdout); |
| 673 | } |
| 674 | |
| 675 | static void |
| 676 | print_firewall_line(const struct ipt_entry *fw, |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 677 | struct iptc_handle *const h) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 678 | { |
| 679 | struct ipt_entry_target *t; |
| 680 | |
| 681 | t = ipt_get_target((struct ipt_entry *)fw); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 682 | print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int |
| 686 | append_entry(const ipt_chainlabel chain, |
| 687 | struct ipt_entry *fw, |
| 688 | unsigned int nsaddrs, |
| 689 | const struct in_addr saddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 690 | const struct in_addr smasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 691 | unsigned int ndaddrs, |
| 692 | const struct in_addr daddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 693 | const struct in_addr dmasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 694 | int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 695 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 696 | { |
| 697 | unsigned int i, j; |
| 698 | int ret = 1; |
| 699 | |
| 700 | for (i = 0; i < nsaddrs; i++) { |
| 701 | fw->ip.src.s_addr = saddrs[i].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 702 | fw->ip.smsk.s_addr = smasks[i].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 703 | for (j = 0; j < ndaddrs; j++) { |
| 704 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 705 | fw->ip.dmsk.s_addr = dmasks[j].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 706 | if (verbose) |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 707 | print_firewall_line(fw, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 708 | ret &= iptc_append_entry(chain, fw, handle); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | static int |
| 716 | replace_entry(const ipt_chainlabel chain, |
| 717 | struct ipt_entry *fw, |
| 718 | unsigned int rulenum, |
Jan Engelhardt | 75cb763 | 2009-11-15 15:51:27 +0100 | [diff] [blame] | 719 | const struct in_addr *saddr, const struct in_addr *smask, |
| 720 | const struct in_addr *daddr, const struct in_addr *dmask, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 721 | int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 722 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 723 | { |
| 724 | fw->ip.src.s_addr = saddr->s_addr; |
| 725 | fw->ip.dst.s_addr = daddr->s_addr; |
Jan Engelhardt | 75cb763 | 2009-11-15 15:51:27 +0100 | [diff] [blame] | 726 | fw->ip.smsk.s_addr = smask->s_addr; |
| 727 | fw->ip.dmsk.s_addr = dmask->s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 728 | |
| 729 | if (verbose) |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 730 | print_firewall_line(fw, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 731 | return iptc_replace_entry(chain, fw, rulenum, handle); |
| 732 | } |
| 733 | |
| 734 | static int |
| 735 | insert_entry(const ipt_chainlabel chain, |
| 736 | struct ipt_entry *fw, |
| 737 | unsigned int rulenum, |
| 738 | unsigned int nsaddrs, |
| 739 | const struct in_addr saddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 740 | const struct in_addr smasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 741 | unsigned int ndaddrs, |
| 742 | const struct in_addr daddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 743 | const struct in_addr dmasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 744 | int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 745 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 746 | { |
| 747 | unsigned int i, j; |
| 748 | int ret = 1; |
| 749 | |
| 750 | for (i = 0; i < nsaddrs; i++) { |
| 751 | fw->ip.src.s_addr = saddrs[i].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 752 | fw->ip.smsk.s_addr = smasks[i].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 753 | for (j = 0; j < ndaddrs; j++) { |
| 754 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 755 | fw->ip.dmsk.s_addr = dmasks[j].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 756 | if (verbose) |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 757 | print_firewall_line(fw, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 758 | ret &= iptc_insert_entry(chain, fw, rulenum, handle); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 765 | static unsigned char * |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 766 | make_delete_mask(const struct xtables_rule_match *matches, |
Jan Engelhardt | 4f0d7b6 | 2009-10-27 02:59:33 +0100 | [diff] [blame] | 767 | const struct xtables_target *target) |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 768 | { |
| 769 | /* Establish mask for comparison */ |
| 770 | unsigned int size; |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 771 | const struct xtables_rule_match *matchp; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 772 | unsigned char *mask, *mptr; |
| 773 | |
| 774 | size = sizeof(struct ipt_entry); |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 775 | for (matchp = matches; matchp; matchp = matchp->next) |
| 776 | size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 777 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 778 | mask = xtables_calloc(1, size |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 779 | + IPT_ALIGN(sizeof(struct ipt_entry_target)) |
Jan Engelhardt | 4f0d7b6 | 2009-10-27 02:59:33 +0100 | [diff] [blame] | 780 | + target->size); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 781 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 782 | memset(mask, 0xFF, sizeof(struct ipt_entry)); |
| 783 | mptr = mask + sizeof(struct ipt_entry); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 784 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 785 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 786 | memset(mptr, 0xFF, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 787 | IPT_ALIGN(sizeof(struct ipt_entry_match)) |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 788 | + matchp->match->userspacesize); |
| 789 | mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Rusty Russell | a4d3e1f | 2001-01-07 06:56:02 +0000 | [diff] [blame] | 792 | memset(mptr, 0xFF, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 793 | IPT_ALIGN(sizeof(struct ipt_entry_target)) |
Jan Engelhardt | 4f0d7b6 | 2009-10-27 02:59:33 +0100 | [diff] [blame] | 794 | + target->userspacesize); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 795 | |
| 796 | return mask; |
| 797 | } |
| 798 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 799 | static int |
| 800 | delete_entry(const ipt_chainlabel chain, |
| 801 | struct ipt_entry *fw, |
| 802 | unsigned int nsaddrs, |
| 803 | const struct in_addr saddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 804 | const struct in_addr smasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 805 | unsigned int ndaddrs, |
| 806 | const struct in_addr daddrs[], |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 807 | const struct in_addr dmasks[], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 808 | int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 809 | struct iptc_handle *handle, |
Jan Engelhardt | 4f0d7b6 | 2009-10-27 02:59:33 +0100 | [diff] [blame] | 810 | struct xtables_rule_match *matches, |
| 811 | const struct xtables_target *target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 812 | { |
| 813 | unsigned int i, j; |
| 814 | int ret = 1; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 815 | unsigned char *mask; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 816 | |
Jan Engelhardt | 4f0d7b6 | 2009-10-27 02:59:33 +0100 | [diff] [blame] | 817 | mask = make_delete_mask(matches, target); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 818 | for (i = 0; i < nsaddrs; i++) { |
| 819 | fw->ip.src.s_addr = saddrs[i].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 820 | fw->ip.smsk.s_addr = smasks[i].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 821 | for (j = 0; j < ndaddrs; j++) { |
| 822 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 823 | fw->ip.dmsk.s_addr = dmasks[j].s_addr; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 824 | if (verbose) |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 825 | print_firewall_line(fw, handle); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 826 | ret &= iptc_delete_entry(chain, fw, mask, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 829 | free(mask); |
| 830 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 831 | return ret; |
| 832 | } |
| 833 | |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 834 | static int |
| 835 | check_entry(const ipt_chainlabel chain, struct ipt_entry *fw, |
| 836 | unsigned int nsaddrs, const struct in_addr *saddrs, |
| 837 | const struct in_addr *smasks, unsigned int ndaddrs, |
| 838 | const struct in_addr *daddrs, const struct in_addr *dmasks, |
| 839 | bool verbose, struct iptc_handle *handle, |
| 840 | struct xtables_rule_match *matches, |
| 841 | const struct xtables_target *target) |
| 842 | { |
| 843 | unsigned int i, j; |
| 844 | int ret = 1; |
| 845 | unsigned char *mask; |
| 846 | |
| 847 | mask = make_delete_mask(matches, target); |
| 848 | for (i = 0; i < nsaddrs; i++) { |
| 849 | fw->ip.src.s_addr = saddrs[i].s_addr; |
| 850 | fw->ip.smsk.s_addr = smasks[i].s_addr; |
| 851 | for (j = 0; j < ndaddrs; j++) { |
| 852 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
| 853 | fw->ip.dmsk.s_addr = dmasks[j].s_addr; |
| 854 | if (verbose) |
| 855 | print_firewall_line(fw, handle); |
| 856 | ret &= iptc_check_entry(chain, fw, mask, handle); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | free(mask); |
| 861 | return ret; |
| 862 | } |
| 863 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 864 | int |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 865 | for_each_chain(int (*fn)(const ipt_chainlabel, int, struct iptc_handle *), |
| 866 | int verbose, int builtinstoo, struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 867 | { |
| 868 | int ret = 1; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 869 | const char *chain; |
| 870 | char *chains; |
| 871 | unsigned int i, chaincount = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 872 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 873 | chain = iptc_first_chain(handle); |
| 874 | while (chain) { |
| 875 | chaincount++; |
| 876 | chain = iptc_next_chain(handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 879 | chains = xtables_malloc(sizeof(ipt_chainlabel) * chaincount); |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 880 | i = 0; |
| 881 | chain = iptc_first_chain(handle); |
| 882 | while (chain) { |
| 883 | strcpy(chains + i*sizeof(ipt_chainlabel), chain); |
| 884 | i++; |
| 885 | chain = iptc_next_chain(handle); |
| 886 | } |
| 887 | |
| 888 | for (i = 0; i < chaincount; i++) { |
| 889 | if (!builtinstoo |
| 890 | && iptc_builtin(chains + i*sizeof(ipt_chainlabel), |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 891 | handle) == 1) |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 892 | continue; |
| 893 | ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle); |
| 894 | } |
| 895 | |
| 896 | free(chains); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 897 | return ret; |
| 898 | } |
| 899 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 900 | int |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 901 | flush_entries(const ipt_chainlabel chain, int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 902 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 903 | { |
| 904 | if (!chain) |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 905 | return for_each_chain(flush_entries, verbose, 1, handle); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 906 | |
| 907 | if (verbose) |
| 908 | fprintf(stdout, "Flushing chain `%s'\n", chain); |
| 909 | return iptc_flush_entries(chain, handle); |
| 910 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 911 | |
| 912 | static int |
| 913 | zero_entries(const ipt_chainlabel chain, int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 914 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 915 | { |
| 916 | if (!chain) |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 917 | return for_each_chain(zero_entries, verbose, 1, handle); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 918 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 919 | if (verbose) |
| 920 | fprintf(stdout, "Zeroing chain `%s'\n", chain); |
| 921 | return iptc_zero_entries(chain, handle); |
| 922 | } |
| 923 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 924 | int |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 925 | delete_chain(const ipt_chainlabel chain, int verbose, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 926 | struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 927 | { |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 928 | if (!chain) |
| 929 | return for_each_chain(delete_chain, verbose, 0, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 930 | |
| 931 | if (verbose) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 932 | fprintf(stdout, "Deleting chain `%s'\n", chain); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 933 | return iptc_delete_chain(chain, handle); |
| 934 | } |
| 935 | |
| 936 | static int |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 937 | list_entries(const ipt_chainlabel chain, int rulenum, int verbose, int numeric, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 938 | int expanded, int linenumbers, struct iptc_handle *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 939 | { |
| 940 | int found = 0; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 941 | unsigned int format; |
| 942 | const char *this; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 943 | |
| 944 | format = FMT_OPTIONS; |
| 945 | if (!verbose) |
| 946 | format |= FMT_NOCOUNTS; |
| 947 | else |
| 948 | format |= FMT_VIA; |
| 949 | |
| 950 | if (numeric) |
| 951 | format |= FMT_NUMERIC; |
| 952 | |
| 953 | if (!expanded) |
| 954 | format |= FMT_KILOMEGAGIGA; |
| 955 | |
| 956 | if (linenumbers) |
| 957 | format |= FMT_LINENUMBERS; |
| 958 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 959 | for (this = iptc_first_chain(handle); |
| 960 | this; |
| 961 | this = iptc_next_chain(handle)) { |
| 962 | const struct ipt_entry *i; |
| 963 | unsigned int num; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 964 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 965 | if (chain && strcmp(chain, this) != 0) |
| 966 | continue; |
| 967 | |
| 968 | if (found) printf("\n"); |
| 969 | |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 970 | if (!rulenum) |
| 971 | print_header(format, this, handle); |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 972 | i = iptc_first_rule(this, handle); |
| 973 | |
| 974 | num = 0; |
| 975 | while (i) { |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 976 | num++; |
| 977 | if (!rulenum || num == rulenum) |
| 978 | print_firewall(i, |
| 979 | iptc_get_target(i, handle), |
| 980 | num, |
| 981 | format, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 982 | handle); |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 983 | i = iptc_next_rule(i, handle); |
| 984 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 985 | found = 1; |
| 986 | } |
| 987 | |
| 988 | errno = ENOENT; |
| 989 | return found; |
| 990 | } |
| 991 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 992 | static void print_proto(uint16_t proto, int invert) |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 993 | { |
| 994 | if (proto) { |
| 995 | unsigned int i; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 996 | const char *invertstr = invert ? " !" : ""; |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 997 | |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 998 | const struct protoent *pent = getprotobynumber(proto); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 999 | if (pent) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1000 | printf("%s -p %s", invertstr, pent->p_name); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1001 | return; |
| 1002 | } |
| 1003 | |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1004 | for (i = 0; xtables_chain_protos[i].name != NULL; ++i) |
| 1005 | if (xtables_chain_protos[i].num == proto) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1006 | printf("%s -p %s", |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1007 | invertstr, xtables_chain_protos[i].name); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1008 | return; |
| 1009 | } |
| 1010 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1011 | printf("%s -p %u", invertstr, proto); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | #define IP_PARTS_NATIVE(n) \ |
| 1016 | (unsigned int)((n)>>24)&0xFF, \ |
| 1017 | (unsigned int)((n)>>16)&0xFF, \ |
| 1018 | (unsigned int)((n)>>8)&0xFF, \ |
| 1019 | (unsigned int)((n)&0xFF) |
| 1020 | |
| 1021 | #define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n)) |
| 1022 | |
| 1023 | /* This assumes that mask is contiguous, and byte-bounded. */ |
| 1024 | static void |
| 1025 | print_iface(char letter, const char *iface, const unsigned char *mask, |
| 1026 | int invert) |
| 1027 | { |
| 1028 | unsigned int i; |
| 1029 | |
| 1030 | if (mask[0] == 0) |
| 1031 | return; |
| 1032 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1033 | printf("%s -%c ", invert ? " !" : "", letter); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1034 | |
| 1035 | for (i = 0; i < IFNAMSIZ; i++) { |
| 1036 | if (mask[i] != 0) { |
| 1037 | if (iface[i] != '\0') |
| 1038 | printf("%c", iface[i]); |
| 1039 | } else { |
| 1040 | /* we can access iface[i-1] here, because |
| 1041 | * a few lines above we make sure that mask[0] != 0 */ |
| 1042 | if (iface[i-1] != '\0') |
| 1043 | printf("+"); |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static int print_match_save(const struct ipt_entry_match *e, |
| 1050 | const struct ipt_ip *ip) |
| 1051 | { |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 1052 | const struct xtables_match *match = |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 1053 | xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1054 | |
| 1055 | if (match) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1056 | printf(" -m %s", e->u.user.name); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1057 | |
| 1058 | /* some matches don't provide a save function */ |
| 1059 | if (match->save) |
| 1060 | match->save(ip, e); |
| 1061 | } else { |
| 1062 | if (e->u.match_size) { |
| 1063 | fprintf(stderr, |
| 1064 | "Can't find library for match `%s'\n", |
| 1065 | e->u.user.name); |
| 1066 | exit(1); |
| 1067 | } |
| 1068 | } |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* print a given ip including mask if neccessary */ |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 1073 | static void print_ip(const char *prefix, uint32_t ip, |
| 1074 | uint32_t mask, int invert) |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1075 | { |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 1076 | uint32_t bits, hmask = ntohl(mask); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1077 | int i; |
| 1078 | |
| 1079 | if (!mask && !ip && !invert) |
| 1080 | return; |
| 1081 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1082 | printf("%s %s %u.%u.%u.%u", |
| 1083 | invert ? " !" : "", |
Jan Engelhardt | b1d968c | 2009-04-04 13:28:40 +0200 | [diff] [blame] | 1084 | prefix, |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1085 | IP_PARTS(ip)); |
| 1086 | |
| 1087 | if (mask == 0xFFFFFFFFU) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1088 | printf("/32"); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | i = 32; |
| 1093 | bits = 0xFFFFFFFEU; |
| 1094 | while (--i >= 0 && hmask != bits) |
| 1095 | bits <<= 1; |
| 1096 | if (i >= 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1097 | printf("/%u", i); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1098 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1099 | printf("/%u.%u.%u.%u", IP_PARTS(mask)); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* We want this to be readable, so only print out neccessary fields. |
| 1103 | * Because that's the kind of world I want to live in. */ |
| 1104 | void print_rule(const struct ipt_entry *e, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1105 | struct iptc_handle *h, const char *chain, int counters) |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1106 | { |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 1107 | const struct ipt_entry_target *t; |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1108 | const char *target_name; |
| 1109 | |
| 1110 | /* print counters for iptables-save */ |
| 1111 | if (counters > 0) |
| 1112 | printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt); |
| 1113 | |
| 1114 | /* print chain name */ |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1115 | printf("-A %s", chain); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1116 | |
| 1117 | /* Print IP part. */ |
| 1118 | print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr, |
| 1119 | e->ip.invflags & IPT_INV_SRCIP); |
| 1120 | |
| 1121 | print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr, |
| 1122 | e->ip.invflags & IPT_INV_DSTIP); |
| 1123 | |
| 1124 | print_iface('i', e->ip.iniface, e->ip.iniface_mask, |
| 1125 | e->ip.invflags & IPT_INV_VIA_IN); |
| 1126 | |
| 1127 | print_iface('o', e->ip.outiface, e->ip.outiface_mask, |
| 1128 | e->ip.invflags & IPT_INV_VIA_OUT); |
| 1129 | |
| 1130 | print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO); |
| 1131 | |
| 1132 | if (e->ip.flags & IPT_F_FRAG) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1133 | printf("%s -f", |
| 1134 | e->ip.invflags & IPT_INV_FRAG ? " !" : ""); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1135 | |
| 1136 | /* Print matchinfo part */ |
| 1137 | if (e->target_offset) { |
| 1138 | IPT_MATCH_ITERATE(e, print_match_save, &e->ip); |
| 1139 | } |
| 1140 | |
| 1141 | /* print counters for iptables -R */ |
| 1142 | if (counters < 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1143 | printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1144 | |
| 1145 | /* Print target name */ |
| 1146 | target_name = iptc_get_target(e, h); |
| 1147 | if (target_name && (*target_name != '\0')) |
| 1148 | #ifdef IPT_F_GOTO |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1149 | printf(" -%c %s", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1150 | #else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1151 | printf(" -j %s", target_name); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1152 | #endif |
| 1153 | |
| 1154 | /* Print targinfo part */ |
| 1155 | t = ipt_get_target((struct ipt_entry *)e); |
| 1156 | if (t->u.user.name[0]) { |
Jan Engelhardt | d1435e0 | 2010-12-18 01:40:04 +0100 | [diff] [blame] | 1157 | const struct xtables_target *target = |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 1158 | xtables_find_target(t->u.user.name, XTF_TRY_LOAD); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1159 | |
| 1160 | if (!target) { |
| 1161 | fprintf(stderr, "Can't find library for target `%s'\n", |
| 1162 | t->u.user.name); |
| 1163 | exit(1); |
| 1164 | } |
| 1165 | |
| 1166 | if (target->save) |
| 1167 | target->save(&e->ip, t); |
| 1168 | else { |
| 1169 | /* If the target size is greater than ipt_entry_target |
| 1170 | * there is something to be saved, we just don't know |
| 1171 | * how to print it */ |
| 1172 | if (t->u.target_size != |
| 1173 | sizeof(struct ipt_entry_target)) { |
| 1174 | fprintf(stderr, "Target `%s' is missing " |
| 1175 | "save function\n", |
| 1176 | t->u.user.name); |
| 1177 | exit(1); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | printf("\n"); |
| 1182 | } |
| 1183 | |
| 1184 | static int |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1185 | list_rules(const ipt_chainlabel chain, int rulenum, int counters, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1186 | struct iptc_handle *handle) |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1187 | { |
| 1188 | const char *this = NULL; |
| 1189 | int found = 0; |
| 1190 | |
| 1191 | if (counters) |
| 1192 | counters = -1; /* iptables -c format */ |
| 1193 | |
| 1194 | /* Dump out chain names first, |
| 1195 | * thereby preventing dependency conflicts */ |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1196 | if (!rulenum) for (this = iptc_first_chain(handle); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1197 | this; |
| 1198 | this = iptc_next_chain(handle)) { |
| 1199 | if (chain && strcmp(this, chain) != 0) |
| 1200 | continue; |
| 1201 | |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1202 | if (iptc_builtin(this, handle)) { |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1203 | struct ipt_counters count; |
| 1204 | printf("-P %s %s", this, iptc_get_policy(this, &count, handle)); |
| 1205 | if (counters) |
| 1206 | printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt); |
| 1207 | printf("\n"); |
| 1208 | } else { |
| 1209 | printf("-N %s\n", this); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | for (this = iptc_first_chain(handle); |
| 1214 | this; |
| 1215 | this = iptc_next_chain(handle)) { |
| 1216 | const struct ipt_entry *e; |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1217 | int num = 0; |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1218 | |
| 1219 | if (chain && strcmp(this, chain) != 0) |
| 1220 | continue; |
| 1221 | |
| 1222 | /* Dump out rules */ |
| 1223 | e = iptc_first_rule(this, handle); |
| 1224 | while(e) { |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1225 | num++; |
| 1226 | if (!rulenum || num == rulenum) |
| 1227 | print_rule(e, handle, this, counters); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1228 | e = iptc_next_rule(e, handle); |
| 1229 | } |
| 1230 | found = 1; |
| 1231 | } |
| 1232 | |
| 1233 | errno = ENOENT; |
| 1234 | return found; |
| 1235 | } |
| 1236 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1237 | static struct ipt_entry * |
| 1238 | generate_entry(const struct ipt_entry *fw, |
Jan Engelhardt | 395e441 | 2009-02-10 10:43:08 +0100 | [diff] [blame] | 1239 | struct xtables_rule_match *matches, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1240 | struct ipt_entry_target *target) |
| 1241 | { |
| 1242 | unsigned int size; |
Jan Engelhardt | 395e441 | 2009-02-10 10:43:08 +0100 | [diff] [blame] | 1243 | struct xtables_rule_match *matchp; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1244 | struct ipt_entry *e; |
| 1245 | |
| 1246 | size = sizeof(struct ipt_entry); |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1247 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1248 | size += matchp->match->m->u.match_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1249 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 1250 | e = xtables_malloc(size + target->u.target_size); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1251 | *e = *fw; |
| 1252 | e->target_offset = size; |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1253 | e->next_offset = size + target->u.target_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1254 | |
| 1255 | size = 0; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1256 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 1257 | memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size); |
| 1258 | size += matchp->match->m->u.match_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1259 | } |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1260 | memcpy(e->elems + size, target, target->u.target_size); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1261 | |
| 1262 | return e; |
| 1263 | } |
| 1264 | |
Jan Engelhardt | 395e441 | 2009-02-10 10:43:08 +0100 | [diff] [blame] | 1265 | static void clear_rule_matches(struct xtables_rule_match **matches) |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1266 | { |
Jan Engelhardt | 395e441 | 2009-02-10 10:43:08 +0100 | [diff] [blame] | 1267 | struct xtables_rule_match *matchp, *tmp; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1268 | |
| 1269 | for (matchp = *matches; matchp;) { |
| 1270 | tmp = matchp->next; |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1271 | if (matchp->match->m) { |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1272 | free(matchp->match->m); |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1273 | matchp->match->m = NULL; |
| 1274 | } |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1275 | if (matchp->match == matchp->match->next) { |
| 1276 | free(matchp->match); |
| 1277 | matchp->match = NULL; |
| 1278 | } |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1279 | free(matchp); |
| 1280 | matchp = tmp; |
| 1281 | } |
| 1282 | |
| 1283 | *matches = NULL; |
| 1284 | } |
| 1285 | |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 1286 | void |
| 1287 | get_kernel_version(void) { |
| 1288 | static struct utsname uts; |
| 1289 | int x = 0, y = 0, z = 0; |
| 1290 | |
| 1291 | if (uname(&uts) == -1) { |
| 1292 | fprintf(stderr, "Unable to retrieve kernel version.\n"); |
Jamal Hadi Salim | 139b3fe | 2009-02-12 11:43:01 -0500 | [diff] [blame] | 1293 | xtables_free_opts(1); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1294 | exit(1); |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | sscanf(uts.release, "%d.%d.%d", &x, &y, &z); |
| 1298 | kernel_version = LINUX_VERSION(x, y, z); |
| 1299 | } |
| 1300 | |
Jan Engelhardt | f935ae0 | 2011-02-06 17:14:48 +0100 | [diff] [blame] | 1301 | static void command_default(struct iptables_command_state *cs) |
| 1302 | { |
| 1303 | struct xtables_rule_match *matchp; |
| 1304 | struct xtables_match *m; |
| 1305 | |
Jan Engelhardt | af3d73e | 2011-02-11 01:45:26 +0100 | [diff] [blame] | 1306 | if (cs->target != NULL && cs->target->parse != NULL && |
| 1307 | cs->c >= cs->target->option_offset && |
| 1308 | cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) { |
Jan Engelhardt | 94e247b | 2011-02-07 03:20:02 +0100 | [diff] [blame] | 1309 | cs->target->parse(cs->c - cs->target->option_offset, cs->argv, |
| 1310 | cs->invert, &cs->target->tflags, &cs->fw, |
| 1311 | &cs->target->t); |
| 1312 | return; |
Jan Engelhardt | f935ae0 | 2011-02-06 17:14:48 +0100 | [diff] [blame] | 1313 | } |
Jan Engelhardt | 94e247b | 2011-02-07 03:20:02 +0100 | [diff] [blame] | 1314 | |
| 1315 | for (matchp = cs->matches; matchp; matchp = matchp->next) { |
| 1316 | m = matchp->match; |
| 1317 | |
| 1318 | if (matchp->completed || m->parse == NULL) |
| 1319 | continue; |
| 1320 | if (cs->c < m->option_offset || |
| 1321 | cs->c >= m->option_offset + XT_OPTION_OFFSET_SCALE) |
| 1322 | continue; |
| 1323 | m->parse(cs->c - m->option_offset, cs->argv, cs->invert, |
| 1324 | &m->mflags, &cs->fw, &m->m); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | /* Try loading protocol */ |
| 1329 | m = load_proto(cs); |
| 1330 | if (m != NULL) { |
| 1331 | size_t size; |
| 1332 | |
| 1333 | cs->proto_used = 1; |
| 1334 | |
| 1335 | size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size; |
| 1336 | |
| 1337 | m->m = xtables_calloc(1, size); |
| 1338 | m->m->u.match_size = size; |
| 1339 | strcpy(m->m->u.user.name, m->name); |
| 1340 | m->m->u.user.revision = m->revision; |
| 1341 | if (m->init != NULL) |
| 1342 | m->init(m->m); |
| 1343 | |
| 1344 | opts = xtables_merge_options(iptables_globals.orig_opts, opts, |
| 1345 | m->extra_opts, &m->option_offset); |
| 1346 | if (opts == NULL) |
| 1347 | xtables_error(OTHER_PROBLEM, "can't alloc memory!"); |
| 1348 | |
| 1349 | optind--; |
| 1350 | return; |
| 1351 | } |
| 1352 | |
Jan Engelhardt | 58b491f | 2011-02-07 03:45:26 +0100 | [diff] [blame] | 1353 | if (cs->c == ':') |
| 1354 | xtables_error(PARAMETER_PROBLEM, "option \"%s\" " |
| 1355 | "requires an argument", cs->argv[optind-1]); |
| 1356 | if (cs->c == '?') |
| 1357 | xtables_error(PARAMETER_PROBLEM, "unknown option " |
| 1358 | "\"%s\"", cs->argv[optind-1]); |
Jan Engelhardt | 94e247b | 2011-02-07 03:20:02 +0100 | [diff] [blame] | 1359 | xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg); |
Jan Engelhardt | f935ae0 | 2011-02-06 17:14:48 +0100 | [diff] [blame] | 1360 | } |
| 1361 | |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1362 | static void command_jump(struct iptables_command_state *cs) |
| 1363 | { |
| 1364 | size_t size; |
| 1365 | |
| 1366 | set_option(&cs->options, OPT_JUMP, &cs->fw.ip.invflags, cs->invert); |
| 1367 | cs->jumpto = parse_target(optarg); |
| 1368 | /* TRY_LOAD (may be chain name) */ |
| 1369 | cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD); |
| 1370 | |
| 1371 | if (cs->target == NULL) |
| 1372 | return; |
| 1373 | |
| 1374 | size = IPT_ALIGN(sizeof(struct ipt_entry_target)) |
| 1375 | + cs->target->size; |
| 1376 | |
| 1377 | cs->target->t = xtables_calloc(1, size); |
| 1378 | cs->target->t->u.target_size = size; |
| 1379 | strcpy(cs->target->t->u.user.name, cs->jumpto); |
| 1380 | cs->target->t->u.user.revision = cs->target->revision; |
| 1381 | if (cs->target->init != NULL) |
| 1382 | cs->target->init(cs->target->t); |
| 1383 | opts = xtables_merge_options(iptables_globals.orig_opts, opts, |
| 1384 | cs->target->extra_opts, |
| 1385 | &cs->target->option_offset); |
| 1386 | if (opts == NULL) |
| 1387 | xtables_error(OTHER_PROBLEM, "can't alloc memory!"); |
| 1388 | } |
| 1389 | |
Jan Engelhardt | 17e310b | 2011-02-07 03:42:47 +0100 | [diff] [blame] | 1390 | static void command_match(struct iptables_command_state *cs) |
| 1391 | { |
| 1392 | struct xtables_match *m; |
| 1393 | size_t size; |
| 1394 | |
| 1395 | if (cs->invert) |
| 1396 | xtables_error(PARAMETER_PROBLEM, |
| 1397 | "unexpected ! flag before --match"); |
| 1398 | |
| 1399 | m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches); |
| 1400 | size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size; |
| 1401 | m->m = xtables_calloc(1, size); |
| 1402 | m->m->u.match_size = size; |
| 1403 | strcpy(m->m->u.user.name, m->name); |
| 1404 | m->m->u.user.revision = m->revision; |
| 1405 | if (m->init != NULL) |
| 1406 | m->init(m->m); |
| 1407 | if (m != m->next) { |
| 1408 | /* Merge options for non-cloned matches */ |
| 1409 | opts = xtables_merge_options(iptables_globals.orig_opts, opts, |
| 1410 | m->extra_opts, &m->option_offset); |
| 1411 | if (opts == NULL) |
| 1412 | xtables_error(OTHER_PROBLEM, "can't alloc memory!"); |
| 1413 | } |
| 1414 | } |
| 1415 | |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 1416 | int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1417 | { |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1418 | struct iptables_command_state cs; |
| 1419 | struct ipt_entry *e = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1420 | unsigned int nsaddrs = 0, ndaddrs = 0; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1421 | struct in_addr *saddrs = NULL, *smasks = NULL; |
| 1422 | struct in_addr *daddrs = NULL, *dmasks = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1423 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1424 | int verbose = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1425 | const char *chain = NULL; |
| 1426 | const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL; |
| 1427 | const char *policy = NULL, *newname = NULL; |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1428 | unsigned int rulenum = 0, command = 0; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1429 | const char *pcnt = NULL, *bcnt = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1430 | int ret = 1; |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1431 | struct xtables_match *m; |
Jan Engelhardt | 395e441 | 2009-02-10 10:43:08 +0100 | [diff] [blame] | 1432 | struct xtables_rule_match *matchp; |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1433 | struct xtables_target *t; |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1434 | unsigned long long cnt; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1435 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1436 | memset(&cs, 0, sizeof(cs)); |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1437 | cs.jumpto = ""; |
Jan Engelhardt | f935ae0 | 2011-02-06 17:14:48 +0100 | [diff] [blame] | 1438 | cs.argv = argv; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1439 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1440 | /* re-set optind to 0 in case do_command gets called |
| 1441 | * a second time */ |
| 1442 | optind = 0; |
| 1443 | |
| 1444 | /* clear mflags in case do_command gets called a second time |
| 1445 | * (we clear the global list of all matches for security)*/ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1446 | for (m = xtables_matches; m; m = m->next) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1447 | m->mflags = 0; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1448 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1449 | for (t = xtables_targets; t; t = t->next) { |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1450 | t->tflags = 0; |
| 1451 | t->used = 0; |
| 1452 | } |
| 1453 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1454 | /* Suppress error messages: we may add new options if we |
| 1455 | demand-load a protocol. */ |
| 1456 | opterr = 0; |
| 1457 | |
Jan Engelhardt | d3b2e39 | 2010-11-28 15:35:06 +0100 | [diff] [blame] | 1458 | opts = xt_params->orig_opts; |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1459 | while ((cs.c = getopt_long(argc, argv, |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 1460 | "-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:", |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1461 | opts, NULL)) != -1) { |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1462 | switch (cs.c) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | * Command selection |
| 1465 | */ |
| 1466 | case 'A': |
| 1467 | add_command(&command, CMD_APPEND, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1468 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1469 | chain = optarg; |
| 1470 | break; |
| 1471 | |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 1472 | case 'C': |
| 1473 | add_command(&command, CMD_CHECK, CMD_NONE, |
| 1474 | cs.invert); |
| 1475 | chain = optarg; |
| 1476 | break; |
| 1477 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1478 | case 'D': |
| 1479 | add_command(&command, CMD_DELETE, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1480 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1481 | chain = optarg; |
| 1482 | if (optind < argc && argv[optind][0] != '-' |
| 1483 | && argv[optind][0] != '!') { |
| 1484 | rulenum = parse_rulenumber(argv[optind++]); |
| 1485 | command = CMD_DELETE_NUM; |
| 1486 | } |
| 1487 | break; |
| 1488 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1489 | case 'R': |
| 1490 | add_command(&command, CMD_REPLACE, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1491 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1492 | chain = optarg; |
| 1493 | if (optind < argc && argv[optind][0] != '-' |
| 1494 | && argv[optind][0] != '!') |
| 1495 | rulenum = parse_rulenumber(argv[optind++]); |
| 1496 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1497 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1498 | "-%c requires a rule number", |
| 1499 | cmd2char(CMD_REPLACE)); |
| 1500 | break; |
| 1501 | |
| 1502 | case 'I': |
| 1503 | add_command(&command, CMD_INSERT, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1504 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1505 | chain = optarg; |
| 1506 | if (optind < argc && argv[optind][0] != '-' |
| 1507 | && argv[optind][0] != '!') |
| 1508 | rulenum = parse_rulenumber(argv[optind++]); |
| 1509 | else rulenum = 1; |
| 1510 | break; |
| 1511 | |
| 1512 | case 'L': |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1513 | add_command(&command, CMD_LIST, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1514 | CMD_ZERO | CMD_ZERO_NUM, cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1515 | if (optarg) chain = optarg; |
| 1516 | else if (optind < argc && argv[optind][0] != '-' |
| 1517 | && argv[optind][0] != '!') |
| 1518 | chain = argv[optind++]; |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1519 | if (optind < argc && argv[optind][0] != '-' |
| 1520 | && argv[optind][0] != '!') |
| 1521 | rulenum = parse_rulenumber(argv[optind++]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1524 | case 'S': |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1525 | add_command(&command, CMD_LIST_RULES, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1526 | CMD_ZERO|CMD_ZERO_NUM, cs.invert); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1527 | if (optarg) chain = optarg; |
| 1528 | else if (optind < argc && argv[optind][0] != '-' |
| 1529 | && argv[optind][0] != '!') |
| 1530 | chain = argv[optind++]; |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1531 | if (optind < argc && argv[optind][0] != '-' |
| 1532 | && argv[optind][0] != '!') |
| 1533 | rulenum = parse_rulenumber(argv[optind++]); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1534 | break; |
| 1535 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1536 | case 'F': |
| 1537 | add_command(&command, CMD_FLUSH, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1538 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1539 | if (optarg) chain = optarg; |
| 1540 | else if (optind < argc && argv[optind][0] != '-' |
| 1541 | && argv[optind][0] != '!') |
| 1542 | chain = argv[optind++]; |
| 1543 | break; |
| 1544 | |
| 1545 | case 'Z': |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1546 | add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1547 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1548 | if (optarg) chain = optarg; |
| 1549 | else if (optind < argc && argv[optind][0] != '-' |
| 1550 | && argv[optind][0] != '!') |
| 1551 | chain = argv[optind++]; |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1552 | if (optind < argc && argv[optind][0] != '-' |
| 1553 | && argv[optind][0] != '!') { |
| 1554 | rulenum = parse_rulenumber(argv[optind++]); |
| 1555 | command = CMD_ZERO_NUM; |
| 1556 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | |
| 1559 | case 'N': |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1560 | if (optarg && (*optarg == '-' || *optarg == '!')) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1561 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | 6336bfd | 2002-05-07 14:41:43 +0000 | [diff] [blame] | 1562 | "chain name not allowed to start " |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1563 | "with `%c'\n", *optarg); |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 1564 | if (xtables_find_target(optarg, XTF_TRY_LOAD)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1565 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1566 | "chain name may not clash " |
| 1567 | "with target name\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1568 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1569 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1570 | chain = optarg; |
| 1571 | break; |
| 1572 | |
| 1573 | case 'X': |
| 1574 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1575 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1576 | if (optarg) chain = optarg; |
| 1577 | else if (optind < argc && argv[optind][0] != '-' |
| 1578 | && argv[optind][0] != '!') |
| 1579 | chain = argv[optind++]; |
| 1580 | break; |
| 1581 | |
| 1582 | case 'E': |
| 1583 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1584 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1585 | chain = optarg; |
| 1586 | if (optind < argc && argv[optind][0] != '-' |
| 1587 | && argv[optind][0] != '!') |
| 1588 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1589 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1590 | xtables_error(PARAMETER_PROBLEM, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1591 | "-%c requires old-chain-name and " |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1592 | "new-chain-name", |
| 1593 | cmd2char(CMD_RENAME_CHAIN)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | |
| 1596 | case 'P': |
| 1597 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1598 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1599 | chain = optarg; |
| 1600 | if (optind < argc && argv[optind][0] != '-' |
| 1601 | && argv[optind][0] != '!') |
| 1602 | policy = argv[optind++]; |
| 1603 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1604 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1605 | "-%c requires a chain and a policy", |
| 1606 | cmd2char(CMD_SET_POLICY)); |
| 1607 | break; |
| 1608 | |
| 1609 | case 'h': |
| 1610 | if (!optarg) |
| 1611 | optarg = argv[optind]; |
| 1612 | |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1613 | /* iptables -p icmp -h */ |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1614 | if (!cs.matches && cs.protocol) |
| 1615 | xtables_find_match(cs.protocol, |
| 1616 | XTF_TRY_LOAD, &cs.matches); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1617 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1618 | exit_printhelp(cs.matches); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* |
| 1621 | * Option selection |
| 1622 | */ |
| 1623 | case 'p': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1624 | xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv); |
| 1625 | set_option(&cs.options, OPT_PROTOCOL, &cs.fw.ip.invflags, |
| 1626 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1627 | |
| 1628 | /* Canonicalize into lower case */ |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1629 | for (cs.protocol = optarg; *cs.protocol; cs.protocol++) |
| 1630 | *cs.protocol = tolower(*cs.protocol); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1631 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1632 | cs.protocol = optarg; |
| 1633 | cs.fw.ip.proto = xtables_parse_protocol(cs.protocol); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1634 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1635 | if (cs.fw.ip.proto == 0 |
| 1636 | && (cs.fw.ip.invflags & IPT_INV_PROTO)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1637 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1638 | "rule would never match protocol"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | |
| 1641 | case 's': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1642 | xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv); |
| 1643 | set_option(&cs.options, OPT_SOURCE, &cs.fw.ip.invflags, |
| 1644 | cs.invert); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 1645 | shostnetworkmask = optarg; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | |
| 1648 | case 'd': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1649 | xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv); |
| 1650 | set_option(&cs.options, OPT_DESTINATION, &cs.fw.ip.invflags, |
| 1651 | cs.invert); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 1652 | dhostnetworkmask = optarg; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1655 | #ifdef IPT_F_GOTO |
| 1656 | case 'g': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1657 | set_option(&cs.options, OPT_JUMP, &cs.fw.ip.invflags, |
| 1658 | cs.invert); |
| 1659 | cs.fw.ip.flags |= IPT_F_GOTO; |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1660 | cs.jumpto = parse_target(optarg); |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | #endif |
| 1663 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1664 | case 'j': |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1665 | command_jump(&cs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | |
| 1668 | |
| 1669 | case 'i': |
Jan Engelhardt | 5b1fecc | 2011-01-07 12:26:59 +0100 | [diff] [blame] | 1670 | if (*optarg == '\0') |
| 1671 | xtables_error(PARAMETER_PROBLEM, |
| 1672 | "Empty interface is likely to be " |
| 1673 | "undesired"); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1674 | xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv); |
| 1675 | set_option(&cs.options, OPT_VIANAMEIN, &cs.fw.ip.invflags, |
| 1676 | cs.invert); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 1677 | xtables_parse_interface(optarg, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1678 | cs.fw.ip.iniface, |
| 1679 | cs.fw.ip.iniface_mask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1680 | break; |
| 1681 | |
| 1682 | case 'o': |
Jan Engelhardt | 5b1fecc | 2011-01-07 12:26:59 +0100 | [diff] [blame] | 1683 | if (*optarg == '\0') |
| 1684 | xtables_error(PARAMETER_PROBLEM, |
| 1685 | "Empty interface is likely to be " |
| 1686 | "undesired"); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1687 | xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv); |
| 1688 | set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw.ip.invflags, |
| 1689 | cs.invert); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 1690 | xtables_parse_interface(optarg, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1691 | cs.fw.ip.outiface, |
| 1692 | cs.fw.ip.outiface_mask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | |
| 1695 | case 'f': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1696 | set_option(&cs.options, OPT_FRAGMENT, &cs.fw.ip.invflags, |
| 1697 | cs.invert); |
| 1698 | cs.fw.ip.flags |= IPT_F_FRAG; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | |
| 1701 | case 'v': |
| 1702 | if (!verbose) |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1703 | set_option(&cs.options, OPT_VERBOSE, |
| 1704 | &cs.fw.ip.invflags, cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1705 | verbose++; |
| 1706 | break; |
| 1707 | |
Jan Engelhardt | 17e310b | 2011-02-07 03:42:47 +0100 | [diff] [blame] | 1708 | case 'm': |
| 1709 | command_match(&cs); |
| 1710 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1711 | |
| 1712 | case 'n': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1713 | set_option(&cs.options, OPT_NUMERIC, &cs.fw.ip.invflags, |
| 1714 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1715 | break; |
| 1716 | |
| 1717 | case 't': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1718 | if (cs.invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1719 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1720 | "unexpected ! flag before --table"); |
Jan Engelhardt | d0cbf5f | 2008-08-04 12:51:01 +0200 | [diff] [blame] | 1721 | *table = optarg; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1722 | break; |
| 1723 | |
| 1724 | case 'x': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1725 | set_option(&cs.options, OPT_EXPANDED, &cs.fw.ip.invflags, |
| 1726 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1727 | break; |
| 1728 | |
| 1729 | case 'V': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1730 | if (cs.invert) |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 1731 | printf("Not %s ;-)\n", prog_vers); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1732 | else |
| 1733 | printf("%s v%s\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 1734 | prog_name, prog_vers); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1735 | exit(0); |
| 1736 | |
| 1737 | case '0': |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1738 | set_option(&cs.options, OPT_LINENUMBERS, &cs.fw.ip.invflags, |
| 1739 | cs.invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1740 | break; |
| 1741 | |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1742 | case 'M': |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 1743 | xtables_modprobe_program = optarg; |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1746 | case 'c': |
| 1747 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1748 | set_option(&cs.options, OPT_COUNTERS, &cs.fw.ip.invflags, |
| 1749 | cs.invert); |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1750 | pcnt = optarg; |
Henrik Nordstrom | 60a6073 | 2008-05-13 13:10:38 +0200 | [diff] [blame] | 1751 | bcnt = strchr(pcnt + 1, ','); |
| 1752 | if (bcnt) |
| 1753 | bcnt++; |
| 1754 | if (!bcnt && optind < argc && argv[optind][0] != '-' |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1755 | && argv[optind][0] != '!') |
| 1756 | bcnt = argv[optind++]; |
Henrik Nordstrom | 60a6073 | 2008-05-13 13:10:38 +0200 | [diff] [blame] | 1757 | if (!bcnt) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1758 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1759 | "-%c requires packet and byte counter", |
| 1760 | opt2char(OPT_COUNTERS)); |
| 1761 | |
Henrik Nordstrom | 60a6073 | 2008-05-13 13:10:38 +0200 | [diff] [blame] | 1762 | if (sscanf(pcnt, "%llu", &cnt) != 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1763 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1764 | "-%c packet counter not numeric", |
| 1765 | opt2char(OPT_COUNTERS)); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1766 | cs.fw.counters.pcnt = cnt; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1767 | |
Henrik Nordstrom | 60a6073 | 2008-05-13 13:10:38 +0200 | [diff] [blame] | 1768 | if (sscanf(bcnt, "%llu", &cnt) != 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1769 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1770 | "-%c byte counter not numeric", |
| 1771 | opt2char(OPT_COUNTERS)); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1772 | cs.fw.counters.bcnt = cnt; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1773 | break; |
| 1774 | |
| 1775 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1776 | case 1: /* non option */ |
| 1777 | if (optarg[0] == '!' && optarg[1] == '\0') { |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1778 | if (cs.invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1779 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1780 | "multiple consecutive ! not" |
| 1781 | " allowed"); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1782 | cs.invert = TRUE; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1783 | optarg[0] = '\0'; |
| 1784 | continue; |
| 1785 | } |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1786 | fprintf(stderr, "Bad argument `%s'\n", optarg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1787 | exit_tryhelp(2); |
| 1788 | |
| 1789 | default: |
Jan Engelhardt | f935ae0 | 2011-02-06 17:14:48 +0100 | [diff] [blame] | 1790 | command_default(&cs); |
| 1791 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1792 | } |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1793 | cs.invert = FALSE; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Jan Engelhardt | 1eada72 | 2008-08-13 14:41:32 +0200 | [diff] [blame] | 1796 | if (strcmp(*table, "nat") == 0 && |
| 1797 | ((policy != NULL && strcmp(policy, "DROP") == 0) || |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1798 | (cs.jumpto != NULL && strcmp(cs.jumpto, "DROP") == 0))) |
Jan Engelhardt | e0390be | 2009-03-15 21:22:49 +0100 | [diff] [blame] | 1799 | xtables_error(PARAMETER_PROBLEM, |
| 1800 | "\nThe \"nat\" table is not intended for filtering, " |
| 1801 | "the use of DROP is therefore inhibited.\n\n"); |
Jan Engelhardt | 1eada72 | 2008-08-13 14:41:32 +0200 | [diff] [blame] | 1802 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1803 | for (matchp = cs.matches; matchp; matchp = matchp->next) |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1804 | if (matchp->match->final_check != NULL) |
| 1805 | matchp->match->final_check(matchp->match->mflags); |
Sven Koch | fb1279a | 2001-02-27 12:25:12 +0000 | [diff] [blame] | 1806 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1807 | if (cs.target != NULL && cs.target->final_check != NULL) |
| 1808 | cs.target->final_check(cs.target->tflags); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1809 | |
| 1810 | /* Fix me: must put inverse options checking here --MN */ |
| 1811 | |
| 1812 | if (optind < argc) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1813 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1814 | "unknown arguments found on commandline"); |
| 1815 | if (!command) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1816 | xtables_error(PARAMETER_PROBLEM, "no command specified"); |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1817 | if (cs.invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1818 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1819 | "nothing appropriate following !"); |
| 1820 | |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 1821 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND | CMD_CHECK)) { |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1822 | if (!(cs.options & OPT_DESTINATION)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1823 | dhostnetworkmask = "0.0.0.0/0"; |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1824 | if (!(cs.options & OPT_SOURCE)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1825 | shostnetworkmask = "0.0.0.0/0"; |
| 1826 | } |
| 1827 | |
| 1828 | if (shostnetworkmask) |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1829 | xtables_ipparse_multiple(shostnetworkmask, &saddrs, |
| 1830 | &smasks, &nsaddrs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1831 | |
| 1832 | if (dhostnetworkmask) |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1833 | xtables_ipparse_multiple(dhostnetworkmask, &daddrs, |
| 1834 | &dmasks, &ndaddrs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1835 | |
| 1836 | if ((nsaddrs > 1 || ndaddrs > 1) && |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1837 | (cs.fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP))) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1838 | xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1839 | " source or destination IP addresses"); |
| 1840 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1841 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1842 | xtables_error(PARAMETER_PROBLEM, "Replacement rule does not " |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1843 | "specify a unique address"); |
| 1844 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1845 | generic_opt_check(command, cs.options); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1846 | |
Jan Engelhardt | 5429b41 | 2010-09-13 15:45:15 +0200 | [diff] [blame] | 1847 | if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1848 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 5429b41 | 2010-09-13 15:45:15 +0200 | [diff] [blame] | 1849 | "chain name `%s' too long (must be under %u chars)", |
| 1850 | chain, XT_EXTENSION_MAXNAMELEN); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1851 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1852 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 1853 | if (!*handle) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1854 | *handle = iptc_init(*table); |
| 1855 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1856 | /* try to insmod the module if iptc_init failed */ |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 1857 | if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1) |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1858 | *handle = iptc_init(*table); |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1859 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1860 | if (!*handle) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1861 | xtables_error(VERSION_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1862 | "can't initialize iptables table `%s': %s", |
| 1863 | *table, iptc_strerror(errno)); |
| 1864 | |
Harald Welte | 6336bfd | 2002-05-07 14:41:43 +0000 | [diff] [blame] | 1865 | if (command == CMD_APPEND |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1866 | || command == CMD_DELETE |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 1867 | || command == CMD_CHECK |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1868 | || command == CMD_INSERT |
| 1869 | || command == CMD_REPLACE) { |
Rusty Russell | a4860fd | 2000-06-17 16:13:02 +0000 | [diff] [blame] | 1870 | if (strcmp(chain, "PREROUTING") == 0 |
| 1871 | || strcmp(chain, "INPUT") == 0) { |
| 1872 | /* -o not valid with incoming packets. */ |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1873 | if (cs.options & OPT_VIANAMEOUT) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1874 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1875 | "Can't use -%c with %s\n", |
| 1876 | opt2char(OPT_VIANAMEOUT), |
| 1877 | chain); |
| 1878 | } |
| 1879 | |
Rusty Russell | a4860fd | 2000-06-17 16:13:02 +0000 | [diff] [blame] | 1880 | if (strcmp(chain, "POSTROUTING") == 0 |
| 1881 | || strcmp(chain, "OUTPUT") == 0) { |
| 1882 | /* -i not valid with outgoing packets */ |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1883 | if (cs.options & OPT_VIANAMEIN) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1884 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1885 | "Can't use -%c with %s\n", |
| 1886 | opt2char(OPT_VIANAMEIN), |
| 1887 | chain); |
| 1888 | } |
| 1889 | |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1890 | if (cs.target && iptc_is_chain(cs.jumpto, *handle)) { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1891 | fprintf(stderr, |
| 1892 | "Warning: using chain %s, not extension\n", |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1893 | cs.jumpto); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1894 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1895 | if (cs.target->t) |
| 1896 | free(cs.target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1897 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1898 | cs.target = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* If they didn't specify a target, or it's a chain |
| 1902 | name, use standard. */ |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1903 | if (!cs.target |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1904 | && (strlen(cs.jumpto) == 0 |
| 1905 | || iptc_is_chain(cs.jumpto, *handle))) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1906 | size_t size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1907 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1908 | cs.target = xtables_find_target(IPT_STANDARD_TARGET, |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 1909 | XTF_LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1910 | |
| 1911 | size = sizeof(struct ipt_entry_target) |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1912 | + cs.target->size; |
| 1913 | cs.target->t = xtables_calloc(1, size); |
| 1914 | cs.target->t->u.target_size = size; |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1915 | strcpy(cs.target->t->u.user.name, cs.jumpto); |
| 1916 | if (!iptc_is_chain(cs.jumpto, *handle)) |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1917 | cs.target->t->u.user.revision = cs.target->revision; |
| 1918 | if (cs.target->init != NULL) |
| 1919 | cs.target->init(cs.target->t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1922 | if (!cs.target) { |
Harald Welte | f2a24bd | 2000-08-30 02:11:18 +0000 | [diff] [blame] | 1923 | /* it is no chain, and we can't load a plugin. |
| 1924 | * We cannot know if the plugin is corrupt, non |
Rusty Russell | a4d3e1f | 2001-01-07 06:56:02 +0000 | [diff] [blame] | 1925 | * existant OR if the user just misspelled a |
Harald Welte | f2a24bd | 2000-08-30 02:11:18 +0000 | [diff] [blame] | 1926 | * chain. */ |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1927 | #ifdef IPT_F_GOTO |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1928 | if (cs.fw.ip.flags & IPT_F_GOTO) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 1929 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1930 | "goto '%s' is not a chain\n", |
| 1931 | cs.jumpto); |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1932 | #endif |
Jan Engelhardt | 9bb7609 | 2011-02-07 03:39:36 +0100 | [diff] [blame] | 1933 | xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1934 | } else { |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1935 | e = generate_entry(&cs.fw, cs.matches, cs.target->t); |
| 1936 | free(cs.target->t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | switch (command) { |
| 1941 | case CMD_APPEND: |
| 1942 | ret = append_entry(chain, e, |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1943 | nsaddrs, saddrs, smasks, |
| 1944 | ndaddrs, daddrs, dmasks, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1945 | cs.options&OPT_VERBOSE, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1946 | *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1947 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1948 | case CMD_DELETE: |
| 1949 | ret = delete_entry(chain, e, |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1950 | nsaddrs, saddrs, smasks, |
| 1951 | ndaddrs, daddrs, dmasks, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1952 | cs.options&OPT_VERBOSE, |
| 1953 | *handle, cs.matches, cs.target); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1954 | break; |
| 1955 | case CMD_DELETE_NUM: |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1956 | ret = iptc_delete_num_entry(chain, rulenum - 1, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1957 | break; |
Stefan Tomanek | d59b9db | 2011-03-08 22:42:51 +0100 | [diff] [blame] | 1958 | case CMD_CHECK: |
| 1959 | ret = check_entry(chain, e, |
| 1960 | nsaddrs, saddrs, smasks, |
| 1961 | ndaddrs, daddrs, dmasks, |
| 1962 | cs.options&OPT_VERBOSE, |
| 1963 | *handle, cs.matches, cs.target); |
| 1964 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1965 | case CMD_REPLACE: |
| 1966 | ret = replace_entry(chain, e, rulenum - 1, |
Jan Engelhardt | 75cb763 | 2009-11-15 15:51:27 +0100 | [diff] [blame] | 1967 | saddrs, smasks, daddrs, dmasks, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1968 | cs.options&OPT_VERBOSE, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1969 | break; |
| 1970 | case CMD_INSERT: |
| 1971 | ret = insert_entry(chain, e, rulenum - 1, |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1972 | nsaddrs, saddrs, smasks, |
| 1973 | ndaddrs, daddrs, dmasks, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1974 | cs.options&OPT_VERBOSE, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1975 | *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1976 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1977 | case CMD_FLUSH: |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1978 | ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1979 | break; |
| 1980 | case CMD_ZERO: |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1981 | ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1982 | break; |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1983 | case CMD_ZERO_NUM: |
| 1984 | ret = iptc_zero_counter(chain, rulenum, *handle); |
| 1985 | break; |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1986 | case CMD_LIST: |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1987 | case CMD_LIST|CMD_ZERO: |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1988 | case CMD_LIST|CMD_ZERO_NUM: |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1989 | ret = list_entries(chain, |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 1990 | rulenum, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1991 | cs.options&OPT_VERBOSE, |
| 1992 | cs.options&OPT_NUMERIC, |
| 1993 | cs.options&OPT_EXPANDED, |
| 1994 | cs.options&OPT_LINENUMBERS, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 1995 | *handle); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 1996 | if (ret && (command & CMD_ZERO)) |
| 1997 | ret = zero_entries(chain, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 1998 | cs.options&OPT_VERBOSE, *handle); |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 1999 | if (ret && (command & CMD_ZERO_NUM)) |
| 2000 | ret = iptc_zero_counter(chain, rulenum, *handle); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 2001 | break; |
| 2002 | case CMD_LIST_RULES: |
| 2003 | case CMD_LIST_RULES|CMD_ZERO: |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 2004 | case CMD_LIST_RULES|CMD_ZERO_NUM: |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 2005 | ret = list_rules(chain, |
Henrik Nordstrom | bb34082 | 2008-05-13 13:09:23 +0200 | [diff] [blame] | 2006 | rulenum, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 2007 | cs.options&OPT_VERBOSE, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 2008 | *handle); |
Henrik Nordstrom | 96296cf | 2008-05-13 13:08:26 +0200 | [diff] [blame] | 2009 | if (ret && (command & CMD_ZERO)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2010 | ret = zero_entries(chain, |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 2011 | cs.options&OPT_VERBOSE, *handle); |
Mohit Mehta | b34199e | 2009-08-19 10:56:33 -0700 | [diff] [blame] | 2012 | if (ret && (command & CMD_ZERO_NUM)) |
| 2013 | ret = iptc_zero_counter(chain, rulenum, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | case CMD_NEW_CHAIN: |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 2016 | ret = iptc_create_chain(chain, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2017 | break; |
| 2018 | case CMD_DELETE_CHAIN: |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 2019 | ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2020 | break; |
| 2021 | case CMD_RENAME_CHAIN: |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame] | 2022 | ret = iptc_rename_chain(chain, newname, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | case CMD_SET_POLICY: |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 2025 | ret = iptc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw.counters : NULL, *handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2026 | break; |
| 2027 | default: |
| 2028 | /* We should never reach this... */ |
| 2029 | exit_tryhelp(2); |
| 2030 | } |
| 2031 | |
| 2032 | if (verbose > 1) |
| 2033 | dump_entries(*handle); |
| 2034 | |
Jan Engelhardt | 3a9d8b0 | 2011-02-06 15:52:11 +0100 | [diff] [blame] | 2035 | clear_rule_matches(&cs.matches); |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 2036 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2037 | if (e != NULL) { |
| 2038 | free(e); |
| 2039 | e = NULL; |
| 2040 | } |
| 2041 | |
keso | 6997cdf | 2004-07-04 15:20:53 +0000 | [diff] [blame] | 2042 | free(saddrs); |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 2043 | free(smasks); |
keso | 6997cdf | 2004-07-04 15:20:53 +0000 | [diff] [blame] | 2044 | free(daddrs); |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 2045 | free(dmasks); |
Jamal Hadi Salim | 139b3fe | 2009-02-12 11:43:01 -0500 | [diff] [blame] | 2046 | xtables_free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2047 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2048 | return ret; |
| 2049 | } |