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