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