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