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