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