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