Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1 | /* Code to take an ip6tables-style command line and do it. */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au |
| 5 | * |
Harald Welte | 10a907f | 2002-08-07 09:07:41 +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 | * |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +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> |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 34 | #include <ctype.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <limits.h> |
| 37 | #include <ip6tables.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 38 | #include <xtables.h> |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 39 | #include <arpa/inet.h> |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | #include <fcntl.h> |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/socket.h> |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 44 | |
| 45 | #ifndef TRUE |
| 46 | #define TRUE 1 |
| 47 | #endif |
| 48 | #ifndef FALSE |
| 49 | #define FALSE 0 |
| 50 | #endif |
| 51 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 52 | #define FMT_NUMERIC 0x0001 |
| 53 | #define FMT_NOCOUNTS 0x0002 |
| 54 | #define FMT_KILOMEGAGIGA 0x0004 |
| 55 | #define FMT_OPTIONS 0x0008 |
| 56 | #define FMT_NOTABLE 0x0010 |
| 57 | #define FMT_NOTARGET 0x0020 |
| 58 | #define FMT_VIA 0x0040 |
| 59 | #define FMT_NONEWLINE 0x0080 |
| 60 | #define FMT_LINENUMBERS 0x0100 |
| 61 | |
| 62 | #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \ |
| 63 | | FMT_NUMERIC | FMT_NOTABLE) |
| 64 | #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab)) |
| 65 | |
| 66 | |
| 67 | #define CMD_NONE 0x0000U |
| 68 | #define CMD_INSERT 0x0001U |
| 69 | #define CMD_DELETE 0x0002U |
| 70 | #define CMD_DELETE_NUM 0x0004U |
| 71 | #define CMD_REPLACE 0x0008U |
| 72 | #define CMD_APPEND 0x0010U |
| 73 | #define CMD_LIST 0x0020U |
| 74 | #define CMD_FLUSH 0x0040U |
| 75 | #define CMD_ZERO 0x0080U |
| 76 | #define CMD_NEW_CHAIN 0x0100U |
| 77 | #define CMD_DELETE_CHAIN 0x0200U |
| 78 | #define CMD_SET_POLICY 0x0400U |
Harald Welte | 0eca33f | 2006-04-21 11:56:30 +0000 | [diff] [blame] | 79 | #define CMD_RENAME_CHAIN 0x0800U |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 80 | #define NUMBER_OF_CMD 13 |
| 81 | static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z', |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 82 | 'N', 'X', 'P', 'E' }; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 83 | |
| 84 | #define OPTION_OFFSET 256 |
| 85 | |
| 86 | #define OPT_NONE 0x00000U |
| 87 | #define OPT_NUMERIC 0x00001U |
| 88 | #define OPT_SOURCE 0x00002U |
| 89 | #define OPT_DESTINATION 0x00004U |
| 90 | #define OPT_PROTOCOL 0x00008U |
| 91 | #define OPT_JUMP 0x00010U |
| 92 | #define OPT_VERBOSE 0x00020U |
| 93 | #define OPT_EXPANDED 0x00040U |
| 94 | #define OPT_VIANAMEIN 0x00080U |
| 95 | #define OPT_VIANAMEOUT 0x00100U |
| 96 | #define OPT_LINENUMBERS 0x00200U |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 97 | #define OPT_COUNTERS 0x00400U |
| 98 | #define NUMBER_OF_OPT 11 |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 99 | static const char optflags[NUMBER_OF_OPT] |
Jonas Berlin | 4a06cf0 | 2005-04-01 06:38:25 +0000 | [diff] [blame] | 100 | = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'}; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 101 | |
| 102 | static struct option original_opts[] = { |
| 103 | { "append", 1, 0, 'A' }, |
| 104 | { "delete", 1, 0, 'D' }, |
| 105 | { "insert", 1, 0, 'I' }, |
| 106 | { "replace", 1, 0, 'R' }, |
| 107 | { "list", 2, 0, 'L' }, |
| 108 | { "flush", 2, 0, 'F' }, |
| 109 | { "zero", 2, 0, 'Z' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 110 | { "new-chain", 1, 0, 'N' }, |
| 111 | { "delete-chain", 2, 0, 'X' }, |
Harald Welte | 68ec9c7 | 2002-11-02 14:48:17 +0000 | [diff] [blame] | 112 | { "rename-chain", 1, 0, 'E' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 113 | { "policy", 1, 0, 'P' }, |
| 114 | { "source", 1, 0, 's' }, |
| 115 | { "destination", 1, 0, 'd' }, |
| 116 | { "src", 1, 0, 's' }, /* synonym */ |
| 117 | { "dst", 1, 0, 'd' }, /* synonym */ |
| 118 | { "protocol", 1, 0, 'p' }, |
| 119 | { "in-interface", 1, 0, 'i' }, |
| 120 | { "jump", 1, 0, 'j' }, |
| 121 | { "table", 1, 0, 't' }, |
| 122 | { "match", 1, 0, 'm' }, |
| 123 | { "numeric", 0, 0, 'n' }, |
| 124 | { "out-interface", 1, 0, 'o' }, |
| 125 | { "verbose", 0, 0, 'v' }, |
| 126 | { "exact", 0, 0, 'x' }, |
| 127 | { "version", 0, 0, 'V' }, |
| 128 | { "help", 2, 0, 'h' }, |
| 129 | { "line-numbers", 0, 0, '0' }, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 130 | { "modprobe", 1, 0, 'M' }, |
| 131 | { "set-counters", 1, 0, 'c' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 132 | { 0 } |
| 133 | }; |
| 134 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 135 | /* we need this for ip6tables-restore. ip6tables-restore.c sets line to the |
| 136 | * current line of the input file, in order to give a more precise error |
| 137 | * message. ip6tables itself doesn't need this, so it is initialized to the |
| 138 | * magic number of -1 */ |
| 139 | int line = -1; |
| 140 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 141 | static struct option *opts = original_opts; |
| 142 | static unsigned int global_option_offset = 0; |
| 143 | |
| 144 | /* Table of legal combinations of commands and options. If any of the |
| 145 | * given commands make an option legal, that option is legal (applies to |
| 146 | * CMD_LIST and CMD_ZERO only). |
| 147 | * Key: |
| 148 | * + compulsory |
| 149 | * x illegal |
| 150 | * optional |
| 151 | */ |
| 152 | |
| 153 | static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = |
| 154 | /* Well, it's better than "Re: Linux vs FreeBSD" */ |
| 155 | { |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 156 | /* -n -s -d -p -j -v -x -i -o --line -c */ |
| 157 | /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '}, |
| 158 | /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'}, |
| 159 | /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'}, |
| 160 | /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '}, |
| 161 | /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '}, |
| 162 | /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'}, |
| 163 | /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'}, |
| 164 | /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'}, |
| 165 | /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'}, |
| 166 | /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'}, |
| 167 | /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'}, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 168 | /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'} |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | static int inverse_for_options[NUMBER_OF_OPT] = |
| 172 | { |
| 173 | /* -n */ 0, |
| 174 | /* -s */ IP6T_INV_SRCIP, |
| 175 | /* -d */ IP6T_INV_DSTIP, |
| 176 | /* -p */ IP6T_INV_PROTO, |
| 177 | /* -j */ 0, |
| 178 | /* -v */ 0, |
| 179 | /* -x */ 0, |
| 180 | /* -i */ IP6T_INV_VIA_IN, |
| 181 | /* -o */ IP6T_INV_VIA_OUT, |
Patrick McHardyHarald Welte | 2cfbd9f | 2006-04-22 02:08:12 +0000 | [diff] [blame] | 182 | /*--line*/ 0, |
| 183 | /* -c */ 0, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | const char *program_version; |
| 187 | const char *program_name; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 188 | |
| 189 | /* Extra debugging from libiptc */ |
| 190 | extern void dump_entries6(const ip6tc_handle_t handle); |
| 191 | |
| 192 | /* A few hardcoded protocols for 'all' and in case the user has no |
| 193 | /etc/protocols */ |
| 194 | struct pprot { |
| 195 | char *name; |
| 196 | u_int8_t num; |
| 197 | }; |
| 198 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 199 | struct afinfo afinfo = { |
| 200 | .family = AF_INET6, |
| 201 | .libprefix = "libip6t_", |
| 202 | .ipproto = IPPROTO_IPV6, |
| 203 | .kmod = "ip6_tables", |
| 204 | .so_rev_match = IP6T_SO_GET_REVISION_MATCH, |
| 205 | .so_rev_target = IP6T_SO_GET_REVISION_TARGET, |
| 206 | }; |
| 207 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 208 | /* Primitive headers... */ |
| 209 | /* defined in netinet/in.h */ |
| 210 | #if 0 |
| 211 | #ifndef IPPROTO_ESP |
| 212 | #define IPPROTO_ESP 50 |
| 213 | #endif |
| 214 | #ifndef IPPROTO_AH |
| 215 | #define IPPROTO_AH 51 |
| 216 | #endif |
| 217 | #endif |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 218 | #ifndef IPPROTO_MH |
| 219 | #define IPPROTO_MH 135 |
| 220 | #endif |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 221 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 222 | static const struct pprot chain_protos[] = { |
| 223 | { "tcp", IPPROTO_TCP }, |
| 224 | { "udp", IPPROTO_UDP }, |
Patrick McHardy | 9561606 | 2007-01-11 09:08:22 +0000 | [diff] [blame] | 225 | { "udplite", IPPROTO_UDPLITE }, |
Harald Welte | de8e5fa | 2000-11-13 14:34:34 +0000 | [diff] [blame] | 226 | { "icmpv6", IPPROTO_ICMPV6 }, |
Yasuyuki KOZAKAI | 85872c8 | 2006-04-15 03:09:37 +0000 | [diff] [blame] | 227 | { "ipv6-icmp", IPPROTO_ICMPV6 }, |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 228 | { "esp", IPPROTO_ESP }, |
| 229 | { "ah", IPPROTO_AH }, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 230 | { "ipv6-mh", IPPROTO_MH }, |
| 231 | { "mh", IPPROTO_MH }, |
Phil Oester | b740891 | 2007-04-30 00:01:39 +0000 | [diff] [blame] | 232 | { "all", 0 }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static char * |
| 236 | proto_to_name(u_int8_t proto, int nolookup) |
| 237 | { |
| 238 | unsigned int i; |
| 239 | |
| 240 | if (proto && !nolookup) { |
| 241 | struct protoent *pent = getprotobynumber(proto); |
| 242 | if (pent) |
| 243 | return pent->p_name; |
| 244 | } |
| 245 | |
| 246 | for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++) |
| 247 | if (chain_protos[i].num == proto) |
| 248 | return chain_protos[i].name; |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | in6addrcpy(struct in6_addr *dst, struct in6_addr *src) |
| 255 | { |
| 256 | memcpy(dst, src, sizeof(struct in6_addr)); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 257 | /* dst->s6_addr = src->s6_addr; */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 260 | static void free_opts(int reset_offset) |
| 261 | { |
| 262 | if (opts != original_opts) { |
| 263 | free(opts); |
| 264 | opts = original_opts; |
| 265 | if (reset_offset) |
| 266 | global_option_offset = 0; |
| 267 | } |
| 268 | } |
| 269 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 270 | void |
Jan Engelhardt | d884051 | 2007-08-01 15:19:15 +0000 | [diff] [blame^] | 271 | exit_error(enum exittype status, const char *msg, ...) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 272 | { |
| 273 | va_list args; |
| 274 | |
| 275 | va_start(args, msg); |
| 276 | fprintf(stderr, "%s v%s: ", program_name, program_version); |
| 277 | vfprintf(stderr, msg, args); |
| 278 | va_end(args); |
| 279 | fprintf(stderr, "\n"); |
| 280 | if (status == PARAMETER_PROBLEM) |
| 281 | exit_tryhelp(status); |
| 282 | if (status == VERSION_PROBLEM) |
| 283 | fprintf(stderr, |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 284 | "Perhaps ip6tables or your kernel needs to be upgraded.\n"); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 285 | /* On error paths, make sure that we don't leak memory */ |
| 286 | free_opts(1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 287 | exit(status); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | exit_tryhelp(int status) |
| 292 | { |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 293 | if (line != -1) |
| 294 | fprintf(stderr, "Error occurred at line: %d\n", line); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 295 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", |
| 296 | program_name, program_name ); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 297 | free_opts(1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 298 | exit(status); |
| 299 | } |
| 300 | |
| 301 | void |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 302 | exit_printhelp(struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 303 | { |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 304 | struct ip6tables_rule_match *matchp = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 305 | struct ip6tables_target *t = NULL; |
| 306 | |
| 307 | printf("%s v%s\n\n" |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 308 | "Usage: %s -[AD] chain rule-specification [options]\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 309 | " %s -[RI] chain rulenum rule-specification [options]\n" |
| 310 | " %s -D chain rulenum [options]\n" |
| 311 | " %s -[LFZ] [chain] [options]\n" |
| 312 | " %s -[NX] chain\n" |
| 313 | " %s -E old-chain-name new-chain-name\n" |
| 314 | " %s -P chain target [options]\n" |
| 315 | " %s -h (print this help information)\n\n", |
| 316 | program_name, program_version, program_name, program_name, |
| 317 | program_name, program_name, program_name, program_name, |
| 318 | program_name, program_name); |
| 319 | |
| 320 | printf( |
| 321 | "Commands:\n" |
| 322 | "Either long or short options are allowed.\n" |
| 323 | " --append -A chain Append to chain\n" |
| 324 | " --delete -D chain Delete matching rule from chain\n" |
| 325 | " --delete -D chain rulenum\n" |
| 326 | " Delete rule rulenum (1 = first) from chain\n" |
| 327 | " --insert -I chain [rulenum]\n" |
| 328 | " Insert in chain as rulenum (default 1=first)\n" |
| 329 | " --replace -R chain rulenum\n" |
| 330 | " Replace rule rulenum (1 = first) in chain\n" |
| 331 | " --list -L [chain] List the rules in a chain or all chains\n" |
| 332 | " --flush -F [chain] Delete all rules in chain or all chains\n" |
| 333 | " --zero -Z [chain] Zero counters in chain or all chains\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 334 | " --new -N chain Create a new user-defined chain\n" |
| 335 | " --delete-chain\n" |
| 336 | " -X [chain] Delete a user-defined chain\n" |
| 337 | " --policy -P chain target\n" |
| 338 | " Change policy on chain to target\n" |
| 339 | " --rename-chain\n" |
| 340 | " -E old-chain new-chain\n" |
| 341 | " Change chain name, (moving any references)\n" |
| 342 | |
| 343 | "Options:\n" |
| 344 | " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n" |
| 345 | " --source -s [!] address[/mask]\n" |
| 346 | " source specification\n" |
| 347 | " --destination -d [!] address[/mask]\n" |
| 348 | " destination specification\n" |
| 349 | " --in-interface -i [!] input name[+]\n" |
| 350 | " network interface name ([+] for wildcard)\n" |
| 351 | " --jump -j target\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 352 | " target for rule (may load target extension)\n" |
| 353 | " --match -m match\n" |
| 354 | " extended match (may load extension)\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 355 | " --numeric -n numeric output of addresses and ports\n" |
| 356 | " --out-interface -o [!] output name[+]\n" |
| 357 | " network interface name ([+] for wildcard)\n" |
| 358 | " --table -t table table to manipulate (default: `filter')\n" |
| 359 | " --verbose -v verbose mode\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 360 | " --line-numbers print line numbers when listing\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 361 | " --exact -x expand numbers (display exact values)\n" |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 362 | /*"[!] --fragment -f match second or further fragments only\n"*/ |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 363 | " --modprobe=<command> try to insert modules using this command\n" |
| 364 | " --set-counters PKTS BYTES set the counter during insert/append\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 365 | "[!] --version -V print package version.\n"); |
| 366 | |
| 367 | /* Print out any special helps. A user might like to be able to add a --help |
| 368 | to the commandline, and see expected results. So we call help for all |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 369 | specified matches & targets */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 370 | for (t = xtables_targets; t; t = t->next) { |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 371 | if (t->used) { |
| 372 | printf("\n"); |
| 373 | t->help(); |
| 374 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 375 | } |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 376 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 377 | printf("\n"); |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 378 | matchp->match->help(); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 379 | } |
| 380 | exit(0); |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | generic_opt_check(int command, int options) |
| 385 | { |
| 386 | int i, j, legal = 0; |
| 387 | |
| 388 | /* Check that commands are valid with options. Complicated by the |
| 389 | * fact that if an option is legal with *any* command given, it is |
| 390 | * legal overall (ie. -z and -l). |
| 391 | */ |
| 392 | for (i = 0; i < NUMBER_OF_OPT; i++) { |
| 393 | legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */ |
| 394 | |
| 395 | for (j = 0; j < NUMBER_OF_CMD; j++) { |
| 396 | if (!(command & (1<<j))) |
| 397 | continue; |
| 398 | |
| 399 | if (!(options & (1<<i))) { |
| 400 | if (commands_v_options[j][i] == '+') |
| 401 | exit_error(PARAMETER_PROBLEM, |
| 402 | "You need to supply the `-%c' " |
| 403 | "option for this command\n", |
| 404 | optflags[i]); |
| 405 | } else { |
| 406 | if (commands_v_options[j][i] != 'x') |
| 407 | legal = 1; |
| 408 | else if (legal == 0) |
| 409 | legal = -1; |
| 410 | } |
| 411 | } |
| 412 | if (legal == -1) |
| 413 | exit_error(PARAMETER_PROBLEM, |
| 414 | "Illegal option `-%c' with this command\n", |
| 415 | optflags[i]); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static char |
| 420 | opt2char(int option) |
| 421 | { |
| 422 | const char *ptr; |
| 423 | for (ptr = optflags; option > 1; option >>= 1, ptr++); |
| 424 | |
| 425 | return *ptr; |
| 426 | } |
| 427 | |
| 428 | static char |
| 429 | cmd2char(int option) |
| 430 | { |
| 431 | const char *ptr; |
| 432 | for (ptr = cmdflags; option > 1; option >>= 1, ptr++); |
| 433 | |
| 434 | return *ptr; |
| 435 | } |
| 436 | |
| 437 | static void |
Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 438 | add_command(unsigned int *cmd, const int newcmd, const int othercmds, |
| 439 | int invert) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 440 | { |
| 441 | if (invert) |
| 442 | exit_error(PARAMETER_PROBLEM, "unexpected ! flag"); |
| 443 | if (*cmd & (~othercmds)) |
| 444 | exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n", |
| 445 | cmd2char(newcmd), cmd2char(*cmd & (~othercmds))); |
| 446 | *cmd |= newcmd; |
| 447 | } |
| 448 | |
| 449 | int |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 450 | check_inverse(const char option[], int *invert, int *optind, int argc) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 451 | { |
| 452 | if (option && strcmp(option, "!") == 0) { |
| 453 | if (*invert) |
| 454 | exit_error(PARAMETER_PROBLEM, |
| 455 | "Multiple `!' flags not allowed"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 456 | *invert = TRUE; |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 457 | if (optind) { |
| 458 | *optind = *optind+1; |
| 459 | if (argc && *optind > argc) |
| 460 | exit_error(PARAMETER_PROBLEM, |
| 461 | "no argument following `!'"); |
| 462 | } |
| 463 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 464 | return TRUE; |
| 465 | } |
| 466 | return FALSE; |
| 467 | } |
| 468 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 469 | static char * |
| 470 | addr_to_numeric(const struct in6_addr *addrp) |
| 471 | { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 472 | /* 0000:0000:0000:0000:0000:000.000.000.000 |
| 473 | * 0000:0000:0000:0000:0000:0000:0000:0000 */ |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 474 | static char buf[50+1]; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 475 | return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static struct in6_addr * |
| 479 | numeric_to_addr(const char *num) |
| 480 | { |
| 481 | static struct in6_addr ap; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 482 | int err; |
| 483 | if ((err=inet_pton(AF_INET6, num, &ap)) == 1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 484 | return ≈ |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 485 | #ifdef DEBUG |
| 486 | fprintf(stderr, "\nnumeric2addr: %d\n", err); |
| 487 | #endif |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 488 | return (struct in6_addr *)NULL; |
| 489 | } |
| 490 | |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 491 | |
| 492 | static struct in6_addr * |
| 493 | host_to_addr(const char *name, unsigned int *naddr) |
| 494 | { |
| 495 | struct addrinfo hints; |
| 496 | struct addrinfo *res; |
| 497 | static struct in6_addr *addr; |
| 498 | int err; |
| 499 | |
| 500 | memset(&hints, 0, sizeof(hints)); |
| 501 | hints.ai_flags=AI_CANONNAME; |
| 502 | hints.ai_family=AF_INET6; |
| 503 | hints.ai_socktype=SOCK_RAW; |
| 504 | hints.ai_protocol=41; |
| 505 | hints.ai_next=NULL; |
| 506 | |
| 507 | *naddr = 0; |
| 508 | if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){ |
| 509 | #ifdef DEBUG |
| 510 | fprintf(stderr,"Name2IP: %s\n",gai_strerror(err)); |
| 511 | #endif |
| 512 | return (struct in6_addr *) NULL; |
| 513 | } else { |
| 514 | if (res->ai_family != AF_INET6 || |
| 515 | res->ai_addrlen != sizeof(struct sockaddr_in6)) |
| 516 | return (struct in6_addr *) NULL; |
| 517 | |
| 518 | #ifdef DEBUG |
| 519 | fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen, |
| 520 | addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr))); |
| 521 | #endif |
| 522 | /* Get the first element of the address-chain */ |
| 523 | addr = fw_calloc(1, sizeof(struct in6_addr)); |
| 524 | in6addrcpy(addr, (struct in6_addr *) |
| 525 | &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); |
| 526 | freeaddrinfo(res); |
| 527 | *naddr = 1; |
| 528 | return addr; |
| 529 | } |
| 530 | |
| 531 | return (struct in6_addr *) NULL; |
| 532 | } |
| 533 | |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 534 | static char * |
| 535 | addr_to_host(const struct in6_addr *addr) |
| 536 | { |
| 537 | struct sockaddr_in6 saddr; |
| 538 | int err; |
| 539 | static char hostname[NI_MAXHOST]; |
| 540 | |
| 541 | memset(&saddr, 0, sizeof(struct sockaddr_in6)); |
| 542 | in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr); |
András Kis-Szabó | ed44b83 | 2001-06-27 02:21:45 +0000 | [diff] [blame] | 543 | saddr.sin6_family = AF_INET6; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 544 | |
| 545 | if ( (err=getnameinfo((struct sockaddr *)&saddr, |
| 546 | sizeof(struct sockaddr_in6), |
| 547 | hostname, sizeof(hostname)-1, |
| 548 | NULL, 0, 0)) != 0 ){ |
| 549 | #ifdef DEBUG |
| 550 | fprintf(stderr,"IP2Name: %s\n",gai_strerror(err)); |
| 551 | #endif |
| 552 | return (char *) NULL; |
| 553 | } else { |
| 554 | #ifdef DEBUG |
| 555 | fprintf (stderr, "\naddr2host: %s\n", hostname); |
| 556 | #endif |
| 557 | |
| 558 | return hostname; |
| 559 | } |
| 560 | |
| 561 | return (char *) NULL; |
| 562 | } |
| 563 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 564 | static char * |
| 565 | mask_to_numeric(const struct in6_addr *addrp) |
| 566 | { |
Harald Welte | 8a50ab8 | 2003-06-24 18:15:59 +0000 | [diff] [blame] | 567 | static char buf[50+2]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 568 | int l = ipv6_prefix_length(addrp); |
Harald Welte | 8a50ab8 | 2003-06-24 18:15:59 +0000 | [diff] [blame] | 569 | if (l == -1) { |
| 570 | strcpy(buf, "/"); |
| 571 | strcat(buf, addr_to_numeric(addrp)); |
| 572 | return buf; |
| 573 | } |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 574 | sprintf(buf, "/%d", l); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 575 | return buf; |
| 576 | } |
| 577 | |
| 578 | static struct in6_addr * |
| 579 | network_to_addr(const char *name) |
| 580 | { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 581 | /* abort();*/ |
| 582 | /* TODO: not implemented yet, but the exception breaks the |
| 583 | * name resolvation */ |
| 584 | return (struct in6_addr *)NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static char * |
| 588 | addr_to_anyname(const struct in6_addr *addr) |
| 589 | { |
| 590 | char *name; |
| 591 | |
| 592 | if ((name = addr_to_host(addr)) != NULL) |
| 593 | return name; |
| 594 | |
| 595 | return addr_to_numeric(addr); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * All functions starting with "parse" should succeed, otherwise |
| 600 | * the program fails. |
| 601 | * Most routines return pointers to static data that may change |
| 602 | * between calls to the same or other routines with a few exceptions: |
| 603 | * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask" |
| 604 | * return global static data. |
| 605 | */ |
| 606 | |
| 607 | static struct in6_addr * |
| 608 | parse_hostnetwork(const char *name, unsigned int *naddrs) |
| 609 | { |
| 610 | struct in6_addr *addrp, *addrptmp; |
| 611 | |
| 612 | if ((addrptmp = numeric_to_addr(name)) != NULL || |
| 613 | (addrptmp = network_to_addr(name)) != NULL) { |
| 614 | addrp = fw_malloc(sizeof(struct in6_addr)); |
| 615 | in6addrcpy(addrp, addrptmp); |
| 616 | *naddrs = 1; |
| 617 | return addrp; |
| 618 | } |
| 619 | if ((addrp = host_to_addr(name, naddrs)) != NULL) |
| 620 | return addrp; |
| 621 | |
| 622 | exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
| 623 | } |
| 624 | |
| 625 | static struct in6_addr * |
| 626 | parse_mask(char *mask) |
| 627 | { |
| 628 | static struct in6_addr maskaddr; |
| 629 | struct in6_addr *addrp; |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 630 | unsigned int bits; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 631 | |
| 632 | if (mask == NULL) { |
| 633 | /* no mask at all defaults to 128 bits */ |
| 634 | memset(&maskaddr, 0xff, sizeof maskaddr); |
| 635 | return &maskaddr; |
| 636 | } |
| 637 | if ((addrp = numeric_to_addr(mask)) != NULL) |
| 638 | return addrp; |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 639 | if (string_to_number(mask, 0, 128, &bits) == -1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 640 | exit_error(PARAMETER_PROBLEM, |
| 641 | "invalid mask `%s' specified", mask); |
| 642 | if (bits != 0) { |
| 643 | char *p = (char *)&maskaddr; |
| 644 | memset(p, 0xff, bits / 8); |
| 645 | memset(p + (bits / 8) + 1, 0, (128 - bits) / 8); |
| 646 | p[bits / 8] = 0xff << (8 - (bits & 7)); |
| 647 | return &maskaddr; |
| 648 | } |
| 649 | |
| 650 | memset(&maskaddr, 0, sizeof maskaddr); |
| 651 | return &maskaddr; |
| 652 | } |
| 653 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 654 | void |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 655 | parse_hostnetworkmask(const char *name, struct in6_addr **addrpp, |
| 656 | struct in6_addr *maskp, unsigned int *naddrs) |
| 657 | { |
| 658 | struct in6_addr *addrp; |
| 659 | char buf[256]; |
| 660 | char *p; |
| 661 | int i, j, n; |
| 662 | |
| 663 | strncpy(buf, name, sizeof(buf) - 1); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 664 | buf[sizeof(buf) - 1] = '\0'; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 665 | if ((p = strrchr(buf, '/')) != NULL) { |
| 666 | *p = '\0'; |
| 667 | addrp = parse_mask(p + 1); |
| 668 | } else |
| 669 | addrp = parse_mask(NULL); |
| 670 | in6addrcpy(maskp, addrp); |
| 671 | |
| 672 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 673 | if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any))) |
| 674 | strcpy(buf, "::"); |
| 675 | |
| 676 | addrp = *addrpp = parse_hostnetwork(buf, naddrs); |
| 677 | n = *naddrs; |
| 678 | for (i = 0, j = 0; i < n; i++) { |
| 679 | int k; |
| 680 | for (k = 0; k < 4; k++) |
| 681 | addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k]; |
| 682 | j++; |
| 683 | for (k = 0; k < j - 1; k++) { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 684 | if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 685 | (*naddrs)--; |
| 686 | j--; |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 693 | /* Christophe Burki wants `-p 6' to imply `-m tcp'. */ |
| 694 | static struct ip6tables_match * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 695 | find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 696 | { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 697 | unsigned int proto; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 698 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 699 | if (string_to_number(pname, 0, 255, &proto) != -1) { |
| 700 | char *protoname = proto_to_name(proto, nolookup); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 701 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 702 | if (protoname) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 703 | return find_match(protoname, tryload, matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 704 | } else |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 705 | return find_match(pname, tryload, matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 706 | |
| 707 | return NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 710 | u_int16_t |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 711 | parse_protocol(const char *s) |
| 712 | { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 713 | unsigned int proto; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 714 | |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 715 | if (string_to_number(s, 0, 255, &proto) == -1) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 716 | struct protoent *pent; |
| 717 | |
Harald Welte | cbe1ec7 | 2006-02-11 09:50:11 +0000 | [diff] [blame] | 718 | /* first deal with the special case of 'all' to prevent |
| 719 | * people from being able to redefine 'all' in nsswitch |
| 720 | * and/or provoke expensive [not working] ldap/nis/... |
| 721 | * lookups */ |
| 722 | if (!strcmp(s, "all")) |
| 723 | return 0; |
| 724 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 725 | if ((pent = getprotobyname(s))) |
| 726 | proto = pent->p_proto; |
| 727 | else { |
| 728 | unsigned int i; |
| 729 | for (i = 0; |
| 730 | i < sizeof(chain_protos)/sizeof(struct pprot); |
| 731 | i++) { |
| 732 | if (strcmp(s, chain_protos[i].name) == 0) { |
| 733 | proto = chain_protos[i].num; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | if (i == sizeof(chain_protos)/sizeof(struct pprot)) |
| 738 | exit_error(PARAMETER_PROBLEM, |
| 739 | "unknown protocol `%s' specified", |
| 740 | s); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return (u_int16_t)proto; |
| 745 | } |
| 746 | |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 747 | /* These are invalid numbers as upper layer protocol */ |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 748 | static int is_exthdr(u_int16_t proto) |
| 749 | { |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 750 | return (proto == IPPROTO_ROUTING || |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 751 | proto == IPPROTO_FRAGMENT || |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 752 | proto == IPPROTO_AH || |
| 753 | proto == IPPROTO_DSTOPTS); |
| 754 | } |
| 755 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 756 | /* Can't be zero. */ |
| 757 | static int |
| 758 | parse_rulenumber(const char *rule) |
| 759 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 760 | unsigned int rulenum; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 761 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 762 | if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 763 | exit_error(PARAMETER_PROBLEM, |
| 764 | "Invalid rule number `%s'", rule); |
| 765 | |
| 766 | return rulenum; |
| 767 | } |
| 768 | |
| 769 | static const char * |
| 770 | parse_target(const char *targetname) |
| 771 | { |
| 772 | const char *ptr; |
| 773 | |
| 774 | if (strlen(targetname) < 1) |
| 775 | exit_error(PARAMETER_PROBLEM, |
| 776 | "Invalid target name (too short)"); |
| 777 | |
| 778 | if (strlen(targetname)+1 > sizeof(ip6t_chainlabel)) |
| 779 | exit_error(PARAMETER_PROBLEM, |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 780 | "Invalid target name `%s' (%u chars max)", |
| 781 | targetname, (unsigned int)sizeof(ip6t_chainlabel)-1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 782 | |
| 783 | for (ptr = targetname; *ptr; ptr++) |
| 784 | if (isspace(*ptr)) |
| 785 | exit_error(PARAMETER_PROBLEM, |
| 786 | "Invalid target name `%s'", targetname); |
| 787 | return targetname; |
| 788 | } |
| 789 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 790 | static void |
| 791 | set_option(unsigned int *options, unsigned int option, u_int8_t *invflg, |
| 792 | int invert) |
| 793 | { |
| 794 | if (*options & option) |
| 795 | exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed", |
| 796 | opt2char(option)); |
| 797 | *options |= option; |
| 798 | |
| 799 | if (invert) { |
| 800 | unsigned int i; |
| 801 | for (i = 0; 1 << i != option; i++); |
| 802 | |
| 803 | if (!inverse_for_options[i]) |
| 804 | exit_error(PARAMETER_PROBLEM, |
| 805 | "cannot have ! before -%c", |
| 806 | opt2char(option)); |
| 807 | *invflg |= inverse_for_options[i]; |
| 808 | } |
| 809 | } |
| 810 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 811 | static struct option * |
Jan Echternach | af8fe9e | 2000-08-27 07:41:39 +0000 | [diff] [blame] | 812 | merge_options(struct option *oldopts, const struct option *newopts, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 813 | unsigned int *option_offset) |
| 814 | { |
| 815 | unsigned int num_old, num_new, i; |
| 816 | struct option *merge; |
| 817 | |
Jan Engelhardt | d014540 | 2007-07-30 14:32:26 +0000 | [diff] [blame] | 818 | if (newopts == NULL) |
| 819 | return oldopts; |
| 820 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 821 | for (num_old = 0; oldopts[num_old].name; num_old++); |
| 822 | for (num_new = 0; newopts[num_new].name; num_new++); |
| 823 | |
| 824 | global_option_offset += OPTION_OFFSET; |
| 825 | *option_offset = global_option_offset; |
| 826 | |
| 827 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
| 828 | memcpy(merge, oldopts, num_old * sizeof(struct option)); |
Marcus Sundberg | d91ed75 | 2005-07-29 13:26:35 +0000 | [diff] [blame] | 829 | free_opts(0); /* Release previous options merged if any */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 830 | for (i = 0; i < num_new; i++) { |
| 831 | merge[num_old + i] = newopts[i]; |
| 832 | merge[num_old + i].val += *option_offset; |
| 833 | } |
| 834 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
| 835 | |
| 836 | return merge; |
| 837 | } |
| 838 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 839 | void register_match6(struct ip6tables_match *me) |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 840 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 841 | me->family = AF_INET6; |
| 842 | xtables_register_match(me); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 845 | void register_target6(struct ip6tables_target *me) |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 846 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 847 | me->family = AF_INET6; |
| 848 | xtables_register_target(me); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void |
| 852 | print_num(u_int64_t number, unsigned int format) |
| 853 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 854 | if (format & FMT_KILOMEGAGIGA) { |
| 855 | if (number > 99999) { |
| 856 | number = (number + 500) / 1000; |
| 857 | if (number > 9999) { |
| 858 | number = (number + 500) / 1000; |
| 859 | if (number > 9999) { |
| 860 | number = (number + 500) / 1000; |
| 861 | if (number > 9999) { |
| 862 | number = (number + 500) / 1000; |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 863 | printf(FMT("%4lluT ","%lluT "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 864 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 865 | else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 866 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 867 | else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 868 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 869 | printf(FMT("%4lluK ","%lluK "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 870 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 871 | printf(FMT("%5llu ","%llu "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 872 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 873 | printf(FMT("%8llu ","%llu "), (unsigned long long)number); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 876 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 877 | static void |
| 878 | print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle) |
| 879 | { |
| 880 | struct ip6t_counters counters; |
| 881 | const char *pol = ip6tc_get_policy(chain, &counters, handle); |
| 882 | printf("Chain %s", chain); |
| 883 | if (pol) { |
| 884 | printf(" (policy %s", pol); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 885 | if (!(format & FMT_NOCOUNTS)) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 886 | fputc(' ', stdout); |
| 887 | print_num(counters.pcnt, (format|FMT_NOTABLE)); |
| 888 | fputs("packets, ", stdout); |
| 889 | print_num(counters.bcnt, (format|FMT_NOTABLE)); |
| 890 | fputs("bytes", stdout); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 891 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 892 | printf(")\n"); |
| 893 | } else { |
| 894 | unsigned int refs; |
| 895 | if (!ip6tc_get_references(&refs, chain, handle)) |
| 896 | printf(" (ERROR obtaining refs)\n"); |
| 897 | else |
| 898 | printf(" (%u references)\n", refs); |
| 899 | } |
| 900 | |
| 901 | if (format & FMT_LINENUMBERS) |
| 902 | printf(FMT("%-4s ", "%s "), "num"); |
| 903 | if (!(format & FMT_NOCOUNTS)) { |
| 904 | if (format & FMT_KILOMEGAGIGA) { |
| 905 | printf(FMT("%5s ","%s "), "pkts"); |
| 906 | printf(FMT("%5s ","%s "), "bytes"); |
| 907 | } else { |
| 908 | printf(FMT("%8s ","%s "), "pkts"); |
| 909 | printf(FMT("%10s ","%s "), "bytes"); |
| 910 | } |
| 911 | } |
| 912 | if (!(format & FMT_NOTARGET)) |
| 913 | printf(FMT("%-9s ","%s "), "target"); |
| 914 | fputs(" prot ", stdout); |
| 915 | if (format & FMT_OPTIONS) |
| 916 | fputs("opt", stdout); |
| 917 | if (format & FMT_VIA) { |
| 918 | printf(FMT(" %-6s ","%s "), "in"); |
| 919 | printf(FMT("%-6s ","%s "), "out"); |
| 920 | } |
| 921 | printf(FMT(" %-19s ","%s "), "source"); |
| 922 | printf(FMT(" %-19s "," %s "), "destination"); |
| 923 | printf("\n"); |
| 924 | } |
| 925 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 926 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 927 | static int |
| 928 | print_match(const struct ip6t_entry_match *m, |
| 929 | const struct ip6t_ip6 *ip, |
| 930 | int numeric) |
| 931 | { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 932 | struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 933 | |
| 934 | if (match) { |
| 935 | if (match->print) |
| 936 | match->print(ip, m, numeric); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 937 | else |
| 938 | printf("%s ", match->name); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 939 | } else { |
| 940 | if (m->u.user.name[0]) |
| 941 | printf("UNKNOWN match `%s' ", m->u.user.name); |
| 942 | } |
| 943 | /* Don't stop iterating. */ |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | /* e is called `fw' here for hysterical raisins */ |
| 948 | static void |
| 949 | print_firewall(const struct ip6t_entry *fw, |
| 950 | const char *targname, |
| 951 | unsigned int num, |
| 952 | unsigned int format, |
| 953 | const ip6tc_handle_t handle) |
| 954 | { |
| 955 | struct ip6tables_target *target = NULL; |
| 956 | const struct ip6t_entry_target *t; |
| 957 | u_int8_t flags; |
| 958 | char buf[BUFSIZ]; |
| 959 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 960 | if (!ip6tc_is_chain(targname, handle)) |
| 961 | target = find_target(targname, TRY_LOAD); |
| 962 | else |
| 963 | target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED); |
| 964 | |
| 965 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 966 | flags = fw->ipv6.flags; |
| 967 | |
| 968 | if (format & FMT_LINENUMBERS) |
| 969 | printf(FMT("%-4u ", "%u "), num+1); |
| 970 | |
| 971 | if (!(format & FMT_NOCOUNTS)) { |
| 972 | print_num(fw->counters.pcnt, format); |
| 973 | print_num(fw->counters.bcnt, format); |
| 974 | } |
| 975 | |
| 976 | if (!(format & FMT_NOTARGET)) |
| 977 | printf(FMT("%-9s ", "%s "), targname); |
| 978 | |
| 979 | fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout); |
| 980 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 981 | char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 982 | if (pname) |
| 983 | printf(FMT("%-5s", "%s "), pname); |
| 984 | else |
| 985 | printf(FMT("%-5hu", "%hu "), fw->ipv6.proto); |
| 986 | } |
| 987 | |
| 988 | if (format & FMT_OPTIONS) { |
| 989 | if (format & FMT_NOTABLE) |
| 990 | fputs("opt ", stdout); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 991 | fputc(' ', stdout); /* Invert flag of FRAG */ |
| 992 | fputc(' ', stdout); /* -f */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 993 | fputc(' ', stdout); |
| 994 | } |
| 995 | |
| 996 | if (format & FMT_VIA) { |
| 997 | char iface[IFNAMSIZ+2]; |
| 998 | |
| 999 | if (fw->ipv6.invflags & IP6T_INV_VIA_IN) { |
| 1000 | iface[0] = '!'; |
| 1001 | iface[1] = '\0'; |
| 1002 | } |
| 1003 | else iface[0] = '\0'; |
| 1004 | |
| 1005 | if (fw->ipv6.iniface[0] != '\0') { |
| 1006 | strcat(iface, fw->ipv6.iniface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1007 | } |
| 1008 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 1009 | else strcat(iface, "any"); |
| 1010 | printf(FMT(" %-6s ","in %s "), iface); |
| 1011 | |
| 1012 | if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) { |
| 1013 | iface[0] = '!'; |
| 1014 | iface[1] = '\0'; |
| 1015 | } |
| 1016 | else iface[0] = '\0'; |
| 1017 | |
| 1018 | if (fw->ipv6.outiface[0] != '\0') { |
| 1019 | strcat(iface, fw->ipv6.outiface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1020 | } |
| 1021 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 1022 | else strcat(iface, "any"); |
| 1023 | printf(FMT("%-6s ","out %s "), iface); |
| 1024 | } |
| 1025 | |
| 1026 | fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout); |
| 1027 | if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any) |
| 1028 | && !(format & FMT_NUMERIC)) |
| 1029 | printf(FMT("%-19s ","%s "), "anywhere"); |
| 1030 | else { |
| 1031 | if (format & FMT_NUMERIC) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1032 | sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1033 | else |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1034 | sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1035 | strcat(buf, mask_to_numeric(&(fw->ipv6.smsk))); |
| 1036 | printf(FMT("%-19s ","%s "), buf); |
| 1037 | } |
| 1038 | |
| 1039 | fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout); |
| 1040 | if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any) |
| 1041 | && !(format & FMT_NUMERIC)) |
| 1042 | printf(FMT("%-19s","-> %s"), "anywhere"); |
| 1043 | else { |
| 1044 | if (format & FMT_NUMERIC) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1045 | sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1046 | else |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1047 | sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1048 | strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk))); |
| 1049 | printf(FMT("%-19s","-> %s"), buf); |
| 1050 | } |
| 1051 | |
| 1052 | if (format & FMT_NOTABLE) |
| 1053 | fputs(" ", stdout); |
| 1054 | |
| 1055 | IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC); |
| 1056 | |
| 1057 | if (target) { |
| 1058 | if (target->print) |
| 1059 | /* Print the target information. */ |
| 1060 | target->print(&fw->ipv6, t, format & FMT_NUMERIC); |
| 1061 | } else if (t->u.target_size != sizeof(*t)) |
| 1062 | printf("[%u bytes of unknown target data] ", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1063 | (unsigned int)(t->u.target_size - sizeof(*t))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (!(format & FMT_NONEWLINE)) |
| 1066 | fputc('\n', stdout); |
| 1067 | } |
| 1068 | |
| 1069 | static void |
| 1070 | print_firewall_line(const struct ip6t_entry *fw, |
| 1071 | const ip6tc_handle_t h) |
| 1072 | { |
| 1073 | struct ip6t_entry_target *t; |
| 1074 | |
| 1075 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 1076 | print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h); |
| 1077 | } |
| 1078 | |
| 1079 | static int |
| 1080 | append_entry(const ip6t_chainlabel chain, |
| 1081 | struct ip6t_entry *fw, |
| 1082 | unsigned int nsaddrs, |
| 1083 | const struct in6_addr saddrs[], |
| 1084 | unsigned int ndaddrs, |
| 1085 | const struct in6_addr daddrs[], |
| 1086 | int verbose, |
| 1087 | ip6tc_handle_t *handle) |
| 1088 | { |
| 1089 | unsigned int i, j; |
| 1090 | int ret = 1; |
| 1091 | |
| 1092 | for (i = 0; i < nsaddrs; i++) { |
| 1093 | fw->ipv6.src = saddrs[i]; |
| 1094 | for (j = 0; j < ndaddrs; j++) { |
| 1095 | fw->ipv6.dst = daddrs[j]; |
| 1096 | if (verbose) |
| 1097 | print_firewall_line(fw, *handle); |
| 1098 | ret &= ip6tc_append_entry(chain, fw, handle); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | return ret; |
| 1103 | } |
| 1104 | |
| 1105 | static int |
| 1106 | replace_entry(const ip6t_chainlabel chain, |
| 1107 | struct ip6t_entry *fw, |
| 1108 | unsigned int rulenum, |
| 1109 | const struct in6_addr *saddr, |
| 1110 | const struct in6_addr *daddr, |
| 1111 | int verbose, |
| 1112 | ip6tc_handle_t *handle) |
| 1113 | { |
| 1114 | fw->ipv6.src = *saddr; |
| 1115 | fw->ipv6.dst = *daddr; |
| 1116 | |
| 1117 | if (verbose) |
| 1118 | print_firewall_line(fw, *handle); |
| 1119 | return ip6tc_replace_entry(chain, fw, rulenum, handle); |
| 1120 | } |
| 1121 | |
| 1122 | static int |
| 1123 | insert_entry(const ip6t_chainlabel chain, |
| 1124 | struct ip6t_entry *fw, |
| 1125 | unsigned int rulenum, |
| 1126 | unsigned int nsaddrs, |
| 1127 | const struct in6_addr saddrs[], |
| 1128 | unsigned int ndaddrs, |
| 1129 | const struct in6_addr daddrs[], |
| 1130 | int verbose, |
| 1131 | ip6tc_handle_t *handle) |
| 1132 | { |
| 1133 | unsigned int i, j; |
| 1134 | int ret = 1; |
| 1135 | |
| 1136 | for (i = 0; i < nsaddrs; i++) { |
| 1137 | fw->ipv6.src = saddrs[i]; |
| 1138 | for (j = 0; j < ndaddrs; j++) { |
| 1139 | fw->ipv6.dst = daddrs[j]; |
| 1140 | if (verbose) |
| 1141 | print_firewall_line(fw, *handle); |
| 1142 | ret &= ip6tc_insert_entry(chain, fw, rulenum, handle); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | static unsigned char * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1150 | make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1151 | { |
| 1152 | /* Establish mask for comparison */ |
| 1153 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1154 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1155 | unsigned char *mask, *mptr; |
| 1156 | |
| 1157 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1158 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1159 | size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1160 | |
| 1161 | mask = fw_calloc(1, size |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1162 | + IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1163 | + xtables_targets->size); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1164 | |
| 1165 | memset(mask, 0xFF, sizeof(struct ip6t_entry)); |
| 1166 | mptr = mask + sizeof(struct ip6t_entry); |
| 1167 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1168 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1169 | memset(mptr, 0xFF, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1170 | IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1171 | + matchp->match->userspacesize); |
| 1172 | mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1175 | memset(mptr, 0xFF, |
| 1176 | IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1177 | + xtables_targets->userspacesize); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1178 | |
| 1179 | return mask; |
| 1180 | } |
| 1181 | |
| 1182 | static int |
| 1183 | delete_entry(const ip6t_chainlabel chain, |
| 1184 | struct ip6t_entry *fw, |
| 1185 | unsigned int nsaddrs, |
| 1186 | const struct in6_addr saddrs[], |
| 1187 | unsigned int ndaddrs, |
| 1188 | const struct in6_addr daddrs[], |
| 1189 | int verbose, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1190 | ip6tc_handle_t *handle, |
| 1191 | struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1192 | { |
| 1193 | unsigned int i, j; |
| 1194 | int ret = 1; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1195 | unsigned char *mask; |
| 1196 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1197 | mask = make_delete_mask(fw, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1198 | for (i = 0; i < nsaddrs; i++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1199 | fw->ipv6.src = saddrs[i]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1200 | for (j = 0; j < ndaddrs; j++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1201 | fw->ipv6.dst = daddrs[j]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1202 | if (verbose) |
| 1203 | print_firewall_line(fw, *handle); |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1204 | ret &= ip6tc_delete_entry(chain, fw, mask, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1207 | free(mask); |
| 1208 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1209 | return ret; |
| 1210 | } |
| 1211 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1212 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1213 | for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), |
| 1214 | int verbose, int builtinstoo, ip6tc_handle_t *handle) |
| 1215 | { |
| 1216 | int ret = 1; |
| 1217 | const char *chain; |
| 1218 | char *chains; |
| 1219 | unsigned int i, chaincount = 0; |
| 1220 | |
| 1221 | chain = ip6tc_first_chain(handle); |
| 1222 | while (chain) { |
| 1223 | chaincount++; |
| 1224 | chain = ip6tc_next_chain(handle); |
| 1225 | } |
| 1226 | |
| 1227 | chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount); |
| 1228 | i = 0; |
| 1229 | chain = ip6tc_first_chain(handle); |
| 1230 | while (chain) { |
| 1231 | strcpy(chains + i*sizeof(ip6t_chainlabel), chain); |
| 1232 | i++; |
| 1233 | chain = ip6tc_next_chain(handle); |
| 1234 | } |
| 1235 | |
| 1236 | for (i = 0; i < chaincount; i++) { |
| 1237 | if (!builtinstoo |
| 1238 | && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel), |
Harald Welte | df1c71c | 2004-08-30 16:00:32 +0000 | [diff] [blame] | 1239 | *handle) == 1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1240 | continue; |
| 1241 | ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle); |
| 1242 | } |
| 1243 | |
| 1244 | free(chains); |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1248 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1249 | flush_entries(const ip6t_chainlabel chain, int verbose, |
| 1250 | ip6tc_handle_t *handle) |
| 1251 | { |
| 1252 | if (!chain) |
| 1253 | return for_each_chain(flush_entries, verbose, 1, handle); |
| 1254 | |
| 1255 | if (verbose) |
| 1256 | fprintf(stdout, "Flushing chain `%s'\n", chain); |
| 1257 | return ip6tc_flush_entries(chain, handle); |
| 1258 | } |
| 1259 | |
| 1260 | static int |
| 1261 | zero_entries(const ip6t_chainlabel chain, int verbose, |
| 1262 | ip6tc_handle_t *handle) |
| 1263 | { |
| 1264 | if (!chain) |
| 1265 | return for_each_chain(zero_entries, verbose, 1, handle); |
| 1266 | |
| 1267 | if (verbose) |
| 1268 | fprintf(stdout, "Zeroing chain `%s'\n", chain); |
| 1269 | return ip6tc_zero_entries(chain, handle); |
| 1270 | } |
| 1271 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1272 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1273 | delete_chain(const ip6t_chainlabel chain, int verbose, |
| 1274 | ip6tc_handle_t *handle) |
| 1275 | { |
| 1276 | if (!chain) |
| 1277 | return for_each_chain(delete_chain, verbose, 0, handle); |
| 1278 | |
| 1279 | if (verbose) |
| 1280 | fprintf(stdout, "Deleting chain `%s'\n", chain); |
| 1281 | return ip6tc_delete_chain(chain, handle); |
| 1282 | } |
| 1283 | |
| 1284 | static int |
| 1285 | list_entries(const ip6t_chainlabel chain, int verbose, int numeric, |
| 1286 | int expanded, int linenumbers, ip6tc_handle_t *handle) |
| 1287 | { |
| 1288 | int found = 0; |
| 1289 | unsigned int format; |
| 1290 | const char *this; |
| 1291 | |
| 1292 | format = FMT_OPTIONS; |
| 1293 | if (!verbose) |
| 1294 | format |= FMT_NOCOUNTS; |
| 1295 | else |
| 1296 | format |= FMT_VIA; |
| 1297 | |
| 1298 | if (numeric) |
| 1299 | format |= FMT_NUMERIC; |
| 1300 | |
| 1301 | if (!expanded) |
| 1302 | format |= FMT_KILOMEGAGIGA; |
| 1303 | |
| 1304 | if (linenumbers) |
| 1305 | format |= FMT_LINENUMBERS; |
| 1306 | |
| 1307 | for (this = ip6tc_first_chain(handle); |
| 1308 | this; |
| 1309 | this = ip6tc_next_chain(handle)) { |
| 1310 | const struct ip6t_entry *i; |
| 1311 | unsigned int num; |
| 1312 | |
| 1313 | if (chain && strcmp(chain, this) != 0) |
| 1314 | continue; |
| 1315 | |
| 1316 | if (found) printf("\n"); |
| 1317 | |
| 1318 | print_header(format, this, handle); |
| 1319 | i = ip6tc_first_rule(this, handle); |
| 1320 | |
| 1321 | num = 0; |
| 1322 | while (i) { |
| 1323 | print_firewall(i, |
| 1324 | ip6tc_get_target(i, handle), |
| 1325 | num++, |
| 1326 | format, |
| 1327 | *handle); |
| 1328 | i = ip6tc_next_rule(i, handle); |
| 1329 | } |
| 1330 | found = 1; |
| 1331 | } |
| 1332 | |
| 1333 | errno = ENOENT; |
| 1334 | return found; |
| 1335 | } |
| 1336 | |
| 1337 | static struct ip6t_entry * |
| 1338 | generate_entry(const struct ip6t_entry *fw, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1339 | struct ip6tables_rule_match *matches, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1340 | struct ip6t_entry_target *target) |
| 1341 | { |
| 1342 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1343 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1344 | struct ip6t_entry *e; |
| 1345 | |
| 1346 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1347 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1348 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1349 | |
| 1350 | e = fw_malloc(size + target->u.target_size); |
| 1351 | *e = *fw; |
| 1352 | e->target_offset = size; |
| 1353 | e->next_offset = size + target->u.target_size; |
| 1354 | |
| 1355 | size = 0; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1356 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 1357 | memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size); |
| 1358 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1359 | } |
| 1360 | memcpy(e->elems + size, target, target->u.target_size); |
| 1361 | |
| 1362 | return e; |
| 1363 | } |
| 1364 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1365 | void clear_rule_matches(struct ip6tables_rule_match **matches) |
| 1366 | { |
| 1367 | struct ip6tables_rule_match *matchp, *tmp; |
| 1368 | |
| 1369 | for (matchp = *matches; matchp;) { |
| 1370 | tmp = matchp->next; |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1371 | if (matchp->match->m) { |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1372 | free(matchp->match->m); |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1373 | matchp->match->m = NULL; |
| 1374 | } |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1375 | if (matchp->match == matchp->match->next) { |
| 1376 | free(matchp->match); |
| 1377 | matchp->match = NULL; |
| 1378 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1379 | free(matchp); |
| 1380 | matchp = tmp; |
| 1381 | } |
| 1382 | |
| 1383 | *matches = NULL; |
| 1384 | } |
| 1385 | |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1386 | static void set_revision(char *name, u_int8_t revision) |
| 1387 | { |
| 1388 | /* Old kernel sources don't have ".revision" field, |
| 1389 | but we stole a byte from name. */ |
| 1390 | name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0'; |
| 1391 | name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision; |
| 1392 | } |
| 1393 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1394 | int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle) |
| 1395 | { |
| 1396 | struct ip6t_entry fw, *e = NULL; |
| 1397 | int invert = 0; |
| 1398 | unsigned int nsaddrs = 0, ndaddrs = 0; |
| 1399 | struct in6_addr *saddrs = NULL, *daddrs = NULL; |
| 1400 | |
| 1401 | int c, verbose = 0; |
| 1402 | const char *chain = NULL; |
| 1403 | const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL; |
| 1404 | const char *policy = NULL, *newname = NULL; |
| 1405 | unsigned int rulenum = 0, options = 0, command = 0; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1406 | const char *pcnt = NULL, *bcnt = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1407 | int ret = 1; |
| 1408 | struct ip6tables_match *m; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1409 | struct ip6tables_rule_match *matches = NULL; |
| 1410 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1411 | struct ip6tables_target *target = NULL; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1412 | struct ip6tables_target *t; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1413 | const char *jumpto = ""; |
| 1414 | char *protocol = NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1415 | int proto_used = 0; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1416 | |
| 1417 | memset(&fw, 0, sizeof(fw)); |
| 1418 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1419 | /* re-set optind to 0 in case do_command gets called |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1420 | * a second time */ |
| 1421 | optind = 0; |
| 1422 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1423 | /* clear mflags in case do_command gets called a second time |
| 1424 | * (we clear the global list of all matches for security)*/ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1425 | for (m = xtables_matches; m; m = m->next) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1426 | m->mflags = 0; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1427 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1428 | for (t = xtables_targets; t; t = t->next) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1429 | t->tflags = 0; |
| 1430 | t->used = 0; |
| 1431 | } |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1432 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1433 | /* Suppress error messages: we may add new options if we |
| 1434 | demand-load a protocol. */ |
| 1435 | opterr = 0; |
| 1436 | |
| 1437 | while ((c = getopt_long(argc, argv, |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1438 | "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:", |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1439 | opts, NULL)) != -1) { |
| 1440 | switch (c) { |
| 1441 | /* |
| 1442 | * Command selection |
| 1443 | */ |
| 1444 | case 'A': |
| 1445 | add_command(&command, CMD_APPEND, CMD_NONE, |
| 1446 | invert); |
| 1447 | chain = optarg; |
| 1448 | break; |
| 1449 | |
| 1450 | case 'D': |
| 1451 | add_command(&command, CMD_DELETE, CMD_NONE, |
| 1452 | invert); |
| 1453 | chain = optarg; |
| 1454 | if (optind < argc && argv[optind][0] != '-' |
| 1455 | && argv[optind][0] != '!') { |
| 1456 | rulenum = parse_rulenumber(argv[optind++]); |
| 1457 | command = CMD_DELETE_NUM; |
| 1458 | } |
| 1459 | break; |
| 1460 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1461 | case 'R': |
| 1462 | add_command(&command, CMD_REPLACE, CMD_NONE, |
| 1463 | invert); |
| 1464 | chain = optarg; |
| 1465 | if (optind < argc && argv[optind][0] != '-' |
| 1466 | && argv[optind][0] != '!') |
| 1467 | rulenum = parse_rulenumber(argv[optind++]); |
| 1468 | else |
| 1469 | exit_error(PARAMETER_PROBLEM, |
| 1470 | "-%c requires a rule number", |
| 1471 | cmd2char(CMD_REPLACE)); |
| 1472 | break; |
| 1473 | |
| 1474 | case 'I': |
| 1475 | add_command(&command, CMD_INSERT, CMD_NONE, |
| 1476 | invert); |
| 1477 | chain = optarg; |
| 1478 | if (optind < argc && argv[optind][0] != '-' |
| 1479 | && argv[optind][0] != '!') |
| 1480 | rulenum = parse_rulenumber(argv[optind++]); |
| 1481 | else rulenum = 1; |
| 1482 | break; |
| 1483 | |
| 1484 | case 'L': |
| 1485 | add_command(&command, CMD_LIST, CMD_ZERO, |
| 1486 | invert); |
| 1487 | if (optarg) chain = optarg; |
| 1488 | else if (optind < argc && argv[optind][0] != '-' |
| 1489 | && argv[optind][0] != '!') |
| 1490 | chain = argv[optind++]; |
| 1491 | break; |
| 1492 | |
| 1493 | case 'F': |
| 1494 | add_command(&command, CMD_FLUSH, CMD_NONE, |
| 1495 | invert); |
| 1496 | if (optarg) chain = optarg; |
| 1497 | else if (optind < argc && argv[optind][0] != '-' |
| 1498 | && argv[optind][0] != '!') |
| 1499 | chain = argv[optind++]; |
| 1500 | break; |
| 1501 | |
| 1502 | case 'Z': |
| 1503 | add_command(&command, CMD_ZERO, CMD_LIST, |
| 1504 | invert); |
| 1505 | if (optarg) chain = optarg; |
| 1506 | else if (optind < argc && argv[optind][0] != '-' |
| 1507 | && argv[optind][0] != '!') |
| 1508 | chain = argv[optind++]; |
| 1509 | break; |
| 1510 | |
| 1511 | case 'N': |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1512 | if (optarg && (*optarg == '-' || *optarg == '!')) |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1513 | exit_error(PARAMETER_PROBLEM, |
| 1514 | "chain name not allowed to start " |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1515 | "with `%c'\n", *optarg); |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1516 | if (find_target(optarg, TRY_LOAD)) |
| 1517 | exit_error(PARAMETER_PROBLEM, |
| 1518 | "chain name may not clash " |
| 1519 | "with target name\n"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1520 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
| 1521 | invert); |
| 1522 | chain = optarg; |
| 1523 | break; |
| 1524 | |
| 1525 | case 'X': |
| 1526 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
| 1527 | invert); |
| 1528 | if (optarg) chain = optarg; |
| 1529 | else if (optind < argc && argv[optind][0] != '-' |
| 1530 | && argv[optind][0] != '!') |
| 1531 | chain = argv[optind++]; |
| 1532 | break; |
| 1533 | |
| 1534 | case 'E': |
| 1535 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
| 1536 | invert); |
| 1537 | chain = optarg; |
| 1538 | if (optind < argc && argv[optind][0] != '-' |
| 1539 | && argv[optind][0] != '!') |
| 1540 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1541 | else |
| 1542 | exit_error(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 950ddc6 | 2007-06-12 01:36:26 +0000 | [diff] [blame] | 1543 | "-%c requires old-chain-name and " |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1544 | "new-chain-name", |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1545 | cmd2char(CMD_RENAME_CHAIN)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1546 | break; |
| 1547 | |
| 1548 | case 'P': |
| 1549 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
| 1550 | invert); |
| 1551 | chain = optarg; |
| 1552 | if (optind < argc && argv[optind][0] != '-' |
| 1553 | && argv[optind][0] != '!') |
| 1554 | policy = argv[optind++]; |
| 1555 | else |
| 1556 | exit_error(PARAMETER_PROBLEM, |
| 1557 | "-%c requires a chain and a policy", |
| 1558 | cmd2char(CMD_SET_POLICY)); |
| 1559 | break; |
| 1560 | |
| 1561 | case 'h': |
| 1562 | if (!optarg) |
| 1563 | optarg = argv[optind]; |
| 1564 | |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1565 | /* ip6tables -p icmp -h */ |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1566 | if (!matches && protocol) |
| 1567 | find_match(protocol, TRY_LOAD, &matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1568 | |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1569 | exit_printhelp(matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1570 | |
| 1571 | /* |
| 1572 | * Option selection |
| 1573 | */ |
| 1574 | case 'p': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1575 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1576 | set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags, |
| 1577 | invert); |
| 1578 | |
| 1579 | /* Canonicalize into lower case */ |
| 1580 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 1581 | *protocol = tolower(*protocol); |
| 1582 | |
| 1583 | protocol = argv[optind-1]; |
| 1584 | fw.ipv6.proto = parse_protocol(protocol); |
| 1585 | fw.ipv6.flags |= IP6T_F_PROTO; |
| 1586 | |
| 1587 | if (fw.ipv6.proto == 0 |
| 1588 | && (fw.ipv6.invflags & IP6T_INV_PROTO)) |
| 1589 | exit_error(PARAMETER_PROBLEM, |
| 1590 | "rule would never match protocol"); |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 1591 | |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 1592 | if (is_exthdr(fw.ipv6.proto) |
| 1593 | && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0) |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 1594 | printf("Warning: never matched protocol: %s. " |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 1595 | "use extension match instead.\n", |
| 1596 | protocol); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1597 | break; |
| 1598 | |
| 1599 | case 's': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1600 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1601 | set_option(&options, OPT_SOURCE, &fw.ipv6.invflags, |
| 1602 | invert); |
| 1603 | shostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1604 | break; |
| 1605 | |
| 1606 | case 'd': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1607 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1608 | set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags, |
| 1609 | invert); |
| 1610 | dhostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | |
| 1613 | case 'j': |
| 1614 | set_option(&options, OPT_JUMP, &fw.ipv6.invflags, |
| 1615 | invert); |
| 1616 | jumpto = parse_target(optarg); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1617 | /* TRY_LOAD (may be chain name) */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1618 | target = find_target(jumpto, TRY_LOAD); |
| 1619 | |
| 1620 | if (target) { |
| 1621 | size_t size; |
| 1622 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1623 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
| 1624 | + target->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1625 | |
| 1626 | target->t = fw_calloc(1, size); |
| 1627 | target->t->u.target_size = size; |
| 1628 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1629 | if (target->init != NULL) |
| 1630 | target->init(target->t, &fw.nfcache); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1631 | opts = merge_options(opts, target->extra_opts, &target->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1632 | } |
| 1633 | break; |
| 1634 | |
| 1635 | |
| 1636 | case 'i': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1637 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1638 | set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags, |
| 1639 | invert); |
| 1640 | parse_interface(argv[optind-1], |
| 1641 | fw.ipv6.iniface, |
| 1642 | fw.ipv6.iniface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | |
| 1645 | case 'o': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1646 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1647 | set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags, |
| 1648 | invert); |
| 1649 | parse_interface(argv[optind-1], |
| 1650 | fw.ipv6.outiface, |
| 1651 | fw.ipv6.outiface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | |
| 1654 | case 'v': |
| 1655 | if (!verbose) |
| 1656 | set_option(&options, OPT_VERBOSE, |
| 1657 | &fw.ipv6.invflags, invert); |
| 1658 | verbose++; |
| 1659 | break; |
| 1660 | |
| 1661 | case 'm': { |
| 1662 | size_t size; |
| 1663 | |
| 1664 | if (invert) |
| 1665 | exit_error(PARAMETER_PROBLEM, |
| 1666 | "unexpected ! flag before --match"); |
| 1667 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1668 | m = find_match(optarg, LOAD_MUST_SUCCEED, &matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1669 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1670 | + m->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1671 | m->m = fw_calloc(1, size); |
| 1672 | m->m->u.match_size = size; |
| 1673 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1674 | set_revision(m->m->u.user.name, m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1675 | if (m->init != NULL) |
| 1676 | m->init(m->m, &fw.nfcache); |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1677 | if (m != m->next) |
| 1678 | /* Merge options for non-cloned matches */ |
| 1679 | opts = merge_options(opts, m->extra_opts, &m->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1680 | } |
| 1681 | break; |
| 1682 | |
| 1683 | case 'n': |
| 1684 | set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags, |
| 1685 | invert); |
| 1686 | break; |
| 1687 | |
| 1688 | case 't': |
| 1689 | if (invert) |
| 1690 | exit_error(PARAMETER_PROBLEM, |
| 1691 | "unexpected ! flag before --table"); |
| 1692 | *table = argv[optind-1]; |
| 1693 | break; |
| 1694 | |
| 1695 | case 'x': |
| 1696 | set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags, |
| 1697 | invert); |
| 1698 | break; |
| 1699 | |
| 1700 | case 'V': |
| 1701 | if (invert) |
| 1702 | printf("Not %s ;-)\n", program_version); |
| 1703 | else |
| 1704 | printf("%s v%s\n", |
| 1705 | program_name, program_version); |
| 1706 | exit(0); |
| 1707 | |
| 1708 | case '0': |
| 1709 | set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags, |
| 1710 | invert); |
| 1711 | break; |
| 1712 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1713 | case 'M': |
| 1714 | modprobe = optarg; |
| 1715 | break; |
| 1716 | |
| 1717 | case 'c': |
| 1718 | |
| 1719 | set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags, |
| 1720 | invert); |
| 1721 | pcnt = optarg; |
| 1722 | if (optind < argc && argv[optind][0] != '-' |
| 1723 | && argv[optind][0] != '!') |
| 1724 | bcnt = argv[optind++]; |
| 1725 | else |
| 1726 | exit_error(PARAMETER_PROBLEM, |
| 1727 | "-%c requires packet and byte counter", |
| 1728 | opt2char(OPT_COUNTERS)); |
| 1729 | |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1730 | if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1731 | exit_error(PARAMETER_PROBLEM, |
| 1732 | "-%c packet counter not numeric", |
| 1733 | opt2char(OPT_COUNTERS)); |
| 1734 | |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1735 | if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1736 | exit_error(PARAMETER_PROBLEM, |
| 1737 | "-%c byte counter not numeric", |
| 1738 | opt2char(OPT_COUNTERS)); |
| 1739 | |
| 1740 | break; |
| 1741 | |
| 1742 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1743 | case 1: /* non option */ |
| 1744 | if (optarg[0] == '!' && optarg[1] == '\0') { |
| 1745 | if (invert) |
| 1746 | exit_error(PARAMETER_PROBLEM, |
| 1747 | "multiple consecutive ! not" |
| 1748 | " allowed"); |
| 1749 | invert = TRUE; |
| 1750 | optarg[0] = '\0'; |
| 1751 | continue; |
| 1752 | } |
| 1753 | printf("Bad argument `%s'\n", optarg); |
| 1754 | exit_tryhelp(2); |
| 1755 | |
| 1756 | default: |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1757 | if (!target |
| 1758 | || !(target->parse(c - target->option_offset, |
| 1759 | argv, invert, |
| 1760 | &target->tflags, |
| 1761 | &fw, &target->t))) { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1762 | for (matchp = matches; matchp; matchp = matchp->next) { |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1763 | if (matchp->completed) |
| 1764 | continue; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1765 | if (matchp->match->parse(c - matchp->match->option_offset, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1766 | argv, invert, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1767 | &matchp->match->mflags, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1768 | &fw, |
| 1769 | &fw.nfcache, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1770 | &matchp->match->m)) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1773 | m = matchp ? matchp->match : NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* If you listen carefully, you can |
| 1776 | actually hear this code suck. */ |
| 1777 | |
| 1778 | /* some explanations (after four different bugs |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1779 | * in 3 different releases): If we encounter a |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1780 | * parameter, that has not been parsed yet, |
| 1781 | * it's not an option of an explicitly loaded |
| 1782 | * match or a target. However, we support |
| 1783 | * implicit loading of the protocol match |
| 1784 | * extension. '-p tcp' means 'l4 proto 6' and |
| 1785 | * at the same time 'load tcp protocol match on |
| 1786 | * demand if we specify --dport'. |
| 1787 | * |
| 1788 | * To make this work, we need to make sure: |
| 1789 | * - the parameter has not been parsed by |
| 1790 | * a match (m above) |
| 1791 | * - a protocol has been specified |
| 1792 | * - the protocol extension has not been |
| 1793 | * loaded yet, or is loaded and unused |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1794 | * [think of ip6tables-restore!] |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1795 | * - the protocol extension can be successively |
| 1796 | * loaded |
| 1797 | */ |
| 1798 | if (m == NULL |
| 1799 | && protocol |
| 1800 | && (!find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1801 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1802 | || (find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1803 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1804 | && (proto_used == 0)) |
| 1805 | ) |
| 1806 | && (m = find_proto(protocol, TRY_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1807 | options&OPT_NUMERIC, &matches))) { |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1808 | /* Try loading protocol */ |
| 1809 | size_t size; |
| 1810 | |
| 1811 | proto_used = 1; |
| 1812 | |
| 1813 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1814 | + m->size; |
| 1815 | |
| 1816 | m->m = fw_calloc(1, size); |
| 1817 | m->m->u.match_size = size; |
| 1818 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1819 | set_revision(m->m->u.user.name, |
| 1820 | m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1821 | if (m->init != NULL) |
| 1822 | m->init(m->m, &fw.nfcache); |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1823 | |
| 1824 | opts = merge_options(opts, |
| 1825 | m->extra_opts, &m->option_offset); |
| 1826 | |
| 1827 | optind--; |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1831 | if (!m) |
| 1832 | exit_error(PARAMETER_PROBLEM, |
| 1833 | "Unknown arg `%s'", |
| 1834 | argv[optind-1]); |
| 1835 | } |
| 1836 | } |
| 1837 | invert = FALSE; |
| 1838 | } |
| 1839 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1840 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1841 | matchp->match->final_check(matchp->match->mflags); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1842 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1843 | if (target) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1844 | target->final_check(target->tflags); |
| 1845 | |
| 1846 | /* Fix me: must put inverse options checking here --MN */ |
| 1847 | |
| 1848 | if (optind < argc) |
| 1849 | exit_error(PARAMETER_PROBLEM, |
| 1850 | "unknown arguments found on commandline"); |
| 1851 | if (!command) |
| 1852 | exit_error(PARAMETER_PROBLEM, "no command specified"); |
| 1853 | if (invert) |
| 1854 | exit_error(PARAMETER_PROBLEM, |
| 1855 | "nothing appropriate following !"); |
| 1856 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1857 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1858 | if (!(options & OPT_DESTINATION)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1859 | dhostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1860 | if (!(options & OPT_SOURCE)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1861 | shostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | if (shostnetworkmask) |
| 1865 | parse_hostnetworkmask(shostnetworkmask, &saddrs, |
| 1866 | &(fw.ipv6.smsk), &nsaddrs); |
| 1867 | |
| 1868 | if (dhostnetworkmask) |
| 1869 | parse_hostnetworkmask(dhostnetworkmask, &daddrs, |
| 1870 | &(fw.ipv6.dmsk), &ndaddrs); |
| 1871 | |
| 1872 | if ((nsaddrs > 1 || ndaddrs > 1) && |
| 1873 | (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP))) |
| 1874 | exit_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
| 1875 | " source or destination IP addresses"); |
| 1876 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1877 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
| 1878 | exit_error(PARAMETER_PROBLEM, "Replacement rule does not " |
| 1879 | "specify a unique address"); |
| 1880 | |
| 1881 | generic_opt_check(command, options); |
| 1882 | |
| 1883 | if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN) |
| 1884 | exit_error(PARAMETER_PROBLEM, |
| 1885 | "chain name `%s' too long (must be under %i chars)", |
| 1886 | chain, IP6T_FUNCTION_MAXNAMELEN); |
| 1887 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1888 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 1889 | if (!*handle) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1890 | *handle = ip6tc_init(*table); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1891 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1892 | /* try to insmod the module if iptc_init failed */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1893 | if (!*handle && load_xtables_ko(modprobe, 0) != -1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1894 | *handle = ip6tc_init(*table); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1895 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1896 | if (!*handle) |
| 1897 | exit_error(VERSION_PROBLEM, |
| 1898 | "can't initialize ip6tables table `%s': %s", |
| 1899 | *table, ip6tc_strerror(errno)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1900 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1901 | if (command == CMD_APPEND |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1902 | || command == CMD_DELETE |
| 1903 | || command == CMD_INSERT |
| 1904 | || command == CMD_REPLACE) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1905 | if (strcmp(chain, "PREROUTING") == 0 |
| 1906 | || strcmp(chain, "INPUT") == 0) { |
| 1907 | /* -o not valid with incoming packets. */ |
| 1908 | if (options & OPT_VIANAMEOUT) |
| 1909 | exit_error(PARAMETER_PROBLEM, |
| 1910 | "Can't use -%c with %s\n", |
| 1911 | opt2char(OPT_VIANAMEOUT), |
| 1912 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1913 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1914 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1915 | if (strcmp(chain, "POSTROUTING") == 0 |
| 1916 | || strcmp(chain, "OUTPUT") == 0) { |
| 1917 | /* -i not valid with outgoing packets */ |
| 1918 | if (options & OPT_VIANAMEIN) |
| 1919 | exit_error(PARAMETER_PROBLEM, |
| 1920 | "Can't use -%c with %s\n", |
| 1921 | opt2char(OPT_VIANAMEIN), |
| 1922 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1923 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1924 | |
| 1925 | if (target && ip6tc_is_chain(jumpto, *handle)) { |
| 1926 | printf("Warning: using chain %s, not extension\n", |
| 1927 | jumpto); |
| 1928 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1929 | if (target->t) |
| 1930 | free(target->t); |
| 1931 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1932 | target = NULL; |
| 1933 | } |
| 1934 | |
| 1935 | /* If they didn't specify a target, or it's a chain |
| 1936 | name, use standard. */ |
| 1937 | if (!target |
| 1938 | && (strlen(jumpto) == 0 |
| 1939 | || ip6tc_is_chain(jumpto, *handle))) { |
| 1940 | size_t size; |
| 1941 | |
| 1942 | target = find_target(IP6T_STANDARD_TARGET, |
| 1943 | LOAD_MUST_SUCCEED); |
| 1944 | |
| 1945 | size = sizeof(struct ip6t_entry_target) |
| 1946 | + target->size; |
| 1947 | target->t = fw_calloc(1, size); |
| 1948 | target->t->u.target_size = size; |
| 1949 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1950 | if (target->init != NULL) |
| 1951 | target->init(target->t, &fw.nfcache); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | if (!target) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1955 | /* it is no chain, and we can't load a plugin. |
| 1956 | * We cannot know if the plugin is corrupt, non |
| 1957 | * existant OR if the user just misspelled a |
| 1958 | * chain. */ |
| 1959 | find_target(jumpto, LOAD_MUST_SUCCEED); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1960 | } else { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1961 | e = generate_entry(&fw, matches, target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1962 | free(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | switch (command) { |
| 1967 | case CMD_APPEND: |
| 1968 | ret = append_entry(chain, e, |
| 1969 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1970 | options&OPT_VERBOSE, |
| 1971 | handle); |
| 1972 | break; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1973 | case CMD_DELETE: |
| 1974 | ret = delete_entry(chain, e, |
| 1975 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1976 | options&OPT_VERBOSE, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1977 | handle, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1978 | break; |
| 1979 | case CMD_DELETE_NUM: |
| 1980 | ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle); |
| 1981 | break; |
| 1982 | case CMD_REPLACE: |
| 1983 | ret = replace_entry(chain, e, rulenum - 1, |
| 1984 | saddrs, daddrs, options&OPT_VERBOSE, |
| 1985 | handle); |
| 1986 | break; |
| 1987 | case CMD_INSERT: |
| 1988 | ret = insert_entry(chain, e, rulenum - 1, |
| 1989 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1990 | options&OPT_VERBOSE, |
| 1991 | handle); |
| 1992 | break; |
| 1993 | case CMD_LIST: |
| 1994 | ret = list_entries(chain, |
| 1995 | options&OPT_VERBOSE, |
| 1996 | options&OPT_NUMERIC, |
| 1997 | options&OPT_EXPANDED, |
| 1998 | options&OPT_LINENUMBERS, |
| 1999 | handle); |
| 2000 | break; |
| 2001 | case CMD_FLUSH: |
| 2002 | ret = flush_entries(chain, options&OPT_VERBOSE, handle); |
| 2003 | break; |
| 2004 | case CMD_ZERO: |
| 2005 | ret = zero_entries(chain, options&OPT_VERBOSE, handle); |
| 2006 | break; |
| 2007 | case CMD_LIST|CMD_ZERO: |
| 2008 | ret = list_entries(chain, |
| 2009 | options&OPT_VERBOSE, |
| 2010 | options&OPT_NUMERIC, |
| 2011 | options&OPT_EXPANDED, |
| 2012 | options&OPT_LINENUMBERS, |
| 2013 | handle); |
| 2014 | if (ret) |
| 2015 | ret = zero_entries(chain, |
| 2016 | options&OPT_VERBOSE, handle); |
| 2017 | break; |
| 2018 | case CMD_NEW_CHAIN: |
| 2019 | ret = ip6tc_create_chain(chain, handle); |
| 2020 | break; |
| 2021 | case CMD_DELETE_CHAIN: |
| 2022 | ret = delete_chain(chain, options&OPT_VERBOSE, handle); |
| 2023 | break; |
| 2024 | case CMD_RENAME_CHAIN: |
| 2025 | ret = ip6tc_rename_chain(chain, newname, handle); |
| 2026 | break; |
| 2027 | case CMD_SET_POLICY: |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 2028 | ret = ip6tc_set_policy(chain, policy, NULL, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2029 | break; |
| 2030 | default: |
| 2031 | /* We should never reach this... */ |
| 2032 | exit_tryhelp(2); |
| 2033 | } |
| 2034 | |
| 2035 | if (verbose > 1) |
| 2036 | dump_entries6(*handle); |
| 2037 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2038 | clear_rule_matches(&matches); |
| 2039 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2040 | if (e != NULL) { |
| 2041 | free(e); |
| 2042 | e = NULL; |
| 2043 | } |
| 2044 | |
| 2045 | for (c = 0; c < nsaddrs; c++) |
| 2046 | free(&saddrs[c]); |
| 2047 | |
| 2048 | for (c = 0; c < ndaddrs; c++) |
| 2049 | free(&daddrs[c]); |
| 2050 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 2051 | free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2052 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2053 | return ret; |
| 2054 | } |