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