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