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