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