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