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