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