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