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