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 | |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 270 | static void |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 271 | exit_tryhelp(int status) |
| 272 | { |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 273 | if (line != -1) |
| 274 | fprintf(stderr, "Error occurred at line: %d\n", line); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 275 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", |
| 276 | program_name, program_name ); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 277 | free_opts(1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 278 | exit(status); |
| 279 | } |
| 280 | |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 281 | static void |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 282 | exit_printhelp(struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 283 | { |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 284 | struct ip6tables_rule_match *matchp = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 285 | struct ip6tables_target *t = NULL; |
| 286 | |
| 287 | printf("%s v%s\n\n" |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 288 | "Usage: %s -[AD] chain rule-specification [options]\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 289 | " %s -[RI] chain rulenum rule-specification [options]\n" |
| 290 | " %s -D chain rulenum [options]\n" |
| 291 | " %s -[LFZ] [chain] [options]\n" |
| 292 | " %s -[NX] chain\n" |
| 293 | " %s -E old-chain-name new-chain-name\n" |
| 294 | " %s -P chain target [options]\n" |
| 295 | " %s -h (print this help information)\n\n", |
| 296 | program_name, program_version, program_name, program_name, |
| 297 | program_name, program_name, program_name, program_name, |
| 298 | program_name, program_name); |
| 299 | |
| 300 | printf( |
| 301 | "Commands:\n" |
| 302 | "Either long or short options are allowed.\n" |
| 303 | " --append -A chain Append to chain\n" |
| 304 | " --delete -D chain Delete matching rule from chain\n" |
| 305 | " --delete -D chain rulenum\n" |
| 306 | " Delete rule rulenum (1 = first) from chain\n" |
| 307 | " --insert -I chain [rulenum]\n" |
| 308 | " Insert in chain as rulenum (default 1=first)\n" |
| 309 | " --replace -R chain rulenum\n" |
| 310 | " Replace rule rulenum (1 = first) in chain\n" |
| 311 | " --list -L [chain] List the rules in a chain or all chains\n" |
| 312 | " --flush -F [chain] Delete all rules in chain or all chains\n" |
| 313 | " --zero -Z [chain] Zero counters in chain or all chains\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 314 | " --new -N chain Create a new user-defined chain\n" |
| 315 | " --delete-chain\n" |
| 316 | " -X [chain] Delete a user-defined chain\n" |
| 317 | " --policy -P chain target\n" |
| 318 | " Change policy on chain to target\n" |
| 319 | " --rename-chain\n" |
| 320 | " -E old-chain new-chain\n" |
| 321 | " Change chain name, (moving any references)\n" |
| 322 | |
| 323 | "Options:\n" |
| 324 | " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n" |
| 325 | " --source -s [!] address[/mask]\n" |
| 326 | " source specification\n" |
| 327 | " --destination -d [!] address[/mask]\n" |
| 328 | " destination specification\n" |
| 329 | " --in-interface -i [!] input name[+]\n" |
| 330 | " network interface name ([+] for wildcard)\n" |
| 331 | " --jump -j target\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 332 | " target for rule (may load target extension)\n" |
| 333 | " --match -m match\n" |
| 334 | " extended match (may load extension)\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 335 | " --numeric -n numeric output of addresses and ports\n" |
| 336 | " --out-interface -o [!] output name[+]\n" |
| 337 | " network interface name ([+] for wildcard)\n" |
| 338 | " --table -t table table to manipulate (default: `filter')\n" |
| 339 | " --verbose -v verbose mode\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 340 | " --line-numbers print line numbers when listing\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 341 | " --exact -x expand numbers (display exact values)\n" |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 342 | /*"[!] --fragment -f match second or further fragments only\n"*/ |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 343 | " --modprobe=<command> try to insert modules using this command\n" |
| 344 | " --set-counters PKTS BYTES set the counter during insert/append\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 345 | "[!] --version -V print package version.\n"); |
| 346 | |
| 347 | /* Print out any special helps. A user might like to be able to add a --help |
| 348 | 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] | 349 | specified matches & targets */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 350 | for (t = xtables_targets; t; t = t->next) { |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 351 | if (t->used) { |
| 352 | printf("\n"); |
| 353 | t->help(); |
| 354 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 355 | } |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 356 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 357 | printf("\n"); |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 358 | matchp->match->help(); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 359 | } |
| 360 | exit(0); |
| 361 | } |
| 362 | |
Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 363 | void |
| 364 | exit_error(enum exittype status, const char *msg, ...) |
| 365 | { |
| 366 | va_list args; |
| 367 | |
| 368 | va_start(args, msg); |
| 369 | fprintf(stderr, "%s v%s: ", program_name, program_version); |
| 370 | vfprintf(stderr, msg, args); |
| 371 | va_end(args); |
| 372 | fprintf(stderr, "\n"); |
| 373 | if (status == PARAMETER_PROBLEM) |
| 374 | exit_tryhelp(status); |
| 375 | if (status == VERSION_PROBLEM) |
| 376 | fprintf(stderr, |
| 377 | "Perhaps ip6tables or your kernel needs to be upgraded.\n"); |
| 378 | /* On error paths, make sure that we don't leak memory */ |
| 379 | free_opts(1); |
| 380 | exit(status); |
| 381 | } |
| 382 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 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; |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1416 | unsigned long long cnt; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1417 | |
| 1418 | memset(&fw, 0, sizeof(fw)); |
| 1419 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1420 | /* 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] | 1421 | * a second time */ |
| 1422 | optind = 0; |
| 1423 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1424 | /* clear mflags in case do_command gets called a second time |
| 1425 | * (we clear the global list of all matches for security)*/ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1426 | for (m = xtables_matches; m; m = m->next) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1427 | m->mflags = 0; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1428 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1429 | for (t = xtables_targets; t; t = t->next) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1430 | t->tflags = 0; |
| 1431 | t->used = 0; |
| 1432 | } |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1433 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1434 | /* Suppress error messages: we may add new options if we |
| 1435 | demand-load a protocol. */ |
| 1436 | opterr = 0; |
| 1437 | |
| 1438 | while ((c = getopt_long(argc, argv, |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1439 | "-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] | 1440 | opts, NULL)) != -1) { |
| 1441 | switch (c) { |
| 1442 | /* |
| 1443 | * Command selection |
| 1444 | */ |
| 1445 | case 'A': |
| 1446 | add_command(&command, CMD_APPEND, CMD_NONE, |
| 1447 | invert); |
| 1448 | chain = optarg; |
| 1449 | break; |
| 1450 | |
| 1451 | case 'D': |
| 1452 | add_command(&command, CMD_DELETE, CMD_NONE, |
| 1453 | invert); |
| 1454 | chain = optarg; |
| 1455 | if (optind < argc && argv[optind][0] != '-' |
| 1456 | && argv[optind][0] != '!') { |
| 1457 | rulenum = parse_rulenumber(argv[optind++]); |
| 1458 | command = CMD_DELETE_NUM; |
| 1459 | } |
| 1460 | break; |
| 1461 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1462 | case 'R': |
| 1463 | add_command(&command, CMD_REPLACE, CMD_NONE, |
| 1464 | invert); |
| 1465 | chain = optarg; |
| 1466 | if (optind < argc && argv[optind][0] != '-' |
| 1467 | && argv[optind][0] != '!') |
| 1468 | rulenum = parse_rulenumber(argv[optind++]); |
| 1469 | else |
| 1470 | exit_error(PARAMETER_PROBLEM, |
| 1471 | "-%c requires a rule number", |
| 1472 | cmd2char(CMD_REPLACE)); |
| 1473 | break; |
| 1474 | |
| 1475 | case 'I': |
| 1476 | add_command(&command, CMD_INSERT, CMD_NONE, |
| 1477 | invert); |
| 1478 | chain = optarg; |
| 1479 | if (optind < argc && argv[optind][0] != '-' |
| 1480 | && argv[optind][0] != '!') |
| 1481 | rulenum = parse_rulenumber(argv[optind++]); |
| 1482 | else rulenum = 1; |
| 1483 | break; |
| 1484 | |
| 1485 | case 'L': |
| 1486 | add_command(&command, CMD_LIST, CMD_ZERO, |
| 1487 | invert); |
| 1488 | if (optarg) chain = optarg; |
| 1489 | else if (optind < argc && argv[optind][0] != '-' |
| 1490 | && argv[optind][0] != '!') |
| 1491 | chain = argv[optind++]; |
| 1492 | break; |
| 1493 | |
| 1494 | case 'F': |
| 1495 | add_command(&command, CMD_FLUSH, CMD_NONE, |
| 1496 | invert); |
| 1497 | if (optarg) chain = optarg; |
| 1498 | else if (optind < argc && argv[optind][0] != '-' |
| 1499 | && argv[optind][0] != '!') |
| 1500 | chain = argv[optind++]; |
| 1501 | break; |
| 1502 | |
| 1503 | case 'Z': |
| 1504 | add_command(&command, CMD_ZERO, CMD_LIST, |
| 1505 | invert); |
| 1506 | if (optarg) chain = optarg; |
| 1507 | else if (optind < argc && argv[optind][0] != '-' |
| 1508 | && argv[optind][0] != '!') |
| 1509 | chain = argv[optind++]; |
| 1510 | break; |
| 1511 | |
| 1512 | case 'N': |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1513 | if (optarg && (*optarg == '-' || *optarg == '!')) |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1514 | exit_error(PARAMETER_PROBLEM, |
| 1515 | "chain name not allowed to start " |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1516 | "with `%c'\n", *optarg); |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1517 | if (find_target(optarg, TRY_LOAD)) |
| 1518 | exit_error(PARAMETER_PROBLEM, |
| 1519 | "chain name may not clash " |
| 1520 | "with target name\n"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1521 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
| 1522 | invert); |
| 1523 | chain = optarg; |
| 1524 | break; |
| 1525 | |
| 1526 | case 'X': |
| 1527 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
| 1528 | invert); |
| 1529 | if (optarg) chain = optarg; |
| 1530 | else if (optind < argc && argv[optind][0] != '-' |
| 1531 | && argv[optind][0] != '!') |
| 1532 | chain = argv[optind++]; |
| 1533 | break; |
| 1534 | |
| 1535 | case 'E': |
| 1536 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
| 1537 | invert); |
| 1538 | chain = optarg; |
| 1539 | if (optind < argc && argv[optind][0] != '-' |
| 1540 | && argv[optind][0] != '!') |
| 1541 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1542 | else |
| 1543 | exit_error(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 950ddc6 | 2007-06-12 01:36:26 +0000 | [diff] [blame] | 1544 | "-%c requires old-chain-name and " |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1545 | "new-chain-name", |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1546 | cmd2char(CMD_RENAME_CHAIN)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | |
| 1549 | case 'P': |
| 1550 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
| 1551 | invert); |
| 1552 | chain = optarg; |
| 1553 | if (optind < argc && argv[optind][0] != '-' |
| 1554 | && argv[optind][0] != '!') |
| 1555 | policy = argv[optind++]; |
| 1556 | else |
| 1557 | exit_error(PARAMETER_PROBLEM, |
| 1558 | "-%c requires a chain and a policy", |
| 1559 | cmd2char(CMD_SET_POLICY)); |
| 1560 | break; |
| 1561 | |
| 1562 | case 'h': |
| 1563 | if (!optarg) |
| 1564 | optarg = argv[optind]; |
| 1565 | |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1566 | /* ip6tables -p icmp -h */ |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1567 | if (!matches && protocol) |
| 1568 | find_match(protocol, TRY_LOAD, &matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1569 | |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1570 | exit_printhelp(matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* |
| 1573 | * Option selection |
| 1574 | */ |
| 1575 | case 'p': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1576 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1577 | set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags, |
| 1578 | invert); |
| 1579 | |
| 1580 | /* Canonicalize into lower case */ |
| 1581 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 1582 | *protocol = tolower(*protocol); |
| 1583 | |
| 1584 | protocol = argv[optind-1]; |
| 1585 | fw.ipv6.proto = parse_protocol(protocol); |
| 1586 | fw.ipv6.flags |= IP6T_F_PROTO; |
| 1587 | |
| 1588 | if (fw.ipv6.proto == 0 |
| 1589 | && (fw.ipv6.invflags & IP6T_INV_PROTO)) |
| 1590 | exit_error(PARAMETER_PROBLEM, |
| 1591 | "rule would never match protocol"); |
Yasuyuki KOZAKAI | 78716a9 | 2006-03-29 09:24:43 +0000 | [diff] [blame] | 1592 | |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 1593 | if (is_exthdr(fw.ipv6.proto) |
| 1594 | && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0) |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1595 | fprintf(stderr, |
| 1596 | "Warning: never matched protocol: %s. " |
| 1597 | "use extension match instead.\n", |
| 1598 | protocol); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | |
| 1601 | case 's': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1602 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1603 | set_option(&options, OPT_SOURCE, &fw.ipv6.invflags, |
| 1604 | invert); |
| 1605 | shostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | |
| 1608 | case 'd': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1609 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1610 | set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags, |
| 1611 | invert); |
| 1612 | dhostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | |
| 1615 | case 'j': |
| 1616 | set_option(&options, OPT_JUMP, &fw.ipv6.invflags, |
| 1617 | invert); |
| 1618 | jumpto = parse_target(optarg); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1619 | /* TRY_LOAD (may be chain name) */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1620 | target = find_target(jumpto, TRY_LOAD); |
| 1621 | |
| 1622 | if (target) { |
| 1623 | size_t size; |
| 1624 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1625 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
| 1626 | + target->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1627 | |
| 1628 | target->t = fw_calloc(1, size); |
| 1629 | target->t->u.target_size = size; |
| 1630 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1631 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1632 | target->init(target->t); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1633 | opts = merge_options(opts, target->extra_opts, &target->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1634 | } |
| 1635 | break; |
| 1636 | |
| 1637 | |
| 1638 | case 'i': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1639 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1640 | set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags, |
| 1641 | invert); |
| 1642 | parse_interface(argv[optind-1], |
| 1643 | fw.ipv6.iniface, |
| 1644 | fw.ipv6.iniface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1645 | break; |
| 1646 | |
| 1647 | case 'o': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1648 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1649 | set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags, |
| 1650 | invert); |
| 1651 | parse_interface(argv[optind-1], |
| 1652 | fw.ipv6.outiface, |
| 1653 | fw.ipv6.outiface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | |
| 1656 | case 'v': |
| 1657 | if (!verbose) |
| 1658 | set_option(&options, OPT_VERBOSE, |
| 1659 | &fw.ipv6.invflags, invert); |
| 1660 | verbose++; |
| 1661 | break; |
| 1662 | |
| 1663 | case 'm': { |
| 1664 | size_t size; |
| 1665 | |
| 1666 | if (invert) |
| 1667 | exit_error(PARAMETER_PROBLEM, |
| 1668 | "unexpected ! flag before --match"); |
| 1669 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1670 | m = find_match(optarg, LOAD_MUST_SUCCEED, &matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1671 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1672 | + m->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1673 | m->m = fw_calloc(1, size); |
| 1674 | m->m->u.match_size = size; |
| 1675 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1676 | set_revision(m->m->u.user.name, m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1677 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1678 | m->init(m->m); |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1679 | if (m != m->next) |
| 1680 | /* Merge options for non-cloned matches */ |
| 1681 | opts = merge_options(opts, m->extra_opts, &m->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1682 | } |
| 1683 | break; |
| 1684 | |
| 1685 | case 'n': |
| 1686 | set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags, |
| 1687 | invert); |
| 1688 | break; |
| 1689 | |
| 1690 | case 't': |
| 1691 | if (invert) |
| 1692 | exit_error(PARAMETER_PROBLEM, |
| 1693 | "unexpected ! flag before --table"); |
| 1694 | *table = argv[optind-1]; |
| 1695 | break; |
| 1696 | |
| 1697 | case 'x': |
| 1698 | set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags, |
| 1699 | invert); |
| 1700 | break; |
| 1701 | |
| 1702 | case 'V': |
| 1703 | if (invert) |
| 1704 | printf("Not %s ;-)\n", program_version); |
| 1705 | else |
| 1706 | printf("%s v%s\n", |
| 1707 | program_name, program_version); |
| 1708 | exit(0); |
| 1709 | |
| 1710 | case '0': |
| 1711 | set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags, |
| 1712 | invert); |
| 1713 | break; |
| 1714 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1715 | case 'M': |
| 1716 | modprobe = optarg; |
| 1717 | break; |
| 1718 | |
| 1719 | case 'c': |
| 1720 | |
| 1721 | set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags, |
| 1722 | invert); |
| 1723 | pcnt = optarg; |
| 1724 | if (optind < argc && argv[optind][0] != '-' |
| 1725 | && argv[optind][0] != '!') |
| 1726 | bcnt = argv[optind++]; |
| 1727 | else |
| 1728 | exit_error(PARAMETER_PROBLEM, |
| 1729 | "-%c requires packet and byte counter", |
| 1730 | opt2char(OPT_COUNTERS)); |
| 1731 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1732 | if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1733 | exit_error(PARAMETER_PROBLEM, |
| 1734 | "-%c packet counter not numeric", |
| 1735 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1736 | fw.counters.pcnt = cnt; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1737 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1738 | if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1739 | exit_error(PARAMETER_PROBLEM, |
| 1740 | "-%c byte counter not numeric", |
| 1741 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1742 | fw.counters.bcnt = cnt; |
| 1743 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | |
| 1746 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1747 | case 1: /* non option */ |
| 1748 | if (optarg[0] == '!' && optarg[1] == '\0') { |
| 1749 | if (invert) |
| 1750 | exit_error(PARAMETER_PROBLEM, |
| 1751 | "multiple consecutive ! not" |
| 1752 | " allowed"); |
| 1753 | invert = TRUE; |
| 1754 | optarg[0] = '\0'; |
| 1755 | continue; |
| 1756 | } |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1757 | fprintf(stderr, "Bad argument `%s'\n", optarg); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1758 | exit_tryhelp(2); |
| 1759 | |
| 1760 | default: |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1761 | if (!target |
| 1762 | || !(target->parse(c - target->option_offset, |
| 1763 | argv, invert, |
| 1764 | &target->tflags, |
| 1765 | &fw, &target->t))) { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1766 | for (matchp = matches; matchp; matchp = matchp->next) { |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1767 | if (matchp->completed) |
| 1768 | continue; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1769 | if (matchp->match->parse(c - matchp->match->option_offset, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1770 | argv, invert, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1771 | &matchp->match->mflags, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1772 | &fw, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1773 | &matchp->match->m)) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1774 | break; |
| 1775 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1776 | m = matchp ? matchp->match : NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1777 | |
| 1778 | /* If you listen carefully, you can |
| 1779 | actually hear this code suck. */ |
| 1780 | |
| 1781 | /* some explanations (after four different bugs |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1782 | * in 3 different releases): If we encounter a |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1783 | * parameter, that has not been parsed yet, |
| 1784 | * it's not an option of an explicitly loaded |
| 1785 | * match or a target. However, we support |
| 1786 | * implicit loading of the protocol match |
| 1787 | * extension. '-p tcp' means 'l4 proto 6' and |
| 1788 | * at the same time 'load tcp protocol match on |
| 1789 | * demand if we specify --dport'. |
| 1790 | * |
| 1791 | * To make this work, we need to make sure: |
| 1792 | * - the parameter has not been parsed by |
| 1793 | * a match (m above) |
| 1794 | * - a protocol has been specified |
| 1795 | * - the protocol extension has not been |
| 1796 | * loaded yet, or is loaded and unused |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1797 | * [think of ip6tables-restore!] |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1798 | * - the protocol extension can be successively |
| 1799 | * loaded |
| 1800 | */ |
| 1801 | if (m == NULL |
| 1802 | && protocol |
| 1803 | && (!find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1804 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1805 | || (find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1806 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1807 | && (proto_used == 0)) |
| 1808 | ) |
| 1809 | && (m = find_proto(protocol, TRY_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1810 | options&OPT_NUMERIC, &matches))) { |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1811 | /* Try loading protocol */ |
| 1812 | size_t size; |
| 1813 | |
| 1814 | proto_used = 1; |
| 1815 | |
| 1816 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1817 | + m->size; |
| 1818 | |
| 1819 | m->m = fw_calloc(1, size); |
| 1820 | m->m->u.match_size = size; |
| 1821 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1822 | set_revision(m->m->u.user.name, |
| 1823 | m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1824 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1825 | m->init(m->m); |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1826 | |
| 1827 | opts = merge_options(opts, |
| 1828 | m->extra_opts, &m->option_offset); |
| 1829 | |
| 1830 | optind--; |
| 1831 | continue; |
| 1832 | } |
| 1833 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1834 | if (!m) |
| 1835 | exit_error(PARAMETER_PROBLEM, |
| 1836 | "Unknown arg `%s'", |
| 1837 | argv[optind-1]); |
| 1838 | } |
| 1839 | } |
| 1840 | invert = FALSE; |
| 1841 | } |
| 1842 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1843 | for (matchp = matches; matchp; matchp = matchp->next) |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1844 | if (matchp->match->final_check != NULL) |
| 1845 | matchp->match->final_check(matchp->match->mflags); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1846 | |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1847 | if (target != NULL && target->final_check != NULL) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1848 | target->final_check(target->tflags); |
| 1849 | |
| 1850 | /* Fix me: must put inverse options checking here --MN */ |
| 1851 | |
| 1852 | if (optind < argc) |
| 1853 | exit_error(PARAMETER_PROBLEM, |
| 1854 | "unknown arguments found on commandline"); |
| 1855 | if (!command) |
| 1856 | exit_error(PARAMETER_PROBLEM, "no command specified"); |
| 1857 | if (invert) |
| 1858 | exit_error(PARAMETER_PROBLEM, |
| 1859 | "nothing appropriate following !"); |
| 1860 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1861 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1862 | if (!(options & OPT_DESTINATION)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1863 | dhostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1864 | if (!(options & OPT_SOURCE)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1865 | shostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | if (shostnetworkmask) |
| 1869 | parse_hostnetworkmask(shostnetworkmask, &saddrs, |
| 1870 | &(fw.ipv6.smsk), &nsaddrs); |
| 1871 | |
| 1872 | if (dhostnetworkmask) |
| 1873 | parse_hostnetworkmask(dhostnetworkmask, &daddrs, |
| 1874 | &(fw.ipv6.dmsk), &ndaddrs); |
| 1875 | |
| 1876 | if ((nsaddrs > 1 || ndaddrs > 1) && |
| 1877 | (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP))) |
| 1878 | exit_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
| 1879 | " source or destination IP addresses"); |
| 1880 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1881 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
| 1882 | exit_error(PARAMETER_PROBLEM, "Replacement rule does not " |
| 1883 | "specify a unique address"); |
| 1884 | |
| 1885 | generic_opt_check(command, options); |
| 1886 | |
| 1887 | if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN) |
| 1888 | exit_error(PARAMETER_PROBLEM, |
| 1889 | "chain name `%s' too long (must be under %i chars)", |
| 1890 | chain, IP6T_FUNCTION_MAXNAMELEN); |
| 1891 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1892 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 1893 | if (!*handle) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1894 | *handle = ip6tc_init(*table); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1895 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1896 | /* try to insmod the module if iptc_init failed */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1897 | if (!*handle && load_xtables_ko(modprobe, 0) != -1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1898 | *handle = ip6tc_init(*table); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1899 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1900 | if (!*handle) |
| 1901 | exit_error(VERSION_PROBLEM, |
| 1902 | "can't initialize ip6tables table `%s': %s", |
| 1903 | *table, ip6tc_strerror(errno)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1904 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1905 | if (command == CMD_APPEND |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1906 | || command == CMD_DELETE |
| 1907 | || command == CMD_INSERT |
| 1908 | || command == CMD_REPLACE) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1909 | if (strcmp(chain, "PREROUTING") == 0 |
| 1910 | || strcmp(chain, "INPUT") == 0) { |
| 1911 | /* -o not valid with incoming packets. */ |
| 1912 | if (options & OPT_VIANAMEOUT) |
| 1913 | exit_error(PARAMETER_PROBLEM, |
| 1914 | "Can't use -%c with %s\n", |
| 1915 | opt2char(OPT_VIANAMEOUT), |
| 1916 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1917 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1918 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1919 | if (strcmp(chain, "POSTROUTING") == 0 |
| 1920 | || strcmp(chain, "OUTPUT") == 0) { |
| 1921 | /* -i not valid with outgoing packets */ |
| 1922 | if (options & OPT_VIANAMEIN) |
| 1923 | exit_error(PARAMETER_PROBLEM, |
| 1924 | "Can't use -%c with %s\n", |
| 1925 | opt2char(OPT_VIANAMEIN), |
| 1926 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1927 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (target && ip6tc_is_chain(jumpto, *handle)) { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1930 | fprintf(stderr, |
| 1931 | "Warning: using chain %s, not extension\n", |
| 1932 | jumpto); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1933 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1934 | if (target->t) |
| 1935 | free(target->t); |
| 1936 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1937 | target = NULL; |
| 1938 | } |
| 1939 | |
| 1940 | /* If they didn't specify a target, or it's a chain |
| 1941 | name, use standard. */ |
| 1942 | if (!target |
| 1943 | && (strlen(jumpto) == 0 |
| 1944 | || ip6tc_is_chain(jumpto, *handle))) { |
| 1945 | size_t size; |
| 1946 | |
| 1947 | target = find_target(IP6T_STANDARD_TARGET, |
| 1948 | LOAD_MUST_SUCCEED); |
| 1949 | |
| 1950 | size = sizeof(struct ip6t_entry_target) |
| 1951 | + target->size; |
| 1952 | target->t = fw_calloc(1, size); |
| 1953 | target->t->u.target_size = size; |
| 1954 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1955 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1956 | target->init(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | if (!target) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1960 | /* it is no chain, and we can't load a plugin. |
| 1961 | * We cannot know if the plugin is corrupt, non |
| 1962 | * existant OR if the user just misspelled a |
| 1963 | * chain. */ |
| 1964 | find_target(jumpto, LOAD_MUST_SUCCEED); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1965 | } else { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1966 | e = generate_entry(&fw, matches, target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1967 | free(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | switch (command) { |
| 1972 | case CMD_APPEND: |
| 1973 | ret = append_entry(chain, e, |
| 1974 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1975 | options&OPT_VERBOSE, |
| 1976 | handle); |
| 1977 | break; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1978 | case CMD_DELETE: |
| 1979 | ret = delete_entry(chain, e, |
| 1980 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1981 | options&OPT_VERBOSE, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1982 | handle, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | case CMD_DELETE_NUM: |
| 1985 | ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle); |
| 1986 | break; |
| 1987 | case CMD_REPLACE: |
| 1988 | ret = replace_entry(chain, e, rulenum - 1, |
| 1989 | saddrs, daddrs, options&OPT_VERBOSE, |
| 1990 | handle); |
| 1991 | break; |
| 1992 | case CMD_INSERT: |
| 1993 | ret = insert_entry(chain, e, rulenum - 1, |
| 1994 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1995 | options&OPT_VERBOSE, |
| 1996 | handle); |
| 1997 | break; |
| 1998 | case CMD_LIST: |
| 1999 | ret = list_entries(chain, |
| 2000 | options&OPT_VERBOSE, |
| 2001 | options&OPT_NUMERIC, |
| 2002 | options&OPT_EXPANDED, |
| 2003 | options&OPT_LINENUMBERS, |
| 2004 | handle); |
| 2005 | break; |
| 2006 | case CMD_FLUSH: |
| 2007 | ret = flush_entries(chain, options&OPT_VERBOSE, handle); |
| 2008 | break; |
| 2009 | case CMD_ZERO: |
| 2010 | ret = zero_entries(chain, options&OPT_VERBOSE, handle); |
| 2011 | break; |
| 2012 | case CMD_LIST|CMD_ZERO: |
| 2013 | ret = list_entries(chain, |
| 2014 | options&OPT_VERBOSE, |
| 2015 | options&OPT_NUMERIC, |
| 2016 | options&OPT_EXPANDED, |
| 2017 | options&OPT_LINENUMBERS, |
| 2018 | handle); |
| 2019 | if (ret) |
| 2020 | ret = zero_entries(chain, |
| 2021 | options&OPT_VERBOSE, handle); |
| 2022 | break; |
| 2023 | case CMD_NEW_CHAIN: |
| 2024 | ret = ip6tc_create_chain(chain, handle); |
| 2025 | break; |
| 2026 | case CMD_DELETE_CHAIN: |
| 2027 | ret = delete_chain(chain, options&OPT_VERBOSE, handle); |
| 2028 | break; |
| 2029 | case CMD_RENAME_CHAIN: |
| 2030 | ret = ip6tc_rename_chain(chain, newname, handle); |
| 2031 | break; |
| 2032 | case CMD_SET_POLICY: |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 2033 | ret = ip6tc_set_policy(chain, policy, NULL, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | default: |
| 2036 | /* We should never reach this... */ |
| 2037 | exit_tryhelp(2); |
| 2038 | } |
| 2039 | |
| 2040 | if (verbose > 1) |
| 2041 | dump_entries6(*handle); |
| 2042 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2043 | clear_rule_matches(&matches); |
| 2044 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2045 | if (e != NULL) { |
| 2046 | free(e); |
| 2047 | e = NULL; |
| 2048 | } |
| 2049 | |
| 2050 | for (c = 0; c < nsaddrs; c++) |
| 2051 | free(&saddrs[c]); |
| 2052 | |
| 2053 | for (c = 0; c < ndaddrs; c++) |
| 2054 | free(&daddrs[c]); |
| 2055 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 2056 | free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2057 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2058 | return ret; |
| 2059 | } |