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