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