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