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