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