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