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