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; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 276 | struct ip6tables_target *t = NULL; |
| 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'. */ |
| 470 | static struct ip6tables_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 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 615 | void register_match6(struct ip6tables_match *me) |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 616 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 617 | me->family = AF_INET6; |
| 618 | xtables_register_match(me); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 621 | void register_target6(struct ip6tables_target *me) |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 622 | { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 623 | me->family = AF_INET6; |
| 624 | xtables_register_target(me); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void |
| 628 | print_num(u_int64_t number, unsigned int format) |
| 629 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 630 | if (format & FMT_KILOMEGAGIGA) { |
| 631 | if (number > 99999) { |
| 632 | number = (number + 500) / 1000; |
| 633 | if (number > 9999) { |
| 634 | number = (number + 500) / 1000; |
| 635 | if (number > 9999) { |
| 636 | number = (number + 500) / 1000; |
| 637 | if (number > 9999) { |
| 638 | number = (number + 500) / 1000; |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 639 | printf(FMT("%4lluT ","%lluT "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 640 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 641 | else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 642 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 643 | else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 644 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 645 | printf(FMT("%4lluK ","%lluK "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 646 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 647 | printf(FMT("%5llu ","%llu "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 648 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 649 | printf(FMT("%8llu ","%llu "), (unsigned long long)number); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 652 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 653 | static void |
| 654 | print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle) |
| 655 | { |
| 656 | struct ip6t_counters counters; |
| 657 | const char *pol = ip6tc_get_policy(chain, &counters, handle); |
| 658 | printf("Chain %s", chain); |
| 659 | if (pol) { |
| 660 | printf(" (policy %s", pol); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 661 | if (!(format & FMT_NOCOUNTS)) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 662 | fputc(' ', stdout); |
| 663 | print_num(counters.pcnt, (format|FMT_NOTABLE)); |
| 664 | fputs("packets, ", stdout); |
| 665 | print_num(counters.bcnt, (format|FMT_NOTABLE)); |
| 666 | fputs("bytes", stdout); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 667 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 668 | printf(")\n"); |
| 669 | } else { |
| 670 | unsigned int refs; |
| 671 | if (!ip6tc_get_references(&refs, chain, handle)) |
| 672 | printf(" (ERROR obtaining refs)\n"); |
| 673 | else |
| 674 | printf(" (%u references)\n", refs); |
| 675 | } |
| 676 | |
| 677 | if (format & FMT_LINENUMBERS) |
| 678 | printf(FMT("%-4s ", "%s "), "num"); |
| 679 | if (!(format & FMT_NOCOUNTS)) { |
| 680 | if (format & FMT_KILOMEGAGIGA) { |
| 681 | printf(FMT("%5s ","%s "), "pkts"); |
| 682 | printf(FMT("%5s ","%s "), "bytes"); |
| 683 | } else { |
| 684 | printf(FMT("%8s ","%s "), "pkts"); |
| 685 | printf(FMT("%10s ","%s "), "bytes"); |
| 686 | } |
| 687 | } |
| 688 | if (!(format & FMT_NOTARGET)) |
| 689 | printf(FMT("%-9s ","%s "), "target"); |
| 690 | fputs(" prot ", stdout); |
| 691 | if (format & FMT_OPTIONS) |
| 692 | fputs("opt", stdout); |
| 693 | if (format & FMT_VIA) { |
| 694 | printf(FMT(" %-6s ","%s "), "in"); |
| 695 | printf(FMT("%-6s ","%s "), "out"); |
| 696 | } |
| 697 | printf(FMT(" %-19s ","%s "), "source"); |
| 698 | printf(FMT(" %-19s "," %s "), "destination"); |
| 699 | printf("\n"); |
| 700 | } |
| 701 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 702 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 703 | static int |
| 704 | print_match(const struct ip6t_entry_match *m, |
| 705 | const struct ip6t_ip6 *ip, |
| 706 | int numeric) |
| 707 | { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 708 | struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 709 | |
| 710 | if (match) { |
| 711 | if (match->print) |
| 712 | match->print(ip, m, numeric); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 713 | else |
| 714 | printf("%s ", match->name); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 715 | } else { |
| 716 | if (m->u.user.name[0]) |
| 717 | printf("UNKNOWN match `%s' ", m->u.user.name); |
| 718 | } |
| 719 | /* Don't stop iterating. */ |
| 720 | return 0; |
| 721 | } |
| 722 | |
Jan Engelhardt | 6cf172e | 2008-03-10 17:48:59 +0100 | [diff] [blame] | 723 | /* e is called `fw' here for historical reasons */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 724 | static void |
| 725 | print_firewall(const struct ip6t_entry *fw, |
| 726 | const char *targname, |
| 727 | unsigned int num, |
| 728 | unsigned int format, |
| 729 | const ip6tc_handle_t handle) |
| 730 | { |
| 731 | struct ip6tables_target *target = NULL; |
| 732 | const struct ip6t_entry_target *t; |
| 733 | u_int8_t flags; |
| 734 | char buf[BUFSIZ]; |
| 735 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 736 | if (!ip6tc_is_chain(targname, handle)) |
| 737 | target = find_target(targname, TRY_LOAD); |
| 738 | else |
| 739 | target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED); |
| 740 | |
| 741 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 742 | flags = fw->ipv6.flags; |
| 743 | |
| 744 | if (format & FMT_LINENUMBERS) |
| 745 | printf(FMT("%-4u ", "%u "), num+1); |
| 746 | |
| 747 | if (!(format & FMT_NOCOUNTS)) { |
| 748 | print_num(fw->counters.pcnt, format); |
| 749 | print_num(fw->counters.bcnt, format); |
| 750 | } |
| 751 | |
| 752 | if (!(format & FMT_NOTARGET)) |
| 753 | printf(FMT("%-9s ", "%s "), targname); |
| 754 | |
| 755 | fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout); |
| 756 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 757 | char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 758 | if (pname) |
| 759 | printf(FMT("%-5s", "%s "), pname); |
| 760 | else |
| 761 | printf(FMT("%-5hu", "%hu "), fw->ipv6.proto); |
| 762 | } |
| 763 | |
| 764 | if (format & FMT_OPTIONS) { |
| 765 | if (format & FMT_NOTABLE) |
| 766 | fputs("opt ", stdout); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 767 | fputc(' ', stdout); /* Invert flag of FRAG */ |
| 768 | fputc(' ', stdout); /* -f */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 769 | fputc(' ', stdout); |
| 770 | } |
| 771 | |
| 772 | if (format & FMT_VIA) { |
| 773 | char iface[IFNAMSIZ+2]; |
| 774 | |
| 775 | if (fw->ipv6.invflags & IP6T_INV_VIA_IN) { |
| 776 | iface[0] = '!'; |
| 777 | iface[1] = '\0'; |
| 778 | } |
| 779 | else iface[0] = '\0'; |
| 780 | |
| 781 | if (fw->ipv6.iniface[0] != '\0') { |
| 782 | strcat(iface, fw->ipv6.iniface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 783 | } |
| 784 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 785 | else strcat(iface, "any"); |
| 786 | printf(FMT(" %-6s ","in %s "), iface); |
| 787 | |
| 788 | if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) { |
| 789 | iface[0] = '!'; |
| 790 | iface[1] = '\0'; |
| 791 | } |
| 792 | else iface[0] = '\0'; |
| 793 | |
| 794 | if (fw->ipv6.outiface[0] != '\0') { |
| 795 | strcat(iface, fw->ipv6.outiface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 796 | } |
| 797 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 798 | else strcat(iface, "any"); |
| 799 | printf(FMT("%-6s ","out %s "), iface); |
| 800 | } |
| 801 | |
| 802 | fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 803 | if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 804 | && !(format & FMT_NUMERIC)) |
| 805 | printf(FMT("%-19s ","%s "), "anywhere"); |
| 806 | else { |
| 807 | if (format & FMT_NUMERIC) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 808 | sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 809 | else |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 810 | sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src)); |
| 811 | strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 812 | printf(FMT("%-19s ","%s "), buf); |
| 813 | } |
| 814 | |
| 815 | fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout); |
| 816 | if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any) |
| 817 | && !(format & FMT_NUMERIC)) |
| 818 | printf(FMT("%-19s","-> %s"), "anywhere"); |
| 819 | else { |
| 820 | if (format & FMT_NUMERIC) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 821 | sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 822 | else |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 823 | sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst)); |
| 824 | strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 825 | printf(FMT("%-19s","-> %s"), buf); |
| 826 | } |
| 827 | |
| 828 | if (format & FMT_NOTABLE) |
| 829 | fputs(" ", stdout); |
| 830 | |
| 831 | IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC); |
| 832 | |
| 833 | if (target) { |
| 834 | if (target->print) |
| 835 | /* Print the target information. */ |
| 836 | target->print(&fw->ipv6, t, format & FMT_NUMERIC); |
| 837 | } else if (t->u.target_size != sizeof(*t)) |
| 838 | printf("[%u bytes of unknown target data] ", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 839 | (unsigned int)(t->u.target_size - sizeof(*t))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 840 | |
| 841 | if (!(format & FMT_NONEWLINE)) |
| 842 | fputc('\n', stdout); |
| 843 | } |
| 844 | |
| 845 | static void |
| 846 | print_firewall_line(const struct ip6t_entry *fw, |
| 847 | const ip6tc_handle_t h) |
| 848 | { |
| 849 | struct ip6t_entry_target *t; |
| 850 | |
| 851 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 852 | print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h); |
| 853 | } |
| 854 | |
| 855 | static int |
| 856 | append_entry(const ip6t_chainlabel chain, |
| 857 | struct ip6t_entry *fw, |
| 858 | unsigned int nsaddrs, |
| 859 | const struct in6_addr saddrs[], |
| 860 | unsigned int ndaddrs, |
| 861 | const struct in6_addr daddrs[], |
| 862 | int verbose, |
| 863 | ip6tc_handle_t *handle) |
| 864 | { |
| 865 | unsigned int i, j; |
| 866 | int ret = 1; |
| 867 | |
| 868 | for (i = 0; i < nsaddrs; i++) { |
| 869 | fw->ipv6.src = saddrs[i]; |
| 870 | for (j = 0; j < ndaddrs; j++) { |
| 871 | fw->ipv6.dst = daddrs[j]; |
| 872 | if (verbose) |
| 873 | print_firewall_line(fw, *handle); |
| 874 | ret &= ip6tc_append_entry(chain, fw, handle); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | static int |
| 882 | replace_entry(const ip6t_chainlabel chain, |
| 883 | struct ip6t_entry *fw, |
| 884 | unsigned int rulenum, |
| 885 | const struct in6_addr *saddr, |
| 886 | const struct in6_addr *daddr, |
| 887 | int verbose, |
| 888 | ip6tc_handle_t *handle) |
| 889 | { |
| 890 | fw->ipv6.src = *saddr; |
| 891 | fw->ipv6.dst = *daddr; |
| 892 | |
| 893 | if (verbose) |
| 894 | print_firewall_line(fw, *handle); |
| 895 | return ip6tc_replace_entry(chain, fw, rulenum, handle); |
| 896 | } |
| 897 | |
| 898 | static int |
| 899 | insert_entry(const ip6t_chainlabel chain, |
| 900 | struct ip6t_entry *fw, |
| 901 | unsigned int rulenum, |
| 902 | unsigned int nsaddrs, |
| 903 | const struct in6_addr saddrs[], |
| 904 | unsigned int ndaddrs, |
| 905 | const struct in6_addr daddrs[], |
| 906 | int verbose, |
| 907 | ip6tc_handle_t *handle) |
| 908 | { |
| 909 | unsigned int i, j; |
| 910 | int ret = 1; |
| 911 | |
| 912 | for (i = 0; i < nsaddrs; i++) { |
| 913 | fw->ipv6.src = saddrs[i]; |
| 914 | for (j = 0; j < ndaddrs; j++) { |
| 915 | fw->ipv6.dst = daddrs[j]; |
| 916 | if (verbose) |
| 917 | print_firewall_line(fw, *handle); |
| 918 | ret &= ip6tc_insert_entry(chain, fw, rulenum, handle); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static unsigned char * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 926 | make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 927 | { |
| 928 | /* Establish mask for comparison */ |
| 929 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 930 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 931 | unsigned char *mask, *mptr; |
| 932 | |
| 933 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 934 | for (matchp = matches; matchp; matchp = matchp->next) |
| 935 | size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 936 | |
| 937 | mask = fw_calloc(1, size |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 938 | + IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 939 | + xtables_targets->size); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 940 | |
| 941 | memset(mask, 0xFF, sizeof(struct ip6t_entry)); |
| 942 | mptr = mask + sizeof(struct ip6t_entry); |
| 943 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 944 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 945 | memset(mptr, 0xFF, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 946 | IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 947 | + matchp->match->userspacesize); |
| 948 | mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 951 | memset(mptr, 0xFF, |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 952 | IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 953 | + xtables_targets->userspacesize); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 954 | |
| 955 | return mask; |
| 956 | } |
| 957 | |
| 958 | static int |
| 959 | delete_entry(const ip6t_chainlabel chain, |
| 960 | struct ip6t_entry *fw, |
| 961 | unsigned int nsaddrs, |
| 962 | const struct in6_addr saddrs[], |
| 963 | unsigned int ndaddrs, |
| 964 | const struct in6_addr daddrs[], |
| 965 | int verbose, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 966 | ip6tc_handle_t *handle, |
| 967 | struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 968 | { |
| 969 | unsigned int i, j; |
| 970 | int ret = 1; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 971 | unsigned char *mask; |
| 972 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 973 | mask = make_delete_mask(fw, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 974 | for (i = 0; i < nsaddrs; i++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 975 | fw->ipv6.src = saddrs[i]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 976 | for (j = 0; j < ndaddrs; j++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 977 | fw->ipv6.dst = daddrs[j]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 978 | if (verbose) |
| 979 | print_firewall_line(fw, *handle); |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 980 | ret &= ip6tc_delete_entry(chain, fw, mask, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 983 | free(mask); |
| 984 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 985 | return ret; |
| 986 | } |
| 987 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 988 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 989 | for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), |
| 990 | int verbose, int builtinstoo, ip6tc_handle_t *handle) |
| 991 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 992 | int ret = 1; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 993 | const char *chain; |
| 994 | char *chains; |
| 995 | unsigned int i, chaincount = 0; |
| 996 | |
| 997 | chain = ip6tc_first_chain(handle); |
| 998 | while (chain) { |
| 999 | chaincount++; |
| 1000 | chain = ip6tc_next_chain(handle); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1001 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1002 | |
| 1003 | chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount); |
| 1004 | i = 0; |
| 1005 | chain = ip6tc_first_chain(handle); |
| 1006 | while (chain) { |
| 1007 | strcpy(chains + i*sizeof(ip6t_chainlabel), chain); |
| 1008 | i++; |
| 1009 | chain = ip6tc_next_chain(handle); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1010 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1011 | |
| 1012 | for (i = 0; i < chaincount; i++) { |
| 1013 | if (!builtinstoo |
| 1014 | && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel), |
Harald Welte | df1c71c | 2004-08-30 16:00:32 +0000 | [diff] [blame] | 1015 | *handle) == 1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1016 | continue; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1017 | ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | free(chains); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1021 | return ret; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1024 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1025 | flush_entries(const ip6t_chainlabel chain, int verbose, |
| 1026 | ip6tc_handle_t *handle) |
| 1027 | { |
| 1028 | if (!chain) |
| 1029 | return for_each_chain(flush_entries, verbose, 1, handle); |
| 1030 | |
| 1031 | if (verbose) |
| 1032 | fprintf(stdout, "Flushing chain `%s'\n", chain); |
| 1033 | return ip6tc_flush_entries(chain, handle); |
| 1034 | } |
| 1035 | |
| 1036 | static int |
| 1037 | zero_entries(const ip6t_chainlabel chain, int verbose, |
| 1038 | ip6tc_handle_t *handle) |
| 1039 | { |
| 1040 | if (!chain) |
| 1041 | return for_each_chain(zero_entries, verbose, 1, handle); |
| 1042 | |
| 1043 | if (verbose) |
| 1044 | fprintf(stdout, "Zeroing chain `%s'\n", chain); |
| 1045 | return ip6tc_zero_entries(chain, handle); |
| 1046 | } |
| 1047 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1048 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1049 | delete_chain(const ip6t_chainlabel chain, int verbose, |
| 1050 | ip6tc_handle_t *handle) |
| 1051 | { |
| 1052 | if (!chain) |
| 1053 | return for_each_chain(delete_chain, verbose, 0, handle); |
| 1054 | |
| 1055 | if (verbose) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1056 | fprintf(stdout, "Deleting chain `%s'\n", chain); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1057 | return ip6tc_delete_chain(chain, handle); |
| 1058 | } |
| 1059 | |
| 1060 | static int |
| 1061 | list_entries(const ip6t_chainlabel chain, int verbose, int numeric, |
| 1062 | int expanded, int linenumbers, ip6tc_handle_t *handle) |
| 1063 | { |
| 1064 | int found = 0; |
| 1065 | unsigned int format; |
| 1066 | const char *this; |
| 1067 | |
| 1068 | format = FMT_OPTIONS; |
| 1069 | if (!verbose) |
| 1070 | format |= FMT_NOCOUNTS; |
| 1071 | else |
| 1072 | format |= FMT_VIA; |
| 1073 | |
| 1074 | if (numeric) |
| 1075 | format |= FMT_NUMERIC; |
| 1076 | |
| 1077 | if (!expanded) |
| 1078 | format |= FMT_KILOMEGAGIGA; |
| 1079 | |
| 1080 | if (linenumbers) |
| 1081 | format |= FMT_LINENUMBERS; |
| 1082 | |
| 1083 | for (this = ip6tc_first_chain(handle); |
| 1084 | this; |
| 1085 | this = ip6tc_next_chain(handle)) { |
| 1086 | const struct ip6t_entry *i; |
| 1087 | unsigned int num; |
| 1088 | |
| 1089 | if (chain && strcmp(chain, this) != 0) |
| 1090 | continue; |
| 1091 | |
| 1092 | if (found) printf("\n"); |
| 1093 | |
| 1094 | print_header(format, this, handle); |
| 1095 | i = ip6tc_first_rule(this, handle); |
| 1096 | |
| 1097 | num = 0; |
| 1098 | while (i) { |
| 1099 | print_firewall(i, |
| 1100 | ip6tc_get_target(i, handle), |
| 1101 | num++, |
| 1102 | format, |
| 1103 | *handle); |
| 1104 | i = ip6tc_next_rule(i, handle); |
| 1105 | } |
| 1106 | found = 1; |
| 1107 | } |
| 1108 | |
| 1109 | errno = ENOENT; |
| 1110 | return found; |
| 1111 | } |
| 1112 | |
| 1113 | static struct ip6t_entry * |
| 1114 | generate_entry(const struct ip6t_entry *fw, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1115 | struct ip6tables_rule_match *matches, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1116 | struct ip6t_entry_target *target) |
| 1117 | { |
| 1118 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1119 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1120 | struct ip6t_entry *e; |
| 1121 | |
| 1122 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1123 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1124 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1125 | |
| 1126 | e = fw_malloc(size + target->u.target_size); |
| 1127 | *e = *fw; |
| 1128 | e->target_offset = size; |
| 1129 | e->next_offset = size + target->u.target_size; |
| 1130 | |
| 1131 | size = 0; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1132 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 1133 | memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size); |
| 1134 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1135 | } |
| 1136 | memcpy(e->elems + size, target, target->u.target_size); |
| 1137 | |
| 1138 | return e; |
| 1139 | } |
| 1140 | |
Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1141 | static void clear_rule_matches(struct ip6tables_rule_match **matches) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1142 | { |
| 1143 | struct ip6tables_rule_match *matchp, *tmp; |
| 1144 | |
| 1145 | for (matchp = *matches; matchp;) { |
| 1146 | tmp = matchp->next; |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1147 | if (matchp->match->m) { |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1148 | free(matchp->match->m); |
Harald Welte | d6bc608 | 2006-02-11 09:34:16 +0000 | [diff] [blame] | 1149 | matchp->match->m = NULL; |
| 1150 | } |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1151 | if (matchp->match == matchp->match->next) { |
| 1152 | free(matchp->match); |
| 1153 | matchp->match = NULL; |
| 1154 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1155 | free(matchp); |
| 1156 | matchp = tmp; |
| 1157 | } |
| 1158 | |
| 1159 | *matches = NULL; |
| 1160 | } |
| 1161 | |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1162 | static void set_revision(char *name, u_int8_t revision) |
| 1163 | { |
| 1164 | /* Old kernel sources don't have ".revision" field, |
| 1165 | but we stole a byte from name. */ |
| 1166 | name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0'; |
| 1167 | name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision; |
| 1168 | } |
| 1169 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1170 | int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle) |
| 1171 | { |
| 1172 | struct ip6t_entry fw, *e = NULL; |
| 1173 | int invert = 0; |
| 1174 | unsigned int nsaddrs = 0, ndaddrs = 0; |
| 1175 | struct in6_addr *saddrs = NULL, *daddrs = NULL; |
| 1176 | |
| 1177 | int c, verbose = 0; |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 1178 | unsigned i; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1179 | const char *chain = NULL; |
| 1180 | const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL; |
| 1181 | const char *policy = NULL, *newname = NULL; |
| 1182 | unsigned int rulenum = 0, options = 0, command = 0; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1183 | const char *pcnt = NULL, *bcnt = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1184 | int ret = 1; |
| 1185 | struct ip6tables_match *m; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1186 | struct ip6tables_rule_match *matches = NULL; |
| 1187 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1188 | struct ip6tables_target *target = NULL; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1189 | struct ip6tables_target *t; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1190 | const char *jumpto = ""; |
| 1191 | char *protocol = NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1192 | int proto_used = 0; |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1193 | unsigned long long cnt; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | memset(&fw, 0, sizeof(fw)); |
| 1196 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1197 | /* 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] | 1198 | * a second time */ |
| 1199 | optind = 0; |
| 1200 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1201 | /* clear mflags in case do_command gets called a second time |
| 1202 | * (we clear the global list of all matches for security)*/ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1203 | for (m = xtables_matches; m; m = m->next) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1204 | m->mflags = 0; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1205 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 1206 | for (t = xtables_targets; t; t = t->next) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1207 | t->tflags = 0; |
| 1208 | t->used = 0; |
| 1209 | } |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1210 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1211 | /* Suppress error messages: we may add new options if we |
| 1212 | demand-load a protocol. */ |
| 1213 | opterr = 0; |
| 1214 | |
| 1215 | while ((c = getopt_long(argc, argv, |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1216 | "-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] | 1217 | opts, NULL)) != -1) { |
| 1218 | switch (c) { |
| 1219 | /* |
| 1220 | * Command selection |
| 1221 | */ |
| 1222 | case 'A': |
| 1223 | add_command(&command, CMD_APPEND, CMD_NONE, |
| 1224 | invert); |
| 1225 | chain = optarg; |
| 1226 | break; |
| 1227 | |
| 1228 | case 'D': |
| 1229 | add_command(&command, CMD_DELETE, CMD_NONE, |
| 1230 | invert); |
| 1231 | chain = optarg; |
| 1232 | if (optind < argc && argv[optind][0] != '-' |
| 1233 | && argv[optind][0] != '!') { |
| 1234 | rulenum = parse_rulenumber(argv[optind++]); |
| 1235 | command = CMD_DELETE_NUM; |
| 1236 | } |
| 1237 | break; |
| 1238 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1239 | case 'R': |
| 1240 | add_command(&command, CMD_REPLACE, CMD_NONE, |
| 1241 | invert); |
| 1242 | chain = optarg; |
| 1243 | if (optind < argc && argv[optind][0] != '-' |
| 1244 | && argv[optind][0] != '!') |
| 1245 | rulenum = parse_rulenumber(argv[optind++]); |
| 1246 | else |
| 1247 | exit_error(PARAMETER_PROBLEM, |
| 1248 | "-%c requires a rule number", |
| 1249 | cmd2char(CMD_REPLACE)); |
| 1250 | break; |
| 1251 | |
| 1252 | case 'I': |
| 1253 | add_command(&command, CMD_INSERT, CMD_NONE, |
| 1254 | invert); |
| 1255 | chain = optarg; |
| 1256 | if (optind < argc && argv[optind][0] != '-' |
| 1257 | && argv[optind][0] != '!') |
| 1258 | rulenum = parse_rulenumber(argv[optind++]); |
| 1259 | else rulenum = 1; |
| 1260 | break; |
| 1261 | |
| 1262 | case 'L': |
| 1263 | add_command(&command, CMD_LIST, CMD_ZERO, |
| 1264 | invert); |
| 1265 | if (optarg) chain = optarg; |
| 1266 | else if (optind < argc && argv[optind][0] != '-' |
| 1267 | && argv[optind][0] != '!') |
| 1268 | chain = argv[optind++]; |
| 1269 | break; |
| 1270 | |
| 1271 | case 'F': |
| 1272 | add_command(&command, CMD_FLUSH, CMD_NONE, |
| 1273 | invert); |
| 1274 | if (optarg) chain = optarg; |
| 1275 | else if (optind < argc && argv[optind][0] != '-' |
| 1276 | && argv[optind][0] != '!') |
| 1277 | chain = argv[optind++]; |
| 1278 | break; |
| 1279 | |
| 1280 | case 'Z': |
| 1281 | add_command(&command, CMD_ZERO, CMD_LIST, |
| 1282 | invert); |
| 1283 | if (optarg) chain = optarg; |
| 1284 | else if (optind < argc && argv[optind][0] != '-' |
| 1285 | && argv[optind][0] != '!') |
| 1286 | chain = argv[optind++]; |
| 1287 | break; |
| 1288 | |
| 1289 | case 'N': |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1290 | if (optarg && (*optarg == '-' || *optarg == '!')) |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1291 | exit_error(PARAMETER_PROBLEM, |
| 1292 | "chain name not allowed to start " |
Yasuyuki KOZAKAI | 8d8c8ea | 2005-06-13 01:06:10 +0000 | [diff] [blame] | 1293 | "with `%c'\n", *optarg); |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1294 | if (find_target(optarg, TRY_LOAD)) |
| 1295 | exit_error(PARAMETER_PROBLEM, |
| 1296 | "chain name may not clash " |
| 1297 | "with target name\n"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1298 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
| 1299 | invert); |
| 1300 | chain = optarg; |
| 1301 | break; |
| 1302 | |
| 1303 | case 'X': |
| 1304 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
| 1305 | invert); |
| 1306 | if (optarg) chain = optarg; |
| 1307 | else if (optind < argc && argv[optind][0] != '-' |
| 1308 | && argv[optind][0] != '!') |
| 1309 | chain = argv[optind++]; |
| 1310 | break; |
| 1311 | |
| 1312 | case 'E': |
| 1313 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
| 1314 | invert); |
| 1315 | chain = optarg; |
| 1316 | if (optind < argc && argv[optind][0] != '-' |
| 1317 | && argv[optind][0] != '!') |
| 1318 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1319 | else |
| 1320 | exit_error(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 950ddc6 | 2007-06-12 01:36:26 +0000 | [diff] [blame] | 1321 | "-%c requires old-chain-name and " |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1322 | "new-chain-name", |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1323 | cmd2char(CMD_RENAME_CHAIN)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | |
| 1326 | case 'P': |
| 1327 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
| 1328 | invert); |
| 1329 | chain = optarg; |
| 1330 | if (optind < argc && argv[optind][0] != '-' |
| 1331 | && argv[optind][0] != '!') |
| 1332 | policy = argv[optind++]; |
| 1333 | else |
| 1334 | exit_error(PARAMETER_PROBLEM, |
| 1335 | "-%c requires a chain and a policy", |
| 1336 | cmd2char(CMD_SET_POLICY)); |
| 1337 | break; |
| 1338 | |
| 1339 | case 'h': |
| 1340 | if (!optarg) |
| 1341 | optarg = argv[optind]; |
| 1342 | |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1343 | /* ip6tables -p icmp -h */ |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1344 | if (!matches && protocol) |
| 1345 | find_match(protocol, TRY_LOAD, &matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1346 | |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1347 | exit_printhelp(matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * Option selection |
| 1351 | */ |
| 1352 | case 'p': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1353 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1354 | set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags, |
| 1355 | invert); |
| 1356 | |
| 1357 | /* Canonicalize into lower case */ |
| 1358 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 1359 | *protocol = tolower(*protocol); |
| 1360 | |
| 1361 | protocol = argv[optind-1]; |
| 1362 | fw.ipv6.proto = parse_protocol(protocol); |
| 1363 | fw.ipv6.flags |= IP6T_F_PROTO; |
| 1364 | |
| 1365 | if (fw.ipv6.proto == 0 |
| 1366 | && (fw.ipv6.invflags & IP6T_INV_PROTO)) |
| 1367 | exit_error(PARAMETER_PROBLEM, |
| 1368 | "rule would never match protocol"); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1369 | |
Yasuyuki KOZAKAI | f69e30c | 2007-06-11 20:17:34 +0000 | [diff] [blame] | 1370 | if (is_exthdr(fw.ipv6.proto) |
| 1371 | && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0) |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1372 | fprintf(stderr, |
| 1373 | "Warning: never matched protocol: %s. " |
| 1374 | "use extension match instead.\n", |
| 1375 | protocol); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | |
| 1378 | case 's': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1379 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1380 | set_option(&options, OPT_SOURCE, &fw.ipv6.invflags, |
| 1381 | invert); |
| 1382 | shostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1383 | break; |
| 1384 | |
| 1385 | case 'd': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1386 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1387 | set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags, |
| 1388 | invert); |
| 1389 | dhostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | case 'j': |
| 1393 | set_option(&options, OPT_JUMP, &fw.ipv6.invflags, |
| 1394 | invert); |
| 1395 | jumpto = parse_target(optarg); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1396 | /* TRY_LOAD (may be chain name) */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1397 | target = find_target(jumpto, TRY_LOAD); |
| 1398 | |
| 1399 | if (target) { |
| 1400 | size_t size; |
| 1401 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1402 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
| 1403 | + target->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1404 | |
| 1405 | target->t = fw_calloc(1, size); |
| 1406 | target->t->u.target_size = size; |
| 1407 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1408 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1409 | target->init(target->t); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1410 | opts = merge_options(opts, target->extra_opts, &target->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1411 | } |
| 1412 | break; |
| 1413 | |
| 1414 | |
| 1415 | case 'i': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1416 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1417 | set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags, |
| 1418 | invert); |
| 1419 | parse_interface(argv[optind-1], |
| 1420 | fw.ipv6.iniface, |
| 1421 | fw.ipv6.iniface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | |
| 1424 | case 'o': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1425 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1426 | set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags, |
| 1427 | invert); |
| 1428 | parse_interface(argv[optind-1], |
| 1429 | fw.ipv6.outiface, |
| 1430 | fw.ipv6.outiface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | |
| 1433 | case 'v': |
| 1434 | if (!verbose) |
| 1435 | set_option(&options, OPT_VERBOSE, |
| 1436 | &fw.ipv6.invflags, invert); |
| 1437 | verbose++; |
| 1438 | break; |
| 1439 | |
| 1440 | case 'm': { |
| 1441 | size_t size; |
| 1442 | |
| 1443 | if (invert) |
| 1444 | exit_error(PARAMETER_PROBLEM, |
| 1445 | "unexpected ! flag before --match"); |
| 1446 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1447 | m = find_match(optarg, LOAD_MUST_SUCCEED, &matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1448 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1449 | + m->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1450 | m->m = fw_calloc(1, size); |
| 1451 | m->m->u.match_size = size; |
| 1452 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1453 | set_revision(m->m->u.user.name, m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1454 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1455 | m->init(m->m); |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1456 | if (m != m->next) |
| 1457 | /* Merge options for non-cloned matches */ |
| 1458 | opts = merge_options(opts, m->extra_opts, &m->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1459 | } |
| 1460 | break; |
| 1461 | |
| 1462 | case 'n': |
| 1463 | set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags, |
| 1464 | invert); |
| 1465 | break; |
| 1466 | |
| 1467 | case 't': |
| 1468 | if (invert) |
| 1469 | exit_error(PARAMETER_PROBLEM, |
| 1470 | "unexpected ! flag before --table"); |
| 1471 | *table = argv[optind-1]; |
| 1472 | break; |
| 1473 | |
| 1474 | case 'x': |
| 1475 | set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags, |
| 1476 | invert); |
| 1477 | break; |
| 1478 | |
| 1479 | case 'V': |
| 1480 | if (invert) |
| 1481 | printf("Not %s ;-)\n", program_version); |
| 1482 | else |
| 1483 | printf("%s v%s\n", |
| 1484 | program_name, program_version); |
| 1485 | exit(0); |
| 1486 | |
| 1487 | case '0': |
| 1488 | set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags, |
| 1489 | invert); |
| 1490 | break; |
| 1491 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1492 | case 'M': |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1493 | modprobe_program = optarg; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | |
| 1496 | case 'c': |
| 1497 | |
| 1498 | set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags, |
| 1499 | invert); |
| 1500 | pcnt = optarg; |
| 1501 | if (optind < argc && argv[optind][0] != '-' |
| 1502 | && argv[optind][0] != '!') |
| 1503 | bcnt = argv[optind++]; |
| 1504 | else |
| 1505 | exit_error(PARAMETER_PROBLEM, |
| 1506 | "-%c requires packet and byte counter", |
| 1507 | opt2char(OPT_COUNTERS)); |
| 1508 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1509 | if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1510 | exit_error(PARAMETER_PROBLEM, |
| 1511 | "-%c packet counter not numeric", |
| 1512 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1513 | fw.counters.pcnt = cnt; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1514 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1515 | if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1516 | exit_error(PARAMETER_PROBLEM, |
| 1517 | "-%c byte counter not numeric", |
| 1518 | opt2char(OPT_COUNTERS)); |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 1519 | fw.counters.bcnt = cnt; |
| 1520 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | |
| 1523 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1524 | case 1: /* non option */ |
| 1525 | if (optarg[0] == '!' && optarg[1] == '\0') { |
| 1526 | if (invert) |
| 1527 | exit_error(PARAMETER_PROBLEM, |
| 1528 | "multiple consecutive ! not" |
| 1529 | " allowed"); |
| 1530 | invert = TRUE; |
| 1531 | optarg[0] = '\0'; |
| 1532 | continue; |
| 1533 | } |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1534 | fprintf(stderr, "Bad argument `%s'\n", optarg); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1535 | exit_tryhelp(2); |
| 1536 | |
| 1537 | default: |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1538 | if (!target |
| 1539 | || !(target->parse(c - target->option_offset, |
| 1540 | argv, invert, |
| 1541 | &target->tflags, |
| 1542 | &fw, &target->t))) { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1543 | for (matchp = matches; matchp; matchp = matchp->next) { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1544 | if (matchp->completed) |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1545 | continue; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1546 | if (matchp->match->parse(c - matchp->match->option_offset, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1547 | argv, invert, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1548 | &matchp->match->mflags, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1549 | &fw, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1550 | &matchp->match->m)) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1553 | m = matchp ? matchp->match : NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* If you listen carefully, you can |
| 1556 | actually hear this code suck. */ |
| 1557 | |
| 1558 | /* some explanations (after four different bugs |
Joszef Kadlecsik | a258ad7 | 2006-03-03 09:36:50 +0000 | [diff] [blame] | 1559 | * in 3 different releases): If we encounter a |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1560 | * parameter, that has not been parsed yet, |
| 1561 | * it's not an option of an explicitly loaded |
| 1562 | * match or a target. However, we support |
| 1563 | * implicit loading of the protocol match |
| 1564 | * extension. '-p tcp' means 'l4 proto 6' and |
| 1565 | * at the same time 'load tcp protocol match on |
| 1566 | * demand if we specify --dport'. |
| 1567 | * |
| 1568 | * To make this work, we need to make sure: |
| 1569 | * - the parameter has not been parsed by |
| 1570 | * a match (m above) |
| 1571 | * - a protocol has been specified |
| 1572 | * - the protocol extension has not been |
| 1573 | * loaded yet, or is loaded and unused |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1574 | * [think of ip6tables-restore!] |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1575 | * - the protocol extension can be successively |
| 1576 | * loaded |
| 1577 | */ |
| 1578 | if (m == NULL |
| 1579 | && protocol |
| 1580 | && (!find_proto(protocol, DONT_LOAD, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1581 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1582 | || (find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1583 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1584 | && (proto_used == 0)) |
| 1585 | ) |
| 1586 | && (m = find_proto(protocol, TRY_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1587 | options&OPT_NUMERIC, &matches))) { |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1588 | /* Try loading protocol */ |
| 1589 | size_t size; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 1590 | |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1591 | proto_used = 1; |
| 1592 | |
| 1593 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1594 | + m->size; |
| 1595 | |
| 1596 | m->m = fw_calloc(1, size); |
| 1597 | m->m->u.match_size = size; |
| 1598 | strcpy(m->m->u.user.name, m->name); |
Rémi Denis-Courmont | 0665217 | 2006-10-20 12:24:34 +0000 | [diff] [blame] | 1599 | set_revision(m->m->u.user.name, |
| 1600 | m->revision); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1601 | if (m->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1602 | m->init(m->m); |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1603 | |
| 1604 | opts = merge_options(opts, |
| 1605 | m->extra_opts, &m->option_offset); |
| 1606 | |
| 1607 | optind--; |
| 1608 | continue; |
| 1609 | } |
| 1610 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1611 | if (!m) |
| 1612 | exit_error(PARAMETER_PROBLEM, |
| 1613 | "Unknown arg `%s'", |
| 1614 | argv[optind-1]); |
| 1615 | } |
| 1616 | } |
| 1617 | invert = FALSE; |
| 1618 | } |
| 1619 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1620 | for (matchp = matches; matchp; matchp = matchp->next) |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1621 | if (matchp->match->final_check != NULL) |
| 1622 | matchp->match->final_check(matchp->match->mflags); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1623 | |
Jan Engelhardt | 830132a | 2007-10-04 16:24:50 +0000 | [diff] [blame] | 1624 | if (target != NULL && target->final_check != NULL) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1625 | target->final_check(target->tflags); |
| 1626 | |
| 1627 | /* Fix me: must put inverse options checking here --MN */ |
| 1628 | |
| 1629 | if (optind < argc) |
| 1630 | exit_error(PARAMETER_PROBLEM, |
| 1631 | "unknown arguments found on commandline"); |
| 1632 | if (!command) |
| 1633 | exit_error(PARAMETER_PROBLEM, "no command specified"); |
| 1634 | if (invert) |
| 1635 | exit_error(PARAMETER_PROBLEM, |
| 1636 | "nothing appropriate following !"); |
| 1637 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1638 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1639 | if (!(options & OPT_DESTINATION)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1640 | dhostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1641 | if (!(options & OPT_SOURCE)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1642 | shostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | if (shostnetworkmask) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1646 | ip6parse_hostnetworkmask(shostnetworkmask, &saddrs, |
| 1647 | &fw.ipv6.smsk, &nsaddrs); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1648 | |
| 1649 | if (dhostnetworkmask) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1650 | ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs, |
| 1651 | &fw.ipv6.dmsk, &ndaddrs); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1652 | |
| 1653 | if ((nsaddrs > 1 || ndaddrs > 1) && |
| 1654 | (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP))) |
| 1655 | exit_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
| 1656 | " source or destination IP addresses"); |
| 1657 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1658 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
| 1659 | exit_error(PARAMETER_PROBLEM, "Replacement rule does not " |
| 1660 | "specify a unique address"); |
| 1661 | |
| 1662 | generic_opt_check(command, options); |
| 1663 | |
| 1664 | if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN) |
| 1665 | exit_error(PARAMETER_PROBLEM, |
| 1666 | "chain name `%s' too long (must be under %i chars)", |
| 1667 | chain, IP6T_FUNCTION_MAXNAMELEN); |
| 1668 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1669 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 1670 | if (!*handle) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1671 | *handle = ip6tc_init(*table); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1672 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1673 | /* try to insmod the module if iptc_init failed */ |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1674 | if (!*handle && load_xtables_ko(modprobe_program, 0) != -1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1675 | *handle = ip6tc_init(*table); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1676 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1677 | if (!*handle) |
| 1678 | exit_error(VERSION_PROBLEM, |
| 1679 | "can't initialize ip6tables table `%s': %s", |
| 1680 | *table, ip6tc_strerror(errno)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1681 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1682 | if (command == CMD_APPEND |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1683 | || command == CMD_DELETE |
| 1684 | || command == CMD_INSERT |
| 1685 | || command == CMD_REPLACE) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1686 | if (strcmp(chain, "PREROUTING") == 0 |
| 1687 | || strcmp(chain, "INPUT") == 0) { |
| 1688 | /* -o not valid with incoming packets. */ |
| 1689 | if (options & OPT_VIANAMEOUT) |
| 1690 | exit_error(PARAMETER_PROBLEM, |
| 1691 | "Can't use -%c with %s\n", |
| 1692 | opt2char(OPT_VIANAMEOUT), |
| 1693 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1694 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1695 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1696 | if (strcmp(chain, "POSTROUTING") == 0 |
| 1697 | || strcmp(chain, "OUTPUT") == 0) { |
| 1698 | /* -i not valid with outgoing packets */ |
| 1699 | if (options & OPT_VIANAMEIN) |
| 1700 | exit_error(PARAMETER_PROBLEM, |
| 1701 | "Can't use -%c with %s\n", |
| 1702 | opt2char(OPT_VIANAMEIN), |
| 1703 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1704 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1705 | |
| 1706 | if (target && ip6tc_is_chain(jumpto, *handle)) { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 1707 | fprintf(stderr, |
| 1708 | "Warning: using chain %s, not extension\n", |
| 1709 | jumpto); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1710 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1711 | if (target->t) |
| 1712 | free(target->t); |
| 1713 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1714 | target = NULL; |
| 1715 | } |
| 1716 | |
| 1717 | /* If they didn't specify a target, or it's a chain |
| 1718 | name, use standard. */ |
| 1719 | if (!target |
| 1720 | && (strlen(jumpto) == 0 |
| 1721 | || ip6tc_is_chain(jumpto, *handle))) { |
| 1722 | size_t size; |
| 1723 | |
| 1724 | target = find_target(IP6T_STANDARD_TARGET, |
| 1725 | LOAD_MUST_SUCCEED); |
| 1726 | |
| 1727 | size = sizeof(struct ip6t_entry_target) |
| 1728 | + target->size; |
| 1729 | target->t = fw_calloc(1, size); |
| 1730 | target->t->u.target_size = size; |
| 1731 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1732 | if (target->init != NULL) |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 1733 | target->init(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | if (!target) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1737 | /* it is no chain, and we can't load a plugin. |
| 1738 | * We cannot know if the plugin is corrupt, non |
| 1739 | * existant OR if the user just misspelled a |
| 1740 | * chain. */ |
| 1741 | find_target(jumpto, LOAD_MUST_SUCCEED); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1742 | } else { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1743 | e = generate_entry(&fw, matches, target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1744 | free(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | switch (command) { |
| 1749 | case CMD_APPEND: |
| 1750 | ret = append_entry(chain, e, |
| 1751 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1752 | options&OPT_VERBOSE, |
| 1753 | handle); |
| 1754 | break; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1755 | case CMD_DELETE: |
| 1756 | ret = delete_entry(chain, e, |
| 1757 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1758 | options&OPT_VERBOSE, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1759 | handle, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | case CMD_DELETE_NUM: |
| 1762 | ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle); |
| 1763 | break; |
| 1764 | case CMD_REPLACE: |
| 1765 | ret = replace_entry(chain, e, rulenum - 1, |
| 1766 | saddrs, daddrs, options&OPT_VERBOSE, |
| 1767 | handle); |
| 1768 | break; |
| 1769 | case CMD_INSERT: |
| 1770 | ret = insert_entry(chain, e, rulenum - 1, |
| 1771 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 1772 | options&OPT_VERBOSE, |
| 1773 | handle); |
| 1774 | break; |
| 1775 | case CMD_LIST: |
| 1776 | ret = list_entries(chain, |
| 1777 | options&OPT_VERBOSE, |
| 1778 | options&OPT_NUMERIC, |
| 1779 | options&OPT_EXPANDED, |
| 1780 | options&OPT_LINENUMBERS, |
| 1781 | handle); |
| 1782 | break; |
| 1783 | case CMD_FLUSH: |
| 1784 | ret = flush_entries(chain, options&OPT_VERBOSE, handle); |
| 1785 | break; |
| 1786 | case CMD_ZERO: |
| 1787 | ret = zero_entries(chain, options&OPT_VERBOSE, handle); |
| 1788 | break; |
| 1789 | case CMD_LIST|CMD_ZERO: |
| 1790 | ret = list_entries(chain, |
| 1791 | options&OPT_VERBOSE, |
| 1792 | options&OPT_NUMERIC, |
| 1793 | options&OPT_EXPANDED, |
| 1794 | options&OPT_LINENUMBERS, |
| 1795 | handle); |
| 1796 | if (ret) |
| 1797 | ret = zero_entries(chain, |
| 1798 | options&OPT_VERBOSE, handle); |
| 1799 | break; |
| 1800 | case CMD_NEW_CHAIN: |
| 1801 | ret = ip6tc_create_chain(chain, handle); |
| 1802 | break; |
| 1803 | case CMD_DELETE_CHAIN: |
| 1804 | ret = delete_chain(chain, options&OPT_VERBOSE, handle); |
| 1805 | break; |
| 1806 | case CMD_RENAME_CHAIN: |
| 1807 | ret = ip6tc_rename_chain(chain, newname, handle); |
| 1808 | break; |
| 1809 | case CMD_SET_POLICY: |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 1810 | ret = ip6tc_set_policy(chain, policy, NULL, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | default: |
| 1813 | /* We should never reach this... */ |
| 1814 | exit_tryhelp(2); |
| 1815 | } |
| 1816 | |
| 1817 | if (verbose > 1) |
| 1818 | dump_entries6(*handle); |
| 1819 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1820 | clear_rule_matches(&matches); |
| 1821 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1822 | if (e != NULL) { |
| 1823 | free(e); |
| 1824 | e = NULL; |
| 1825 | } |
| 1826 | |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 1827 | for (i = 0; i < nsaddrs; i++) |
| 1828 | free(&saddrs[i]); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1829 | |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 1830 | for (i = 0; i < ndaddrs; i++) |
| 1831 | free(&daddrs[i]); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1832 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame] | 1833 | free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1834 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1835 | return ret; |
| 1836 | } |