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