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[] = { |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 102 | { "append", 1, NULL, 'A' }, |
| 103 | { "delete", 1, NULL, 'D' }, |
| 104 | { "insert", 1, NULL, 'I' }, |
| 105 | { "replace", 1, NULL, 'R' }, |
| 106 | { "list", 2, NULL, 'L' }, |
| 107 | { "flush", 2, NULL, 'F' }, |
| 108 | { "zero", 2, NULL, 'Z' }, |
| 109 | { "new-chain", 1, NULL, 'N' }, |
| 110 | { "delete-chain", 2, NULL, 'X' }, |
| 111 | { "rename-chain", 1, NULL, 'E' }, |
| 112 | { "policy", 1, NULL, 'P' }, |
| 113 | { "source", 1, NULL, 's' }, |
| 114 | { "destination", 1, NULL, 'd' }, |
| 115 | { "src", 1, NULL, 's' }, /* synonym */ |
| 116 | { "dst", 1, NULL, 'd' }, /* synonym */ |
| 117 | { "protocol", 1, NULL, 'p' }, |
| 118 | { "in-interface", 1, NULL, 'i' }, |
| 119 | { "jump", 1, NULL, 'j' }, |
| 120 | { "table", 1, NULL, 't' }, |
| 121 | { "match", 1, NULL, 'm' }, |
| 122 | { "numeric", 0, NULL, 'n' }, |
| 123 | { "out-interface", 1, NULL, 'o' }, |
| 124 | { "verbose", 0, NULL, 'v' }, |
| 125 | { "exact", 0, NULL, 'x' }, |
| 126 | { "fragments", 0, NULL, 'f' }, |
| 127 | { "version", 0, NULL, 'V' }, |
| 128 | { "help", 2, NULL, 'h' }, |
| 129 | { "line-numbers", 0, NULL, '0' }, |
| 130 | { "modprobe", 1, NULL, 'M' }, |
| 131 | { "set-counters", 1, NULL, 'c' }, |
| 132 | { "goto", 1, NULL, 'g' }, |
| 133 | { } |
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; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 279 | struct iptables_target *t = NULL; |
| 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'. */ |
| 477 | static struct iptables_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 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 615 | void register_match(struct iptables_match *me) |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 616 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 617 | me->family = PF_INET; |
| 618 | xtables_register_match(me); |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 621 | void register_target(struct iptables_target *me) |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 622 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 623 | me->family = PF_INET; |
| 624 | xtables_register_target(me); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 628 | print_num(u_int64_t number, unsigned int format) |
| 629 | { |
| 630 | if (format & FMT_KILOMEGAGIGA) { |
| 631 | if (number > 99999) { |
| 632 | number = (number + 500) / 1000; |
| 633 | if (number > 9999) { |
| 634 | number = (number + 500) / 1000; |
| 635 | if (number > 9999) { |
| 636 | number = (number + 500) / 1000; |
Rusty Russell | 5a66fe4 | 2001-08-15 11:21:59 +0000 | [diff] [blame] | 637 | if (number > 9999) { |
| 638 | number = (number + 500) / 1000; |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 639 | printf(FMT("%4lluT ","%lluT "), (unsigned long long)number); |
Rusty Russell | 5a66fe4 | 2001-08-15 11:21:59 +0000 | [diff] [blame] | 640 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 641 | else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 642 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 643 | else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 644 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 645 | printf(FMT("%4lluK ","%lluK "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 646 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 647 | printf(FMT("%5llu ","%llu "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 648 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 649 | printf(FMT("%8llu ","%llu "), (unsigned long long)number); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | static void |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 654 | print_header(unsigned int format, const char *chain, iptc_handle_t *handle) |
| 655 | { |
| 656 | struct ipt_counters counters; |
| 657 | const char *pol = iptc_get_policy(chain, &counters, handle); |
| 658 | printf("Chain %s", chain); |
| 659 | if (pol) { |
| 660 | printf(" (policy %s", pol); |
Harald Welte | a0b4f79 | 2001-03-25 19:55:04 +0000 | [diff] [blame] | 661 | if (!(format & FMT_NOCOUNTS)) { |
| 662 | fputc(' ', stdout); |
| 663 | print_num(counters.pcnt, (format|FMT_NOTABLE)); |
| 664 | fputs("packets, ", stdout); |
| 665 | print_num(counters.bcnt, (format|FMT_NOTABLE)); |
| 666 | fputs("bytes", stdout); |
| 667 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 668 | printf(")\n"); |
| 669 | } else { |
| 670 | unsigned int refs; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 671 | if (!iptc_get_references(&refs, chain, handle)) |
| 672 | printf(" (ERROR obtaining refs)\n"); |
| 673 | else |
| 674 | printf(" (%u references)\n", refs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (format & FMT_LINENUMBERS) |
| 678 | printf(FMT("%-4s ", "%s "), "num"); |
| 679 | if (!(format & FMT_NOCOUNTS)) { |
| 680 | if (format & FMT_KILOMEGAGIGA) { |
| 681 | printf(FMT("%5s ","%s "), "pkts"); |
| 682 | printf(FMT("%5s ","%s "), "bytes"); |
| 683 | } else { |
| 684 | printf(FMT("%8s ","%s "), "pkts"); |
| 685 | printf(FMT("%10s ","%s "), "bytes"); |
| 686 | } |
| 687 | } |
| 688 | if (!(format & FMT_NOTARGET)) |
| 689 | printf(FMT("%-9s ","%s "), "target"); |
| 690 | fputs(" prot ", stdout); |
| 691 | if (format & FMT_OPTIONS) |
| 692 | fputs("opt", stdout); |
| 693 | if (format & FMT_VIA) { |
| 694 | printf(FMT(" %-6s ","%s "), "in"); |
| 695 | printf(FMT("%-6s ","%s "), "out"); |
| 696 | } |
| 697 | printf(FMT(" %-19s ","%s "), "source"); |
| 698 | printf(FMT(" %-19s "," %s "), "destination"); |
| 699 | printf("\n"); |
| 700 | } |
| 701 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 702 | |
| 703 | static int |
| 704 | print_match(const struct ipt_entry_match *m, |
| 705 | const struct ipt_ip *ip, |
| 706 | int numeric) |
| 707 | { |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 708 | struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 709 | |
| 710 | if (match) { |
| 711 | if (match->print) |
| 712 | match->print(ip, m, numeric); |
Rusty Russell | 629149f | 2000-09-01 06:01:00 +0000 | [diff] [blame] | 713 | else |
Rusty Russell | b039b02 | 2000-09-01 06:04:05 +0000 | [diff] [blame] | 714 | printf("%s ", match->name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 715 | } else { |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 716 | if (m->u.user.name[0]) |
| 717 | printf("UNKNOWN match `%s' ", m->u.user.name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 718 | } |
| 719 | /* Don't stop iterating. */ |
| 720 | return 0; |
| 721 | } |
| 722 | |
Jan Engelhardt | 6cf172e | 2008-03-10 17:48:59 +0100 | [diff] [blame^] | 723 | /* e is called `fw' here for historical reasons */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 724 | static void |
| 725 | print_firewall(const struct ipt_entry *fw, |
| 726 | const char *targname, |
| 727 | unsigned int num, |
| 728 | unsigned int format, |
| 729 | const iptc_handle_t handle) |
| 730 | { |
| 731 | struct iptables_target *target = NULL; |
| 732 | const struct ipt_entry_target *t; |
| 733 | u_int8_t flags; |
| 734 | char buf[BUFSIZ]; |
| 735 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 736 | if (!iptc_is_chain(targname, handle)) |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 737 | target = find_target(targname, TRY_LOAD); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 738 | else |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 739 | target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 740 | |
| 741 | t = ipt_get_target((struct ipt_entry *)fw); |
| 742 | flags = fw->ip.flags; |
| 743 | |
| 744 | if (format & FMT_LINENUMBERS) |
| 745 | printf(FMT("%-4u ", "%u "), num+1); |
| 746 | |
| 747 | if (!(format & FMT_NOCOUNTS)) { |
| 748 | print_num(fw->counters.pcnt, format); |
| 749 | print_num(fw->counters.bcnt, format); |
| 750 | } |
| 751 | |
| 752 | if (!(format & FMT_NOTARGET)) |
| 753 | printf(FMT("%-9s ", "%s "), targname); |
| 754 | |
| 755 | fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout); |
| 756 | { |
Rusty Russell | 28381a4 | 2000-05-10 00:19:50 +0000 | [diff] [blame] | 757 | char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 758 | if (pname) |
| 759 | printf(FMT("%-5s", "%s "), pname); |
| 760 | else |
| 761 | printf(FMT("%-5hu", "%hu "), fw->ip.proto); |
| 762 | } |
| 763 | |
| 764 | if (format & FMT_OPTIONS) { |
| 765 | if (format & FMT_NOTABLE) |
| 766 | fputs("opt ", stdout); |
| 767 | fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout); |
| 768 | fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout); |
| 769 | fputc(' ', stdout); |
| 770 | } |
| 771 | |
| 772 | if (format & FMT_VIA) { |
| 773 | char iface[IFNAMSIZ+2]; |
| 774 | |
| 775 | if (fw->ip.invflags & IPT_INV_VIA_IN) { |
| 776 | iface[0] = '!'; |
| 777 | iface[1] = '\0'; |
| 778 | } |
| 779 | else iface[0] = '\0'; |
| 780 | |
| 781 | if (fw->ip.iniface[0] != '\0') { |
| 782 | strcat(iface, fw->ip.iniface); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 783 | } |
| 784 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 785 | else strcat(iface, "any"); |
| 786 | printf(FMT(" %-6s ","in %s "), iface); |
| 787 | |
| 788 | if (fw->ip.invflags & IPT_INV_VIA_OUT) { |
| 789 | iface[0] = '!'; |
| 790 | iface[1] = '\0'; |
| 791 | } |
| 792 | else iface[0] = '\0'; |
| 793 | |
| 794 | if (fw->ip.outiface[0] != '\0') { |
| 795 | strcat(iface, fw->ip.outiface); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 796 | } |
| 797 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 798 | else strcat(iface, "any"); |
| 799 | printf(FMT("%-6s ","out %s "), iface); |
| 800 | } |
| 801 | |
| 802 | fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout); |
| 803 | if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC)) |
| 804 | printf(FMT("%-19s ","%s "), "anywhere"); |
| 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.src)); |
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.src)); |
| 810 | strcat(buf, ipmask_to_numeric(&fw->ip.smsk)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 811 | printf(FMT("%-19s ","%s "), buf); |
| 812 | } |
| 813 | |
| 814 | fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout); |
| 815 | if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC)) |
Harald Welte | 25fc1d7 | 2003-05-31 21:30:33 +0000 | [diff] [blame] | 816 | printf(FMT("%-19s ","-> %s"), "anywhere"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 817 | else { |
| 818 | if (format & FMT_NUMERIC) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 819 | sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.dst)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 820 | else |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 821 | sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.dst)); |
| 822 | strcat(buf, ipmask_to_numeric(&fw->ip.dmsk)); |
Harald Welte | 25fc1d7 | 2003-05-31 21:30:33 +0000 | [diff] [blame] | 823 | printf(FMT("%-19s ","-> %s"), buf); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | if (format & FMT_NOTABLE) |
| 827 | fputs(" ", stdout); |
| 828 | |
Harald Welte | 72bd87e | 2005-11-24 17:04:05 +0000 | [diff] [blame] | 829 | #ifdef IPT_F_GOTO |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 830 | if(fw->ip.flags & IPT_F_GOTO) |
| 831 | printf("[goto] "); |
Harald Welte | 72bd87e | 2005-11-24 17:04:05 +0000 | [diff] [blame] | 832 | #endif |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 833 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 834 | IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC); |
| 835 | |
| 836 | if (target) { |
| 837 | if (target->print) |
| 838 | /* Print the target information. */ |
| 839 | target->print(&fw->ip, t, format & FMT_NUMERIC); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 840 | } else if (t->u.target_size != sizeof(*t)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 841 | printf("[%u bytes of unknown target data] ", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 842 | (unsigned int)(t->u.target_size - sizeof(*t))); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 843 | |
| 844 | if (!(format & FMT_NONEWLINE)) |
| 845 | fputc('\n', stdout); |
| 846 | } |
| 847 | |
| 848 | static void |
| 849 | print_firewall_line(const struct ipt_entry *fw, |
| 850 | const iptc_handle_t h) |
| 851 | { |
| 852 | struct ipt_entry_target *t; |
| 853 | |
| 854 | t = ipt_get_target((struct ipt_entry *)fw); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 855 | print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static int |
| 859 | append_entry(const ipt_chainlabel chain, |
| 860 | struct ipt_entry *fw, |
| 861 | unsigned int nsaddrs, |
| 862 | const struct in_addr saddrs[], |
| 863 | unsigned int ndaddrs, |
| 864 | const struct in_addr daddrs[], |
| 865 | int verbose, |
| 866 | iptc_handle_t *handle) |
| 867 | { |
| 868 | unsigned int i, j; |
| 869 | int ret = 1; |
| 870 | |
| 871 | for (i = 0; i < nsaddrs; i++) { |
| 872 | fw->ip.src.s_addr = saddrs[i].s_addr; |
| 873 | for (j = 0; j < ndaddrs; j++) { |
| 874 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
| 875 | if (verbose) |
| 876 | print_firewall_line(fw, *handle); |
| 877 | ret &= iptc_append_entry(chain, fw, handle); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | static int |
| 885 | replace_entry(const ipt_chainlabel chain, |
| 886 | struct ipt_entry *fw, |
| 887 | unsigned int rulenum, |
| 888 | const struct in_addr *saddr, |
| 889 | const struct in_addr *daddr, |
| 890 | int verbose, |
| 891 | iptc_handle_t *handle) |
| 892 | { |
| 893 | fw->ip.src.s_addr = saddr->s_addr; |
| 894 | fw->ip.dst.s_addr = daddr->s_addr; |
| 895 | |
| 896 | if (verbose) |
| 897 | print_firewall_line(fw, *handle); |
| 898 | return iptc_replace_entry(chain, fw, rulenum, handle); |
| 899 | } |
| 900 | |
| 901 | static int |
| 902 | insert_entry(const ipt_chainlabel chain, |
| 903 | struct ipt_entry *fw, |
| 904 | unsigned int rulenum, |
| 905 | unsigned int nsaddrs, |
| 906 | const struct in_addr saddrs[], |
| 907 | unsigned int ndaddrs, |
| 908 | const struct in_addr daddrs[], |
| 909 | int verbose, |
| 910 | iptc_handle_t *handle) |
| 911 | { |
| 912 | unsigned int i, j; |
| 913 | int ret = 1; |
| 914 | |
| 915 | for (i = 0; i < nsaddrs; i++) { |
| 916 | fw->ip.src.s_addr = saddrs[i].s_addr; |
| 917 | for (j = 0; j < ndaddrs; j++) { |
| 918 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
| 919 | if (verbose) |
| 920 | print_firewall_line(fw, *handle); |
| 921 | ret &= iptc_insert_entry(chain, fw, rulenum, handle); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 928 | static unsigned char * |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 929 | make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches) |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 930 | { |
| 931 | /* Establish mask for comparison */ |
| 932 | unsigned int size; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 933 | struct iptables_rule_match *matchp; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 934 | unsigned char *mask, *mptr; |
| 935 | |
| 936 | size = sizeof(struct ipt_entry); |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 937 | for (matchp = matches; matchp; matchp = matchp->next) |
| 938 | size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 939 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 940 | mask = fw_calloc(1, size |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 941 | + IPT_ALIGN(sizeof(struct ipt_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 942 | + xtables_targets->size); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 943 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 944 | memset(mask, 0xFF, sizeof(struct ipt_entry)); |
| 945 | mptr = mask + sizeof(struct ipt_entry); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 946 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 947 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 948 | memset(mptr, 0xFF, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 949 | IPT_ALIGN(sizeof(struct ipt_entry_match)) |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 950 | + matchp->match->userspacesize); |
| 951 | mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Rusty Russell | a4d3e1f | 2001-01-07 06:56:02 +0000 | [diff] [blame] | 954 | memset(mptr, 0xFF, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 955 | IPT_ALIGN(sizeof(struct ipt_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 956 | + xtables_targets->userspacesize); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 957 | |
| 958 | return mask; |
| 959 | } |
| 960 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 961 | static int |
| 962 | delete_entry(const ipt_chainlabel chain, |
| 963 | struct ipt_entry *fw, |
| 964 | unsigned int nsaddrs, |
| 965 | const struct in_addr saddrs[], |
| 966 | unsigned int ndaddrs, |
| 967 | const struct in_addr daddrs[], |
| 968 | int verbose, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 969 | iptc_handle_t *handle, |
| 970 | struct iptables_rule_match *matches) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 971 | { |
| 972 | unsigned int i, j; |
| 973 | int ret = 1; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 974 | unsigned char *mask; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 975 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 976 | mask = make_delete_mask(fw, matches); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 977 | for (i = 0; i < nsaddrs; i++) { |
| 978 | fw->ip.src.s_addr = saddrs[i].s_addr; |
| 979 | for (j = 0; j < ndaddrs; j++) { |
| 980 | fw->ip.dst.s_addr = daddrs[j].s_addr; |
| 981 | if (verbose) |
| 982 | print_firewall_line(fw, *handle); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 983 | ret &= iptc_delete_entry(chain, fw, mask, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 986 | free(mask); |
| 987 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 988 | return ret; |
| 989 | } |
| 990 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 991 | int |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 992 | for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *), |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 993 | int verbose, int builtinstoo, iptc_handle_t *handle) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 994 | { |
| 995 | int ret = 1; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 996 | const char *chain; |
| 997 | char *chains; |
| 998 | unsigned int i, chaincount = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 999 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1000 | chain = iptc_first_chain(handle); |
| 1001 | while (chain) { |
| 1002 | chaincount++; |
| 1003 | chain = iptc_next_chain(handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1006 | chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount); |
| 1007 | i = 0; |
| 1008 | chain = iptc_first_chain(handle); |
| 1009 | while (chain) { |
| 1010 | strcpy(chains + i*sizeof(ipt_chainlabel), chain); |
| 1011 | i++; |
| 1012 | chain = iptc_next_chain(handle); |
| 1013 | } |
| 1014 | |
| 1015 | for (i = 0; i < chaincount; i++) { |
| 1016 | if (!builtinstoo |
| 1017 | && iptc_builtin(chains + i*sizeof(ipt_chainlabel), |
Harald Welte | 3a506ac | 2004-08-30 16:00:09 +0000 | [diff] [blame] | 1018 | *handle) == 1) |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1019 | continue; |
| 1020 | ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle); |
| 1021 | } |
| 1022 | |
| 1023 | free(chains); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1024 | return ret; |
| 1025 | } |
| 1026 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1027 | int |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1028 | flush_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(flush_entries, verbose, 1, handle); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (verbose) |
| 1035 | fprintf(stdout, "Flushing chain `%s'\n", chain); |
| 1036 | return iptc_flush_entries(chain, handle); |
| 1037 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1038 | |
| 1039 | static int |
| 1040 | zero_entries(const ipt_chainlabel chain, int verbose, |
| 1041 | iptc_handle_t *handle) |
| 1042 | { |
| 1043 | if (!chain) |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1044 | return for_each_chain(zero_entries, verbose, 1, handle); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1045 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1046 | if (verbose) |
| 1047 | fprintf(stdout, "Zeroing chain `%s'\n", chain); |
| 1048 | return iptc_zero_entries(chain, handle); |
| 1049 | } |
| 1050 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1051 | int |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1052 | delete_chain(const ipt_chainlabel chain, int verbose, |
| 1053 | iptc_handle_t *handle) |
| 1054 | { |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1055 | if (!chain) |
| 1056 | return for_each_chain(delete_chain, verbose, 0, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (verbose) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1059 | fprintf(stdout, "Deleting chain `%s'\n", chain); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1060 | return iptc_delete_chain(chain, handle); |
| 1061 | } |
| 1062 | |
| 1063 | static int |
| 1064 | list_entries(const ipt_chainlabel chain, int verbose, int numeric, |
| 1065 | int expanded, int linenumbers, iptc_handle_t *handle) |
| 1066 | { |
| 1067 | int found = 0; |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1068 | unsigned int format; |
| 1069 | const char *this; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1070 | |
| 1071 | format = FMT_OPTIONS; |
| 1072 | if (!verbose) |
| 1073 | format |= FMT_NOCOUNTS; |
| 1074 | else |
| 1075 | format |= FMT_VIA; |
| 1076 | |
| 1077 | if (numeric) |
| 1078 | format |= FMT_NUMERIC; |
| 1079 | |
| 1080 | if (!expanded) |
| 1081 | format |= FMT_KILOMEGAGIGA; |
| 1082 | |
| 1083 | if (linenumbers) |
| 1084 | format |= FMT_LINENUMBERS; |
| 1085 | |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1086 | for (this = iptc_first_chain(handle); |
| 1087 | this; |
| 1088 | this = iptc_next_chain(handle)) { |
| 1089 | const struct ipt_entry *i; |
| 1090 | unsigned int num; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1091 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1092 | if (chain && strcmp(chain, this) != 0) |
| 1093 | continue; |
| 1094 | |
| 1095 | if (found) printf("\n"); |
| 1096 | |
| 1097 | print_header(format, this, handle); |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1098 | i = iptc_first_rule(this, handle); |
| 1099 | |
| 1100 | num = 0; |
| 1101 | while (i) { |
| 1102 | print_firewall(i, |
| 1103 | iptc_get_target(i, handle), |
| 1104 | num++, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1105 | format, |
| 1106 | *handle); |
Rusty Russell | 9e1d214 | 2000-04-23 09:11:12 +0000 | [diff] [blame] | 1107 | i = iptc_next_rule(i, handle); |
| 1108 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1109 | found = 1; |
| 1110 | } |
| 1111 | |
| 1112 | errno = ENOENT; |
| 1113 | return found; |
| 1114 | } |
| 1115 | |
| 1116 | static struct ipt_entry * |
| 1117 | generate_entry(const struct ipt_entry *fw, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1118 | struct iptables_rule_match *matches, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1119 | struct ipt_entry_target *target) |
| 1120 | { |
| 1121 | unsigned int size; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1122 | struct iptables_rule_match *matchp; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1123 | struct ipt_entry *e; |
| 1124 | |
| 1125 | size = sizeof(struct ipt_entry); |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1126 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1127 | size += matchp->match->m->u.match_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1128 | |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1129 | e = fw_malloc(size + target->u.target_size); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1130 | *e = *fw; |
| 1131 | e->target_offset = size; |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1132 | e->next_offset = size + target->u.target_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1133 | |
| 1134 | size = 0; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1135 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 1136 | memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size); |
| 1137 | size += matchp->match->m->u.match_size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1138 | } |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1139 | memcpy(e->elems + size, target, target->u.target_size); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1140 | |
| 1141 | return e; |
| 1142 | } |
| 1143 | |
Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1144 | static void clear_rule_matches(struct iptables_rule_match **matches) |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1145 | { |
| 1146 | struct iptables_rule_match *matchp, *tmp; |
| 1147 | |
| 1148 | for (matchp = *matches; matchp;) { |
| 1149 | tmp = matchp->next; |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1150 | if (matchp->match->m) { |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1151 | free(matchp->match->m); |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1152 | matchp->match->m = NULL; |
| 1153 | } |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1154 | if (matchp->match == matchp->match->next) { |
| 1155 | free(matchp->match); |
| 1156 | matchp->match = NULL; |
| 1157 | } |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1158 | free(matchp); |
| 1159 | matchp = tmp; |
| 1160 | } |
| 1161 | |
| 1162 | *matches = NULL; |
| 1163 | } |
| 1164 | |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1165 | static void set_revision(char *name, u_int8_t revision) |
| 1166 | { |
| 1167 | /* Old kernel sources don't have ".revision" field, |
| 1168 | but we stole a byte from name. */ |
| 1169 | name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0'; |
| 1170 | name[IPT_FUNCTION_MAXNAMELEN - 1] = revision; |
| 1171 | } |
| 1172 | |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 1173 | void |
| 1174 | get_kernel_version(void) { |
| 1175 | static struct utsname uts; |
| 1176 | int x = 0, y = 0, z = 0; |
| 1177 | |
| 1178 | if (uname(&uts) == -1) { |
| 1179 | fprintf(stderr, "Unable to retrieve kernel version.\n"); |
| 1180 | free_opts(1); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1181 | exit(1); |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | sscanf(uts.release, "%d.%d.%d", &x, &y, &z); |
| 1185 | kernel_version = LINUX_VERSION(x, y, z); |
| 1186 | } |
| 1187 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1188 | int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle) |
| 1189 | { |
| 1190 | struct ipt_entry fw, *e = NULL; |
| 1191 | int invert = 0; |
| 1192 | unsigned int nsaddrs = 0, ndaddrs = 0; |
| 1193 | struct in_addr *saddrs = NULL, *daddrs = NULL; |
| 1194 | |
| 1195 | int c, verbose = 0; |
| 1196 | const char *chain = NULL; |
| 1197 | const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL; |
| 1198 | const char *policy = NULL, *newname = NULL; |
| 1199 | unsigned int rulenum = 0, options = 0, command = 0; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1200 | const char *pcnt = NULL, *bcnt = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1201 | int ret = 1; |
| 1202 | struct iptables_match *m; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1203 | struct iptables_rule_match *matches = NULL; |
| 1204 | struct iptables_rule_match *matchp; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1205 | struct iptables_target *target = NULL; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1206 | struct iptables_target *t; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1207 | const char *jumpto = ""; |
| 1208 | char *protocol = NULL; |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1209 | int proto_used = 0; |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1210 | unsigned long long cnt; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1211 | |
| 1212 | memset(&fw, 0, sizeof(fw)); |
| 1213 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1214 | /* re-set optind to 0 in case do_command gets called |
| 1215 | * a second time */ |
| 1216 | optind = 0; |
| 1217 | |
| 1218 | /* clear mflags in case do_command gets called a second time |
| 1219 | * (we clear the global list of all matches for security)*/ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1220 | for (m = xtables_matches; m; m = m->next) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1221 | m->mflags = 0; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1222 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1223 | for (t = xtables_targets; t; t = t->next) { |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1224 | t->tflags = 0; |
| 1225 | t->used = 0; |
| 1226 | } |
| 1227 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1228 | /* Suppress error messages: we may add new options if we |
| 1229 | demand-load a protocol. */ |
| 1230 | opterr = 0; |
| 1231 | |
| 1232 | while ((c = getopt_long(argc, argv, |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1233 | "-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] | 1234 | opts, NULL)) != -1) { |
| 1235 | switch (c) { |
| 1236 | /* |
| 1237 | * Command selection |
| 1238 | */ |
| 1239 | case 'A': |
| 1240 | add_command(&command, CMD_APPEND, CMD_NONE, |
| 1241 | invert); |
| 1242 | chain = optarg; |
| 1243 | break; |
| 1244 | |
| 1245 | case 'D': |
| 1246 | add_command(&command, CMD_DELETE, CMD_NONE, |
| 1247 | invert); |
| 1248 | chain = optarg; |
| 1249 | if (optind < argc && argv[optind][0] != '-' |
| 1250 | && argv[optind][0] != '!') { |
| 1251 | rulenum = parse_rulenumber(argv[optind++]); |
| 1252 | command = CMD_DELETE_NUM; |
| 1253 | } |
| 1254 | break; |
| 1255 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1256 | case 'R': |
| 1257 | add_command(&command, CMD_REPLACE, CMD_NONE, |
| 1258 | invert); |
| 1259 | chain = optarg; |
| 1260 | if (optind < argc && argv[optind][0] != '-' |
| 1261 | && argv[optind][0] != '!') |
| 1262 | rulenum = parse_rulenumber(argv[optind++]); |
| 1263 | else |
| 1264 | exit_error(PARAMETER_PROBLEM, |
| 1265 | "-%c requires a rule number", |
| 1266 | cmd2char(CMD_REPLACE)); |
| 1267 | break; |
| 1268 | |
| 1269 | case 'I': |
| 1270 | add_command(&command, CMD_INSERT, CMD_NONE, |
| 1271 | invert); |
| 1272 | chain = optarg; |
| 1273 | if (optind < argc && argv[optind][0] != '-' |
| 1274 | && argv[optind][0] != '!') |
| 1275 | rulenum = parse_rulenumber(argv[optind++]); |
| 1276 | else rulenum = 1; |
| 1277 | break; |
| 1278 | |
| 1279 | case 'L': |
| 1280 | add_command(&command, CMD_LIST, CMD_ZERO, |
| 1281 | invert); |
| 1282 | if (optarg) chain = optarg; |
| 1283 | else if (optind < argc && argv[optind][0] != '-' |
| 1284 | && argv[optind][0] != '!') |
| 1285 | chain = argv[optind++]; |
| 1286 | break; |
| 1287 | |
| 1288 | case 'F': |
| 1289 | add_command(&command, CMD_FLUSH, CMD_NONE, |
| 1290 | invert); |
| 1291 | if (optarg) chain = optarg; |
| 1292 | else if (optind < argc && argv[optind][0] != '-' |
| 1293 | && argv[optind][0] != '!') |
| 1294 | chain = argv[optind++]; |
| 1295 | break; |
| 1296 | |
| 1297 | case 'Z': |
| 1298 | add_command(&command, CMD_ZERO, CMD_LIST, |
| 1299 | invert); |
| 1300 | if (optarg) chain = optarg; |
| 1301 | else if (optind < argc && argv[optind][0] != '-' |
| 1302 | && argv[optind][0] != '!') |
| 1303 | chain = argv[optind++]; |
| 1304 | break; |
| 1305 | |
| 1306 | case 'N': |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1307 | if (optarg && (*optarg == '-' || *optarg == '!')) |
Harald Welte | 6336bfd | 2002-05-07 14:41:43 +0000 | [diff] [blame] | 1308 | exit_error(PARAMETER_PROBLEM, |
| 1309 | "chain name not allowed to start " |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1310 | "with `%c'\n", *optarg); |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1311 | if (find_target(optarg, TRY_LOAD)) |
| 1312 | exit_error(PARAMETER_PROBLEM, |
| 1313 | "chain name may not clash " |
| 1314 | "with target name\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1315 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
| 1316 | invert); |
| 1317 | chain = optarg; |
| 1318 | break; |
| 1319 | |
| 1320 | case 'X': |
| 1321 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
| 1322 | invert); |
| 1323 | if (optarg) chain = optarg; |
| 1324 | else if (optind < argc && argv[optind][0] != '-' |
| 1325 | && argv[optind][0] != '!') |
| 1326 | chain = argv[optind++]; |
| 1327 | break; |
| 1328 | |
| 1329 | case 'E': |
| 1330 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
| 1331 | invert); |
| 1332 | chain = optarg; |
| 1333 | if (optind < argc && argv[optind][0] != '-' |
| 1334 | && argv[optind][0] != '!') |
| 1335 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1336 | else |
| 1337 | exit_error(PARAMETER_PROBLEM, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1338 | "-%c requires old-chain-name and " |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1339 | "new-chain-name", |
| 1340 | cmd2char(CMD_RENAME_CHAIN)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | |
| 1343 | case 'P': |
| 1344 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
| 1345 | invert); |
| 1346 | chain = optarg; |
| 1347 | if (optind < argc && argv[optind][0] != '-' |
| 1348 | && argv[optind][0] != '!') |
| 1349 | policy = argv[optind++]; |
| 1350 | else |
| 1351 | exit_error(PARAMETER_PROBLEM, |
| 1352 | "-%c requires a chain and a policy", |
| 1353 | cmd2char(CMD_SET_POLICY)); |
| 1354 | break; |
| 1355 | |
| 1356 | case 'h': |
| 1357 | if (!optarg) |
| 1358 | optarg = argv[optind]; |
| 1359 | |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1360 | /* iptables -p icmp -h */ |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1361 | if (!matches && protocol) |
| 1362 | find_match(protocol, TRY_LOAD, &matches); |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1363 | |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1364 | exit_printhelp(matches); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * Option selection |
| 1368 | */ |
| 1369 | case 'p': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1370 | check_inverse(optarg, &invert, &optind, argc); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1371 | set_option(&options, OPT_PROTOCOL, &fw.ip.invflags, |
| 1372 | invert); |
| 1373 | |
| 1374 | /* Canonicalize into lower case */ |
| 1375 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 1376 | *protocol = tolower(*protocol); |
| 1377 | |
| 1378 | protocol = argv[optind-1]; |
| 1379 | fw.ip.proto = parse_protocol(protocol); |
| 1380 | |
| 1381 | if (fw.ip.proto == 0 |
| 1382 | && (fw.ip.invflags & IPT_INV_PROTO)) |
| 1383 | exit_error(PARAMETER_PROBLEM, |
| 1384 | "rule would never match protocol"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | |
| 1387 | case 's': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1388 | check_inverse(optarg, &invert, &optind, argc); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1389 | set_option(&options, OPT_SOURCE, &fw.ip.invflags, |
| 1390 | invert); |
| 1391 | shostnetworkmask = argv[optind-1]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | |
| 1394 | case 'd': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1395 | check_inverse(optarg, &invert, &optind, argc); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1396 | set_option(&options, OPT_DESTINATION, &fw.ip.invflags, |
| 1397 | invert); |
| 1398 | dhostnetworkmask = argv[optind-1]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1401 | #ifdef IPT_F_GOTO |
| 1402 | case 'g': |
| 1403 | set_option(&options, OPT_JUMP, &fw.ip.invflags, |
| 1404 | invert); |
| 1405 | fw.ip.flags |= IPT_F_GOTO; |
| 1406 | jumpto = parse_target(optarg); |
| 1407 | break; |
| 1408 | #endif |
| 1409 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1410 | case 'j': |
| 1411 | set_option(&options, OPT_JUMP, &fw.ip.invflags, |
| 1412 | invert); |
| 1413 | jumpto = parse_target(optarg); |
Rusty Russell | 859f726 | 2000-08-24 06:00:33 +0000 | [diff] [blame] | 1414 | /* TRY_LOAD (may be chain name) */ |
| 1415 | target = find_target(jumpto, TRY_LOAD); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (target) { |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1418 | size_t size; |
| 1419 | |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 1420 | size = IPT_ALIGN(sizeof(struct ipt_entry_target)) |
| 1421 | + target->size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1422 | |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1423 | target->t = fw_calloc(1, size); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1424 | target->t->u.target_size = size; |
| 1425 | strcpy(target->t->u.user.name, jumpto); |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1426 | set_revision(target->t->u.user.name, |
| 1427 | target->revision); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 1428 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1429 | target->init(target->t); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1430 | opts = merge_options(opts, |
| 1431 | target->extra_opts, |
Pablo Neira Ayuso | 8e707d7 | 2008-01-17 17:30:27 +0000 | [diff] [blame] | 1432 | &target->option_offset); |
| 1433 | if (opts == NULL) |
| 1434 | exit_error(OTHER_PROBLEM, |
| 1435 | "can't alloc memory!"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1436 | } |
| 1437 | break; |
| 1438 | |
| 1439 | |
| 1440 | case 'i': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1441 | check_inverse(optarg, &invert, &optind, argc); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1442 | set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags, |
| 1443 | invert); |
| 1444 | parse_interface(argv[optind-1], |
| 1445 | fw.ip.iniface, |
| 1446 | fw.ip.iniface_mask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1447 | break; |
| 1448 | |
| 1449 | case 'o': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1450 | check_inverse(optarg, &invert, &optind, argc); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1451 | set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags, |
| 1452 | invert); |
| 1453 | parse_interface(argv[optind-1], |
| 1454 | fw.ip.outiface, |
| 1455 | fw.ip.outiface_mask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1456 | break; |
| 1457 | |
| 1458 | case 'f': |
| 1459 | set_option(&options, OPT_FRAGMENT, &fw.ip.invflags, |
| 1460 | invert); |
| 1461 | fw.ip.flags |= IPT_F_FRAG; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | |
| 1464 | case 'v': |
| 1465 | if (!verbose) |
| 1466 | set_option(&options, OPT_VERBOSE, |
| 1467 | &fw.ip.invflags, invert); |
| 1468 | verbose++; |
| 1469 | break; |
| 1470 | |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 1471 | case 'm': { |
| 1472 | size_t size; |
| 1473 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1474 | if (invert) |
| 1475 | exit_error(PARAMETER_PROBLEM, |
| 1476 | "unexpected ! flag before --match"); |
| 1477 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1478 | m = find_match(optarg, LOAD_MUST_SUCCEED, &matches); |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 1479 | size = IPT_ALIGN(sizeof(struct ipt_entry_match)) |
| 1480 | + m->size; |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 1481 | m->m = fw_calloc(1, size); |
| 1482 | m->m->u.match_size = size; |
Rusty Russell | 27ff347 | 2000-05-12 14:04:50 +0000 | [diff] [blame] | 1483 | strcpy(m->m->u.user.name, m->name); |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1484 | set_revision(m->m->u.user.name, m->revision); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 1485 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1486 | m->init(m->m); |
Pablo Neira Ayuso | 8e707d7 | 2008-01-17 17:30:27 +0000 | [diff] [blame] | 1487 | if (m != m->next) { |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1488 | /* Merge options for non-cloned matches */ |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1489 | opts = merge_options(opts, |
| 1490 | m->extra_opts, |
Pablo Neira Ayuso | 8e707d7 | 2008-01-17 17:30:27 +0000 | [diff] [blame] | 1491 | &m->option_offset); |
| 1492 | if (opts == NULL) |
| 1493 | exit_error(OTHER_PROBLEM, |
| 1494 | "can't alloc memory!"); |
| 1495 | } |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 1496 | } |
| 1497 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1498 | |
| 1499 | case 'n': |
| 1500 | set_option(&options, OPT_NUMERIC, &fw.ip.invflags, |
| 1501 | invert); |
| 1502 | break; |
| 1503 | |
| 1504 | case 't': |
| 1505 | if (invert) |
| 1506 | exit_error(PARAMETER_PROBLEM, |
| 1507 | "unexpected ! flag before --table"); |
| 1508 | *table = argv[optind-1]; |
| 1509 | break; |
| 1510 | |
| 1511 | case 'x': |
| 1512 | set_option(&options, OPT_EXPANDED, &fw.ip.invflags, |
| 1513 | invert); |
| 1514 | break; |
| 1515 | |
| 1516 | case 'V': |
| 1517 | if (invert) |
| 1518 | printf("Not %s ;-)\n", program_version); |
| 1519 | else |
| 1520 | printf("%s v%s\n", |
| 1521 | program_name, program_version); |
| 1522 | exit(0); |
| 1523 | |
| 1524 | case '0': |
| 1525 | set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags, |
| 1526 | invert); |
| 1527 | break; |
| 1528 | |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1529 | case 'M': |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1530 | modprobe_program = optarg; |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1531 | break; |
| 1532 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1533 | case 'c': |
| 1534 | |
| 1535 | set_option(&options, OPT_COUNTERS, &fw.ip.invflags, |
| 1536 | invert); |
| 1537 | pcnt = optarg; |
| 1538 | if (optind < argc && argv[optind][0] != '-' |
| 1539 | && argv[optind][0] != '!') |
| 1540 | bcnt = argv[optind++]; |
| 1541 | else |
| 1542 | exit_error(PARAMETER_PROBLEM, |
| 1543 | "-%c requires packet and byte counter", |
| 1544 | opt2char(OPT_COUNTERS)); |
| 1545 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1546 | if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1547 | exit_error(PARAMETER_PROBLEM, |
| 1548 | "-%c packet counter not numeric", |
| 1549 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1550 | fw.counters.pcnt = cnt; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1551 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1552 | if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1553 | exit_error(PARAMETER_PROBLEM, |
| 1554 | "-%c byte counter not numeric", |
| 1555 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1556 | fw.counters.bcnt = cnt; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | |
| 1559 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1560 | case 1: /* non option */ |
| 1561 | if (optarg[0] == '!' && optarg[1] == '\0') { |
| 1562 | if (invert) |
| 1563 | exit_error(PARAMETER_PROBLEM, |
| 1564 | "multiple consecutive ! not" |
| 1565 | " allowed"); |
| 1566 | invert = TRUE; |
| 1567 | optarg[0] = '\0'; |
| 1568 | continue; |
| 1569 | } |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1570 | fprintf(stderr, "Bad argument `%s'\n", optarg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1571 | exit_tryhelp(2); |
| 1572 | |
| 1573 | default: |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1574 | if (!target |
| 1575 | || !(target->parse(c - target->option_offset, |
| 1576 | argv, invert, |
| 1577 | &target->tflags, |
| 1578 | &fw, &target->t))) { |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1579 | for (matchp = matches; matchp; matchp = matchp->next) { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1580 | if (matchp->completed) |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1581 | continue; |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1582 | if (matchp->match->parse(c - matchp->match->option_offset, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1583 | argv, invert, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1584 | &matchp->match->mflags, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1585 | &fw, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1586 | &matchp->match->m)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1589 | m = matchp ? matchp->match : NULL; |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* If you listen carefully, you can |
| 1592 | actually hear this code suck. */ |
| 1593 | |
| 1594 | /* some explanations (after four different bugs |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1595 | * in 3 different releases): If we encounter a |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1596 | * parameter, that has not been parsed yet, |
| 1597 | * it's not an option of an explicitly loaded |
| 1598 | * match or a target. However, we support |
| 1599 | * implicit loading of the protocol match |
| 1600 | * extension. '-p tcp' means 'l4 proto 6' and |
| 1601 | * at the same time 'load tcp protocol match on |
| 1602 | * demand if we specify --dport'. |
| 1603 | * |
| 1604 | * To make this work, we need to make sure: |
| 1605 | * - the parameter has not been parsed by |
| 1606 | * a match (m above) |
| 1607 | * - a protocol has been specified |
| 1608 | * - the protocol extension has not been |
| 1609 | * loaded yet, or is loaded and unused |
| 1610 | * [think of iptables-restore!] |
| 1611 | * - the protocol extension can be successively |
| 1612 | * loaded |
| 1613 | */ |
| 1614 | if (m == NULL |
| 1615 | && protocol |
| 1616 | && (!find_proto(protocol, DONT_LOAD, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1617 | options&OPT_NUMERIC, NULL) |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1618 | || (find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1619 | options&OPT_NUMERIC, NULL) |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1620 | && (proto_used == 0)) |
| 1621 | ) |
| 1622 | && (m = find_proto(protocol, TRY_LOAD, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1623 | options&OPT_NUMERIC, &matches))) { |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1624 | /* Try loading protocol */ |
| 1625 | size_t size; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1626 | |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1627 | proto_used = 1; |
| 1628 | |
| 1629 | size = IPT_ALIGN(sizeof(struct ipt_entry_match)) |
| 1630 | + m->size; |
| 1631 | |
| 1632 | m->m = fw_calloc(1, size); |
| 1633 | m->m->u.match_size = size; |
| 1634 | strcpy(m->m->u.user.name, m->name); |
Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1635 | set_revision(m->m->u.user.name, |
| 1636 | m->revision); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 1637 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1638 | m->init(m->m); |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1639 | |
| 1640 | opts = merge_options(opts, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1641 | m->extra_opts, |
Pablo Neira Ayuso | 8e707d7 | 2008-01-17 17:30:27 +0000 | [diff] [blame] | 1642 | &m->option_offset); |
| 1643 | if (opts == NULL) |
| 1644 | exit_error(OTHER_PROBLEM, |
| 1645 | "can't alloc memory!"); |
Harald Welte | 2d86b77 | 2002-08-26 12:21:44 +0000 | [diff] [blame] | 1646 | |
| 1647 | optind--; |
| 1648 | continue; |
| 1649 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1650 | if (!m) |
| 1651 | exit_error(PARAMETER_PROBLEM, |
| 1652 | "Unknown arg `%s'", |
| 1653 | argv[optind-1]); |
| 1654 | } |
| 1655 | } |
| 1656 | invert = FALSE; |
| 1657 | } |
| 1658 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1659 | for (matchp = matches; matchp; matchp = matchp->next) |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1660 | if (matchp->match->final_check != NULL) |
| 1661 | matchp->match->final_check(matchp->match->mflags); |
Sven Koch | fb1279a | 2001-02-27 12:25:12 +0000 | [diff] [blame] | 1662 | |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1663 | if (target != NULL && target->final_check != NULL) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1664 | target->final_check(target->tflags); |
| 1665 | |
| 1666 | /* Fix me: must put inverse options checking here --MN */ |
| 1667 | |
| 1668 | if (optind < argc) |
| 1669 | exit_error(PARAMETER_PROBLEM, |
| 1670 | "unknown arguments found on commandline"); |
| 1671 | if (!command) |
| 1672 | exit_error(PARAMETER_PROBLEM, "no command specified"); |
| 1673 | if (invert) |
| 1674 | exit_error(PARAMETER_PROBLEM, |
| 1675 | "nothing appropriate following !"); |
| 1676 | |
Harald Welte | 6336bfd | 2002-05-07 14:41:43 +0000 | [diff] [blame] | 1677 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1678 | if (!(options & OPT_DESTINATION)) |
| 1679 | dhostnetworkmask = "0.0.0.0/0"; |
| 1680 | if (!(options & OPT_SOURCE)) |
| 1681 | shostnetworkmask = "0.0.0.0/0"; |
| 1682 | } |
| 1683 | |
| 1684 | if (shostnetworkmask) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1685 | ipparse_hostnetworkmask(shostnetworkmask, &saddrs, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1686 | &fw.ip.smsk, &nsaddrs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1687 | |
| 1688 | if (dhostnetworkmask) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1689 | ipparse_hostnetworkmask(dhostnetworkmask, &daddrs, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1690 | &fw.ip.dmsk, &ndaddrs); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1691 | |
| 1692 | if ((nsaddrs > 1 || ndaddrs > 1) && |
| 1693 | (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP))) |
| 1694 | exit_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
| 1695 | " source or destination IP addresses"); |
| 1696 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1697 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
| 1698 | exit_error(PARAMETER_PROBLEM, "Replacement rule does not " |
| 1699 | "specify a unique address"); |
| 1700 | |
| 1701 | generic_opt_check(command, options); |
| 1702 | |
| 1703 | if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN) |
| 1704 | exit_error(PARAMETER_PROBLEM, |
| 1705 | "chain name `%s' too long (must be under %i chars)", |
| 1706 | chain, IPT_FUNCTION_MAXNAMELEN); |
| 1707 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1708 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 1709 | if (!*handle) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1710 | *handle = iptc_init(*table); |
| 1711 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1712 | /* try to insmod the module if iptc_init failed */ |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1713 | if (!*handle && load_xtables_ko(modprobe_program, 0) != -1) |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1714 | *handle = iptc_init(*table); |
Harald Welte | 82dd2ec | 2000-12-19 05:18:15 +0000 | [diff] [blame] | 1715 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1716 | if (!*handle) |
| 1717 | exit_error(VERSION_PROBLEM, |
| 1718 | "can't initialize iptables table `%s': %s", |
| 1719 | *table, iptc_strerror(errno)); |
| 1720 | |
Harald Welte | 6336bfd | 2002-05-07 14:41:43 +0000 | [diff] [blame] | 1721 | if (command == CMD_APPEND |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1722 | || command == CMD_DELETE |
| 1723 | || command == CMD_INSERT |
| 1724 | || command == CMD_REPLACE) { |
Rusty Russell | a4860fd | 2000-06-17 16:13:02 +0000 | [diff] [blame] | 1725 | if (strcmp(chain, "PREROUTING") == 0 |
| 1726 | || strcmp(chain, "INPUT") == 0) { |
| 1727 | /* -o not valid with incoming packets. */ |
| 1728 | if (options & OPT_VIANAMEOUT) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1729 | exit_error(PARAMETER_PROBLEM, |
| 1730 | "Can't use -%c with %s\n", |
| 1731 | opt2char(OPT_VIANAMEOUT), |
| 1732 | chain); |
| 1733 | } |
| 1734 | |
Rusty Russell | a4860fd | 2000-06-17 16:13:02 +0000 | [diff] [blame] | 1735 | if (strcmp(chain, "POSTROUTING") == 0 |
| 1736 | || strcmp(chain, "OUTPUT") == 0) { |
| 1737 | /* -i not valid with outgoing packets */ |
| 1738 | if (options & OPT_VIANAMEIN) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1739 | exit_error(PARAMETER_PROBLEM, |
| 1740 | "Can't use -%c with %s\n", |
| 1741 | opt2char(OPT_VIANAMEIN), |
| 1742 | chain); |
| 1743 | } |
| 1744 | |
| 1745 | if (target && iptc_is_chain(jumpto, *handle)) { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1746 | fprintf(stderr, |
| 1747 | "Warning: using chain %s, not extension\n", |
| 1748 | jumpto); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1749 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1750 | if (target->t) |
| 1751 | free(target->t); |
| 1752 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1753 | target = NULL; |
| 1754 | } |
| 1755 | |
| 1756 | /* If they didn't specify a target, or it's a chain |
| 1757 | name, use standard. */ |
| 1758 | if (!target |
| 1759 | && (strlen(jumpto) == 0 |
| 1760 | || iptc_is_chain(jumpto, *handle))) { |
| 1761 | size_t size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1762 | |
Rusty Russell | 52a5149 | 2000-05-02 16:44:29 +0000 | [diff] [blame] | 1763 | target = find_target(IPT_STANDARD_TARGET, |
| 1764 | LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1765 | |
| 1766 | size = sizeof(struct ipt_entry_target) |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1767 | + target->size; |
Rusty Russell | 2e0a321 | 2000-04-19 11:23:18 +0000 | [diff] [blame] | 1768 | target->t = fw_calloc(1, size); |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1769 | target->t->u.target_size = size; |
| 1770 | strcpy(target->t->u.user.name, jumpto); |
Pablo Neira | 0b90564 | 2005-11-17 13:04:49 +0000 | [diff] [blame] | 1771 | if (!iptc_is_chain(jumpto, *handle)) |
| 1772 | set_revision(target->t->u.user.name, |
| 1773 | target->revision); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 1774 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1775 | target->init(target->t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1778 | if (!target) { |
Harald Welte | f2a24bd | 2000-08-30 02:11:18 +0000 | [diff] [blame] | 1779 | /* it is no chain, and we can't load a plugin. |
| 1780 | * We cannot know if the plugin is corrupt, non |
Rusty Russell | a4d3e1f | 2001-01-07 06:56:02 +0000 | [diff] [blame] | 1781 | * existant OR if the user just misspelled a |
Harald Welte | f2a24bd | 2000-08-30 02:11:18 +0000 | [diff] [blame] | 1782 | * chain. */ |
Henrik Nordstrom | 17fc163 | 2005-11-05 09:26:40 +0000 | [diff] [blame] | 1783 | #ifdef IPT_F_GOTO |
| 1784 | if (fw.ip.flags & IPT_F_GOTO) |
| 1785 | exit_error(PARAMETER_PROBLEM, |
| 1786 | "goto '%s' is not a chain\n", jumpto); |
| 1787 | #endif |
Harald Welte | f2a24bd | 2000-08-30 02:11:18 +0000 | [diff] [blame] | 1788 | find_target(jumpto, LOAD_MUST_SUCCEED); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1789 | } else { |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1790 | e = generate_entry(&fw, matches, target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1791 | free(target->t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | switch (command) { |
| 1796 | case CMD_APPEND: |
| 1797 | ret = append_entry(chain, e, |
| 1798 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1799 | options&OPT_VERBOSE, |
| 1800 | handle); |
| 1801 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1802 | case CMD_DELETE: |
| 1803 | ret = delete_entry(chain, e, |
| 1804 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1805 | options&OPT_VERBOSE, |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1806 | handle, matches); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | case CMD_DELETE_NUM: |
| 1809 | ret = iptc_delete_num_entry(chain, rulenum - 1, handle); |
| 1810 | break; |
| 1811 | case CMD_REPLACE: |
| 1812 | ret = replace_entry(chain, e, rulenum - 1, |
| 1813 | saddrs, daddrs, options&OPT_VERBOSE, |
| 1814 | handle); |
| 1815 | break; |
| 1816 | case CMD_INSERT: |
| 1817 | ret = insert_entry(chain, e, rulenum - 1, |
| 1818 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1819 | options&OPT_VERBOSE, |
| 1820 | handle); |
| 1821 | break; |
| 1822 | case CMD_LIST: |
| 1823 | ret = list_entries(chain, |
| 1824 | options&OPT_VERBOSE, |
| 1825 | options&OPT_NUMERIC, |
| 1826 | options&OPT_EXPANDED, |
| 1827 | options&OPT_LINENUMBERS, |
| 1828 | handle); |
| 1829 | break; |
| 1830 | case CMD_FLUSH: |
| 1831 | ret = flush_entries(chain, options&OPT_VERBOSE, handle); |
| 1832 | break; |
| 1833 | case CMD_ZERO: |
| 1834 | ret = zero_entries(chain, options&OPT_VERBOSE, handle); |
| 1835 | break; |
| 1836 | case CMD_LIST|CMD_ZERO: |
| 1837 | ret = list_entries(chain, |
| 1838 | options&OPT_VERBOSE, |
| 1839 | options&OPT_NUMERIC, |
| 1840 | options&OPT_EXPANDED, |
| 1841 | options&OPT_LINENUMBERS, |
| 1842 | handle); |
| 1843 | if (ret) |
| 1844 | ret = zero_entries(chain, |
| 1845 | options&OPT_VERBOSE, handle); |
| 1846 | break; |
| 1847 | case CMD_NEW_CHAIN: |
| 1848 | ret = iptc_create_chain(chain, handle); |
| 1849 | break; |
| 1850 | case CMD_DELETE_CHAIN: |
| 1851 | ret = delete_chain(chain, options&OPT_VERBOSE, handle); |
| 1852 | break; |
| 1853 | case CMD_RENAME_CHAIN: |
| 1854 | ret = iptc_rename_chain(chain, newname, handle); |
| 1855 | break; |
| 1856 | case CMD_SET_POLICY: |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 1857 | ret = iptc_set_policy(chain, policy, NULL, handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1858 | break; |
| 1859 | default: |
| 1860 | /* We should never reach this... */ |
| 1861 | exit_tryhelp(2); |
| 1862 | } |
| 1863 | |
| 1864 | if (verbose > 1) |
| 1865 | dump_entries(*handle); |
| 1866 | |
Martin Josefsson | 78cafda | 2004-02-02 20:01:18 +0000 | [diff] [blame] | 1867 | clear_rule_matches(&matches); |
| 1868 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1869 | if (e != NULL) { |
| 1870 | free(e); |
| 1871 | e = NULL; |
| 1872 | } |
| 1873 | |
keso | 6997cdf | 2004-07-04 15:20:53 +0000 | [diff] [blame] | 1874 | free(saddrs); |
| 1875 | free(daddrs); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 1876 | free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1877 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1878 | return ret; |
| 1879 | } |