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> |
| 34 | #include <dlfcn.h> |
| 35 | #include <ctype.h> |
| 36 | #include <stdarg.h> |
| 37 | #include <limits.h> |
| 38 | #include <ip6tables.h> |
| 39 | #include <arpa/inet.h> |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 45 | |
| 46 | #ifndef TRUE |
| 47 | #define TRUE 1 |
| 48 | #endif |
| 49 | #ifndef FALSE |
| 50 | #define FALSE 0 |
| 51 | #endif |
| 52 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 53 | #ifndef PROC_SYS_MODPROBE |
| 54 | #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe" |
| 55 | #endif |
| 56 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 57 | #define FMT_NUMERIC 0x0001 |
| 58 | #define FMT_NOCOUNTS 0x0002 |
| 59 | #define FMT_KILOMEGAGIGA 0x0004 |
| 60 | #define FMT_OPTIONS 0x0008 |
| 61 | #define FMT_NOTABLE 0x0010 |
| 62 | #define FMT_NOTARGET 0x0020 |
| 63 | #define FMT_VIA 0x0040 |
| 64 | #define FMT_NONEWLINE 0x0080 |
| 65 | #define FMT_LINENUMBERS 0x0100 |
| 66 | |
| 67 | #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \ |
| 68 | | FMT_NUMERIC | FMT_NOTABLE) |
| 69 | #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab)) |
| 70 | |
| 71 | |
| 72 | #define CMD_NONE 0x0000U |
| 73 | #define CMD_INSERT 0x0001U |
| 74 | #define CMD_DELETE 0x0002U |
| 75 | #define CMD_DELETE_NUM 0x0004U |
| 76 | #define CMD_REPLACE 0x0008U |
| 77 | #define CMD_APPEND 0x0010U |
| 78 | #define CMD_LIST 0x0020U |
| 79 | #define CMD_FLUSH 0x0040U |
| 80 | #define CMD_ZERO 0x0080U |
| 81 | #define CMD_NEW_CHAIN 0x0100U |
| 82 | #define CMD_DELETE_CHAIN 0x0200U |
| 83 | #define CMD_SET_POLICY 0x0400U |
| 84 | #define CMD_CHECK 0x0800U |
| 85 | #define CMD_RENAME_CHAIN 0x1000U |
| 86 | #define NUMBER_OF_CMD 13 |
| 87 | static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z', |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 88 | 'N', 'X', 'P', 'E' }; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 89 | |
| 90 | #define OPTION_OFFSET 256 |
| 91 | |
| 92 | #define OPT_NONE 0x00000U |
| 93 | #define OPT_NUMERIC 0x00001U |
| 94 | #define OPT_SOURCE 0x00002U |
| 95 | #define OPT_DESTINATION 0x00004U |
| 96 | #define OPT_PROTOCOL 0x00008U |
| 97 | #define OPT_JUMP 0x00010U |
| 98 | #define OPT_VERBOSE 0x00020U |
| 99 | #define OPT_EXPANDED 0x00040U |
| 100 | #define OPT_VIANAMEIN 0x00080U |
| 101 | #define OPT_VIANAMEOUT 0x00100U |
| 102 | #define OPT_LINENUMBERS 0x00200U |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 103 | #define OPT_COUNTERS 0x00400U |
| 104 | #define NUMBER_OF_OPT 11 |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 105 | static const char optflags[NUMBER_OF_OPT] |
Jonas Berlin | 4a06cf0 | 2005-04-01 06:38:25 +0000 | [diff] [blame] | 106 | = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'}; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 107 | |
| 108 | static struct option original_opts[] = { |
| 109 | { "append", 1, 0, 'A' }, |
| 110 | { "delete", 1, 0, 'D' }, |
| 111 | { "insert", 1, 0, 'I' }, |
| 112 | { "replace", 1, 0, 'R' }, |
| 113 | { "list", 2, 0, 'L' }, |
| 114 | { "flush", 2, 0, 'F' }, |
| 115 | { "zero", 2, 0, 'Z' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 116 | { "new-chain", 1, 0, 'N' }, |
| 117 | { "delete-chain", 2, 0, 'X' }, |
Harald Welte | 68ec9c7 | 2002-11-02 14:48:17 +0000 | [diff] [blame] | 118 | { "rename-chain", 1, 0, 'E' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 119 | { "policy", 1, 0, 'P' }, |
| 120 | { "source", 1, 0, 's' }, |
| 121 | { "destination", 1, 0, 'd' }, |
| 122 | { "src", 1, 0, 's' }, /* synonym */ |
| 123 | { "dst", 1, 0, 'd' }, /* synonym */ |
| 124 | { "protocol", 1, 0, 'p' }, |
| 125 | { "in-interface", 1, 0, 'i' }, |
| 126 | { "jump", 1, 0, 'j' }, |
| 127 | { "table", 1, 0, 't' }, |
| 128 | { "match", 1, 0, 'm' }, |
| 129 | { "numeric", 0, 0, 'n' }, |
| 130 | { "out-interface", 1, 0, 'o' }, |
| 131 | { "verbose", 0, 0, 'v' }, |
| 132 | { "exact", 0, 0, 'x' }, |
| 133 | { "version", 0, 0, 'V' }, |
| 134 | { "help", 2, 0, 'h' }, |
| 135 | { "line-numbers", 0, 0, '0' }, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 136 | { "modprobe", 1, 0, 'M' }, |
| 137 | { "set-counters", 1, 0, 'c' }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 138 | { 0 } |
| 139 | }; |
| 140 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 141 | /* we need this for ip6tables-restore. ip6tables-restore.c sets line to the |
| 142 | * current line of the input file, in order to give a more precise error |
| 143 | * message. ip6tables itself doesn't need this, so it is initialized to the |
| 144 | * magic number of -1 */ |
| 145 | int line = -1; |
| 146 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 147 | static struct option *opts = original_opts; |
| 148 | static unsigned int global_option_offset = 0; |
| 149 | |
| 150 | /* Table of legal combinations of commands and options. If any of the |
| 151 | * given commands make an option legal, that option is legal (applies to |
| 152 | * CMD_LIST and CMD_ZERO only). |
| 153 | * Key: |
| 154 | * + compulsory |
| 155 | * x illegal |
| 156 | * optional |
| 157 | */ |
| 158 | |
| 159 | static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = |
| 160 | /* Well, it's better than "Re: Linux vs FreeBSD" */ |
| 161 | { |
| 162 | /* -n -s -d -p -j -v -x -i -o --line */ |
| 163 | /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'}, |
| 164 | /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'}, |
| 165 | /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'}, |
| 166 | /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'}, |
| 167 | /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'}, |
| 168 | /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '}, |
| 169 | /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'}, |
| 170 | /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'}, |
| 171 | /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'}, |
| 172 | /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'}, |
| 173 | /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'}, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 174 | /*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'}, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 175 | /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'} |
| 176 | }; |
| 177 | |
| 178 | static int inverse_for_options[NUMBER_OF_OPT] = |
| 179 | { |
| 180 | /* -n */ 0, |
| 181 | /* -s */ IP6T_INV_SRCIP, |
| 182 | /* -d */ IP6T_INV_DSTIP, |
| 183 | /* -p */ IP6T_INV_PROTO, |
| 184 | /* -j */ 0, |
| 185 | /* -v */ 0, |
| 186 | /* -x */ 0, |
| 187 | /* -i */ IP6T_INV_VIA_IN, |
| 188 | /* -o */ IP6T_INV_VIA_OUT, |
| 189 | /*--line*/ 0 |
| 190 | }; |
| 191 | |
| 192 | const char *program_version; |
| 193 | const char *program_name; |
Martin Josefsson | 357d59d | 2004-12-27 19:49:28 +0000 | [diff] [blame] | 194 | char *lib_dir; |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 195 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 196 | /* Keeping track of external matches and targets: linked lists. */ |
| 197 | struct ip6tables_match *ip6tables_matches = NULL; |
| 198 | struct ip6tables_target *ip6tables_targets = NULL; |
| 199 | |
| 200 | /* Extra debugging from libiptc */ |
| 201 | extern void dump_entries6(const ip6tc_handle_t handle); |
| 202 | |
| 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 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 210 | /* Primitive headers... */ |
| 211 | /* defined in netinet/in.h */ |
| 212 | #if 0 |
| 213 | #ifndef IPPROTO_ESP |
| 214 | #define IPPROTO_ESP 50 |
| 215 | #endif |
| 216 | #ifndef IPPROTO_AH |
| 217 | #define IPPROTO_AH 51 |
| 218 | #endif |
| 219 | #endif |
| 220 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 221 | static const struct pprot chain_protos[] = { |
| 222 | { "tcp", IPPROTO_TCP }, |
| 223 | { "udp", IPPROTO_UDP }, |
Harald Welte | de8e5fa | 2000-11-13 14:34:34 +0000 | [diff] [blame] | 224 | { "icmpv6", IPPROTO_ICMPV6 }, |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 225 | { "esp", IPPROTO_ESP }, |
| 226 | { "ah", IPPROTO_AH }, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 227 | { "all", 0 }, |
| 228 | }; |
| 229 | |
| 230 | static char * |
| 231 | proto_to_name(u_int8_t proto, int nolookup) |
| 232 | { |
| 233 | unsigned int i; |
| 234 | |
| 235 | if (proto && !nolookup) { |
| 236 | struct protoent *pent = getprotobynumber(proto); |
| 237 | if (pent) |
| 238 | return pent->p_name; |
| 239 | } |
| 240 | |
| 241 | for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++) |
| 242 | if (chain_protos[i].num == proto) |
| 243 | return chain_protos[i].name; |
| 244 | |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | in6addrcpy(struct in6_addr *dst, struct in6_addr *src) |
| 250 | { |
| 251 | memcpy(dst, src, sizeof(struct in6_addr)); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 252 | /* dst->s6_addr = src->s6_addr; */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame^] | 255 | static void free_opts(int reset_offset) |
| 256 | { |
| 257 | if (opts != original_opts) { |
| 258 | free(opts); |
| 259 | opts = original_opts; |
| 260 | if (reset_offset) |
| 261 | global_option_offset = 0; |
| 262 | } |
| 263 | } |
| 264 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 265 | void |
| 266 | exit_error(enum exittype status, char *msg, ...) |
| 267 | { |
| 268 | va_list args; |
| 269 | |
| 270 | va_start(args, msg); |
| 271 | fprintf(stderr, "%s v%s: ", program_name, program_version); |
| 272 | vfprintf(stderr, msg, args); |
| 273 | va_end(args); |
| 274 | fprintf(stderr, "\n"); |
| 275 | if (status == PARAMETER_PROBLEM) |
| 276 | exit_tryhelp(status); |
| 277 | if (status == VERSION_PROBLEM) |
| 278 | fprintf(stderr, |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 279 | "Perhaps ip6tables or your kernel needs to be upgraded.\n"); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame^] | 280 | /* On error paths, make sure that we don't leak memory */ |
| 281 | free_opts(1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 282 | exit(status); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | exit_tryhelp(int status) |
| 287 | { |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 288 | if (line != -1) |
| 289 | fprintf(stderr, "Error occurred at line: %d\n", line); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 290 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", |
| 291 | program_name, program_name ); |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame^] | 292 | free_opts(1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 293 | exit(status); |
| 294 | } |
| 295 | |
| 296 | void |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 297 | exit_printhelp(struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 298 | { |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 299 | struct ip6tables_rule_match *matchp = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 300 | struct ip6tables_target *t = NULL; |
| 301 | |
| 302 | printf("%s v%s\n\n" |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 303 | "Usage: %s -[AD] chain rule-specification [options]\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 304 | " %s -[RI] chain rulenum rule-specification [options]\n" |
| 305 | " %s -D chain rulenum [options]\n" |
| 306 | " %s -[LFZ] [chain] [options]\n" |
| 307 | " %s -[NX] chain\n" |
| 308 | " %s -E old-chain-name new-chain-name\n" |
| 309 | " %s -P chain target [options]\n" |
| 310 | " %s -h (print this help information)\n\n", |
| 311 | program_name, program_version, program_name, program_name, |
| 312 | program_name, program_name, program_name, program_name, |
| 313 | program_name, program_name); |
| 314 | |
| 315 | printf( |
| 316 | "Commands:\n" |
| 317 | "Either long or short options are allowed.\n" |
| 318 | " --append -A chain Append to chain\n" |
| 319 | " --delete -D chain Delete matching rule from chain\n" |
| 320 | " --delete -D chain rulenum\n" |
| 321 | " Delete rule rulenum (1 = first) from chain\n" |
| 322 | " --insert -I chain [rulenum]\n" |
| 323 | " Insert in chain as rulenum (default 1=first)\n" |
| 324 | " --replace -R chain rulenum\n" |
| 325 | " Replace rule rulenum (1 = first) in chain\n" |
| 326 | " --list -L [chain] List the rules in a chain or all chains\n" |
| 327 | " --flush -F [chain] Delete all rules in chain or all chains\n" |
| 328 | " --zero -Z [chain] Zero counters in chain or all chains\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 329 | " --new -N chain Create a new user-defined chain\n" |
| 330 | " --delete-chain\n" |
| 331 | " -X [chain] Delete a user-defined chain\n" |
| 332 | " --policy -P chain target\n" |
| 333 | " Change policy on chain to target\n" |
| 334 | " --rename-chain\n" |
| 335 | " -E old-chain new-chain\n" |
| 336 | " Change chain name, (moving any references)\n" |
| 337 | |
| 338 | "Options:\n" |
| 339 | " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n" |
| 340 | " --source -s [!] address[/mask]\n" |
| 341 | " source specification\n" |
| 342 | " --destination -d [!] address[/mask]\n" |
| 343 | " destination specification\n" |
| 344 | " --in-interface -i [!] input name[+]\n" |
| 345 | " network interface name ([+] for wildcard)\n" |
| 346 | " --jump -j target\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 347 | " target for rule (may load target extension)\n" |
| 348 | " --match -m match\n" |
| 349 | " extended match (may load extension)\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 350 | " --numeric -n numeric output of addresses and ports\n" |
| 351 | " --out-interface -o [!] output name[+]\n" |
| 352 | " network interface name ([+] for wildcard)\n" |
| 353 | " --table -t table table to manipulate (default: `filter')\n" |
| 354 | " --verbose -v verbose mode\n" |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 355 | " --line-numbers print line numbers when listing\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 356 | " --exact -x expand numbers (display exact values)\n" |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 357 | /*"[!] --fragment -f match second or further fragments only\n"*/ |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 358 | " --modprobe=<command> try to insert modules using this command\n" |
| 359 | " --set-counters PKTS BYTES set the counter during insert/append\n" |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 360 | "[!] --version -V print package version.\n"); |
| 361 | |
| 362 | /* Print out any special helps. A user might like to be able to add a --help |
| 363 | 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] | 364 | specified matches & targets */ |
| 365 | for (t = ip6tables_targets; t; t = t->next) { |
| 366 | if (t->used) { |
| 367 | printf("\n"); |
| 368 | t->help(); |
| 369 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 370 | } |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 371 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 372 | printf("\n"); |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 373 | matchp->match->help(); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 374 | } |
| 375 | exit(0); |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | generic_opt_check(int command, int options) |
| 380 | { |
| 381 | int i, j, legal = 0; |
| 382 | |
| 383 | /* Check that commands are valid with options. Complicated by the |
| 384 | * fact that if an option is legal with *any* command given, it is |
| 385 | * legal overall (ie. -z and -l). |
| 386 | */ |
| 387 | for (i = 0; i < NUMBER_OF_OPT; i++) { |
| 388 | legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */ |
| 389 | |
| 390 | for (j = 0; j < NUMBER_OF_CMD; j++) { |
| 391 | if (!(command & (1<<j))) |
| 392 | continue; |
| 393 | |
| 394 | if (!(options & (1<<i))) { |
| 395 | if (commands_v_options[j][i] == '+') |
| 396 | exit_error(PARAMETER_PROBLEM, |
| 397 | "You need to supply the `-%c' " |
| 398 | "option for this command\n", |
| 399 | optflags[i]); |
| 400 | } else { |
| 401 | if (commands_v_options[j][i] != 'x') |
| 402 | legal = 1; |
| 403 | else if (legal == 0) |
| 404 | legal = -1; |
| 405 | } |
| 406 | } |
| 407 | if (legal == -1) |
| 408 | exit_error(PARAMETER_PROBLEM, |
| 409 | "Illegal option `-%c' with this command\n", |
| 410 | optflags[i]); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | static char |
| 415 | opt2char(int option) |
| 416 | { |
| 417 | const char *ptr; |
| 418 | for (ptr = optflags; option > 1; option >>= 1, ptr++); |
| 419 | |
| 420 | return *ptr; |
| 421 | } |
| 422 | |
| 423 | static char |
| 424 | cmd2char(int option) |
| 425 | { |
| 426 | const char *ptr; |
| 427 | for (ptr = cmdflags; option > 1; option >>= 1, ptr++); |
| 428 | |
| 429 | return *ptr; |
| 430 | } |
| 431 | |
| 432 | static void |
| 433 | add_command(int *cmd, const int newcmd, const int othercmds, int invert) |
| 434 | { |
| 435 | if (invert) |
| 436 | exit_error(PARAMETER_PROBLEM, "unexpected ! flag"); |
| 437 | if (*cmd & (~othercmds)) |
| 438 | exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n", |
| 439 | cmd2char(newcmd), cmd2char(*cmd & (~othercmds))); |
| 440 | *cmd |= newcmd; |
| 441 | } |
| 442 | |
| 443 | int |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 444 | check_inverse(const char option[], int *invert, int *optind, int argc) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 445 | { |
| 446 | if (option && strcmp(option, "!") == 0) { |
| 447 | if (*invert) |
| 448 | exit_error(PARAMETER_PROBLEM, |
| 449 | "Multiple `!' flags not allowed"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 450 | *invert = TRUE; |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 451 | if (optind) { |
| 452 | *optind = *optind+1; |
| 453 | if (argc && *optind > argc) |
| 454 | exit_error(PARAMETER_PROBLEM, |
| 455 | "no argument following `!'"); |
| 456 | } |
| 457 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 458 | return TRUE; |
| 459 | } |
| 460 | return FALSE; |
| 461 | } |
| 462 | |
| 463 | static void * |
| 464 | fw_calloc(size_t count, size_t size) |
| 465 | { |
| 466 | void *p; |
| 467 | |
| 468 | if ((p = calloc(count, size)) == NULL) { |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 469 | perror("ip6tables: calloc failed"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 470 | exit(1); |
| 471 | } |
| 472 | return p; |
| 473 | } |
| 474 | |
| 475 | static void * |
| 476 | fw_malloc(size_t size) |
| 477 | { |
| 478 | void *p; |
| 479 | |
| 480 | if ((p = malloc(size)) == NULL) { |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 481 | perror("ip6tables: malloc failed"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 482 | exit(1); |
| 483 | } |
| 484 | return p; |
| 485 | } |
| 486 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 487 | static char * |
| 488 | addr_to_numeric(const struct in6_addr *addrp) |
| 489 | { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 490 | /* 0000:0000:0000:0000:0000:000.000.000.000 |
| 491 | * 0000:0000:0000:0000:0000:0000:0000:0000 */ |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 492 | static char buf[50+1]; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 493 | return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static struct in6_addr * |
| 497 | numeric_to_addr(const char *num) |
| 498 | { |
| 499 | static struct in6_addr ap; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 500 | int err; |
| 501 | if ((err=inet_pton(AF_INET6, num, &ap)) == 1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 502 | return ≈ |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 503 | #ifdef DEBUG |
| 504 | fprintf(stderr, "\nnumeric2addr: %d\n", err); |
| 505 | #endif |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 506 | return (struct in6_addr *)NULL; |
| 507 | } |
| 508 | |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 509 | |
| 510 | static struct in6_addr * |
| 511 | host_to_addr(const char *name, unsigned int *naddr) |
| 512 | { |
| 513 | struct addrinfo hints; |
| 514 | struct addrinfo *res; |
| 515 | static struct in6_addr *addr; |
| 516 | int err; |
| 517 | |
| 518 | memset(&hints, 0, sizeof(hints)); |
| 519 | hints.ai_flags=AI_CANONNAME; |
| 520 | hints.ai_family=AF_INET6; |
| 521 | hints.ai_socktype=SOCK_RAW; |
| 522 | hints.ai_protocol=41; |
| 523 | hints.ai_next=NULL; |
| 524 | |
| 525 | *naddr = 0; |
| 526 | if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){ |
| 527 | #ifdef DEBUG |
| 528 | fprintf(stderr,"Name2IP: %s\n",gai_strerror(err)); |
| 529 | #endif |
| 530 | return (struct in6_addr *) NULL; |
| 531 | } else { |
| 532 | if (res->ai_family != AF_INET6 || |
| 533 | res->ai_addrlen != sizeof(struct sockaddr_in6)) |
| 534 | return (struct in6_addr *) NULL; |
| 535 | |
| 536 | #ifdef DEBUG |
| 537 | fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen, |
| 538 | addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr))); |
| 539 | #endif |
| 540 | /* Get the first element of the address-chain */ |
| 541 | addr = fw_calloc(1, sizeof(struct in6_addr)); |
| 542 | in6addrcpy(addr, (struct in6_addr *) |
| 543 | &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); |
| 544 | freeaddrinfo(res); |
| 545 | *naddr = 1; |
| 546 | return addr; |
| 547 | } |
| 548 | |
| 549 | return (struct in6_addr *) NULL; |
| 550 | } |
| 551 | |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 552 | static char * |
| 553 | addr_to_host(const struct in6_addr *addr) |
| 554 | { |
| 555 | struct sockaddr_in6 saddr; |
| 556 | int err; |
| 557 | static char hostname[NI_MAXHOST]; |
| 558 | |
| 559 | memset(&saddr, 0, sizeof(struct sockaddr_in6)); |
| 560 | in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr); |
András Kis-Szabó | ed44b83 | 2001-06-27 02:21:45 +0000 | [diff] [blame] | 561 | saddr.sin6_family = AF_INET6; |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 562 | |
| 563 | if ( (err=getnameinfo((struct sockaddr *)&saddr, |
| 564 | sizeof(struct sockaddr_in6), |
| 565 | hostname, sizeof(hostname)-1, |
| 566 | NULL, 0, 0)) != 0 ){ |
| 567 | #ifdef DEBUG |
| 568 | fprintf(stderr,"IP2Name: %s\n",gai_strerror(err)); |
| 569 | #endif |
| 570 | return (char *) NULL; |
| 571 | } else { |
| 572 | #ifdef DEBUG |
| 573 | fprintf (stderr, "\naddr2host: %s\n", hostname); |
| 574 | #endif |
| 575 | |
| 576 | return hostname; |
| 577 | } |
| 578 | |
| 579 | return (char *) NULL; |
| 580 | } |
| 581 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 582 | static char * |
| 583 | mask_to_numeric(const struct in6_addr *addrp) |
| 584 | { |
Harald Welte | 8a50ab8 | 2003-06-24 18:15:59 +0000 | [diff] [blame] | 585 | static char buf[50+2]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 586 | int l = ipv6_prefix_length(addrp); |
Harald Welte | 8a50ab8 | 2003-06-24 18:15:59 +0000 | [diff] [blame] | 587 | if (l == -1) { |
| 588 | strcpy(buf, "/"); |
| 589 | strcat(buf, addr_to_numeric(addrp)); |
| 590 | return buf; |
| 591 | } |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 592 | sprintf(buf, "/%d", l); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 593 | return buf; |
| 594 | } |
| 595 | |
| 596 | static struct in6_addr * |
| 597 | network_to_addr(const char *name) |
| 598 | { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 599 | /* abort();*/ |
| 600 | /* TODO: not implemented yet, but the exception breaks the |
| 601 | * name resolvation */ |
| 602 | return (struct in6_addr *)NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static char * |
| 606 | addr_to_anyname(const struct in6_addr *addr) |
| 607 | { |
| 608 | char *name; |
| 609 | |
| 610 | if ((name = addr_to_host(addr)) != NULL) |
| 611 | return name; |
| 612 | |
| 613 | return addr_to_numeric(addr); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * All functions starting with "parse" should succeed, otherwise |
| 618 | * the program fails. |
| 619 | * Most routines return pointers to static data that may change |
| 620 | * between calls to the same or other routines with a few exceptions: |
| 621 | * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask" |
| 622 | * return global static data. |
| 623 | */ |
| 624 | |
| 625 | static struct in6_addr * |
| 626 | parse_hostnetwork(const char *name, unsigned int *naddrs) |
| 627 | { |
| 628 | struct in6_addr *addrp, *addrptmp; |
| 629 | |
| 630 | if ((addrptmp = numeric_to_addr(name)) != NULL || |
| 631 | (addrptmp = network_to_addr(name)) != NULL) { |
| 632 | addrp = fw_malloc(sizeof(struct in6_addr)); |
| 633 | in6addrcpy(addrp, addrptmp); |
| 634 | *naddrs = 1; |
| 635 | return addrp; |
| 636 | } |
| 637 | if ((addrp = host_to_addr(name, naddrs)) != NULL) |
| 638 | return addrp; |
| 639 | |
| 640 | exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
| 641 | } |
| 642 | |
| 643 | static struct in6_addr * |
| 644 | parse_mask(char *mask) |
| 645 | { |
| 646 | static struct in6_addr maskaddr; |
| 647 | struct in6_addr *addrp; |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 648 | unsigned int bits; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 649 | |
| 650 | if (mask == NULL) { |
| 651 | /* no mask at all defaults to 128 bits */ |
| 652 | memset(&maskaddr, 0xff, sizeof maskaddr); |
| 653 | return &maskaddr; |
| 654 | } |
| 655 | if ((addrp = numeric_to_addr(mask)) != NULL) |
| 656 | return addrp; |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 657 | if (string_to_number(mask, 0, 128, &bits) == -1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 658 | exit_error(PARAMETER_PROBLEM, |
| 659 | "invalid mask `%s' specified", mask); |
| 660 | if (bits != 0) { |
| 661 | char *p = (char *)&maskaddr; |
| 662 | memset(p, 0xff, bits / 8); |
| 663 | memset(p + (bits / 8) + 1, 0, (128 - bits) / 8); |
| 664 | p[bits / 8] = 0xff << (8 - (bits & 7)); |
| 665 | return &maskaddr; |
| 666 | } |
| 667 | |
| 668 | memset(&maskaddr, 0, sizeof maskaddr); |
| 669 | return &maskaddr; |
| 670 | } |
| 671 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 672 | void |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 673 | parse_hostnetworkmask(const char *name, struct in6_addr **addrpp, |
| 674 | struct in6_addr *maskp, unsigned int *naddrs) |
| 675 | { |
| 676 | struct in6_addr *addrp; |
| 677 | char buf[256]; |
| 678 | char *p; |
| 679 | int i, j, n; |
| 680 | |
| 681 | strncpy(buf, name, sizeof(buf) - 1); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 682 | buf[sizeof(buf) - 1] = '\0'; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 683 | if ((p = strrchr(buf, '/')) != NULL) { |
| 684 | *p = '\0'; |
| 685 | addrp = parse_mask(p + 1); |
| 686 | } else |
| 687 | addrp = parse_mask(NULL); |
| 688 | in6addrcpy(maskp, addrp); |
| 689 | |
| 690 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 691 | if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any))) |
| 692 | strcpy(buf, "::"); |
| 693 | |
| 694 | addrp = *addrpp = parse_hostnetwork(buf, naddrs); |
| 695 | n = *naddrs; |
| 696 | for (i = 0, j = 0; i < n; i++) { |
| 697 | int k; |
| 698 | for (k = 0; k < 4; k++) |
| 699 | addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k]; |
| 700 | j++; |
| 701 | for (k = 0; k < j - 1; k++) { |
András Kis-Szabó | 058eaad | 2001-06-16 15:42:17 +0000 | [diff] [blame] | 702 | if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 703 | (*naddrs)--; |
| 704 | j--; |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | struct ip6tables_match * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 712 | find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 713 | { |
| 714 | struct ip6tables_match *ptr; |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 715 | int icmphack = 0; |
| 716 | |
| 717 | /* This is ugly as hell. Nonetheless, there is no way of changing |
| 718 | * this without hurting backwards compatibility */ |
| 719 | if ( (strcmp(name,"icmpv6") == 0) || |
| 720 | (strcmp(name,"ipv6-icmp") == 0) || |
| 721 | (strcmp(name,"icmp6") == 0) ) icmphack = 1; |
| 722 | |
| 723 | if (!icmphack) { |
| 724 | for (ptr = ip6tables_matches; ptr; ptr = ptr->next) { |
| 725 | if (strcmp(name, ptr->name) == 0) |
| 726 | break; |
| 727 | } |
| 728 | } else { |
| 729 | for (ptr = ip6tables_matches; ptr; ptr = ptr->next) { |
| 730 | if (strcmp("icmp6", ptr->name) == 0) |
| 731 | break; |
| 732 | } |
| 733 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 734 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 735 | #ifndef NO_SHARED_LIBS |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 736 | if (!ptr && tryload != DONT_LOAD) { |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 737 | char path[strlen(lib_dir) + sizeof("/libip6t_.so") |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 738 | + strlen(name)]; |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 739 | if (!icmphack) |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 740 | sprintf(path, "%s/libip6t_%s.so", lib_dir, name); |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 741 | else |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 742 | sprintf(path, "%s/libip6t_%s.so", lib_dir, "icmpv6"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 743 | if (dlopen(path, RTLD_NOW)) { |
| 744 | /* Found library. If it didn't register itself, |
| 745 | maybe they specified target as match. */ |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 746 | ptr = find_match(name, DONT_LOAD, NULL); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 747 | |
| 748 | if (!ptr) |
| 749 | exit_error(PARAMETER_PROBLEM, |
| 750 | "Couldn't load match `%s'\n", |
| 751 | name); |
| 752 | } else if (tryload == LOAD_MUST_SUCCEED) |
| 753 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 754 | "Couldn't load match `%s':%s\n", |
| 755 | name, dlerror()); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 756 | } |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 757 | #else |
| 758 | if (ptr && !ptr->loaded) { |
| 759 | if (tryload != DONT_LOAD) |
| 760 | ptr->loaded = 1; |
| 761 | else |
| 762 | ptr = NULL; |
| 763 | } |
Marc Boucher | 067477b | 2002-03-24 15:09:31 +0000 | [diff] [blame] | 764 | if(!ptr && (tryload == LOAD_MUST_SUCCEED)) { |
| 765 | exit_error(PARAMETER_PROBLEM, |
| 766 | "Couldn't find match `%s'\n", name); |
| 767 | } |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 768 | #endif |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 769 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 770 | if (ptr && matches) { |
| 771 | struct ip6tables_rule_match **i; |
| 772 | struct ip6tables_rule_match *newentry; |
| 773 | |
| 774 | newentry = fw_malloc(sizeof(struct ip6tables_rule_match)); |
| 775 | |
| 776 | for (i = matches; *i; i = &(*i)->next); |
| 777 | newentry->match = ptr; |
| 778 | newentry->next = NULL; |
| 779 | *i = newentry; |
| 780 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 781 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 782 | return ptr; |
| 783 | } |
| 784 | |
| 785 | /* Christophe Burki wants `-p 6' to imply `-m tcp'. */ |
| 786 | static struct ip6tables_match * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 787 | find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 788 | { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 789 | unsigned int proto; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 790 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 791 | if (string_to_number(pname, 0, 255, &proto) != -1) { |
| 792 | char *protoname = proto_to_name(proto, nolookup); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 793 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 794 | if (protoname) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 795 | return find_match(protoname, tryload, matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 796 | } else |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 797 | return find_match(pname, tryload, matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 798 | |
| 799 | return NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 802 | u_int16_t |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 803 | parse_protocol(const char *s) |
| 804 | { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 805 | unsigned int proto; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 806 | |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 807 | if (string_to_number(s, 0, 255, &proto) == -1) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 808 | struct protoent *pent; |
| 809 | |
| 810 | if ((pent = getprotobyname(s))) |
| 811 | proto = pent->p_proto; |
| 812 | else { |
| 813 | unsigned int i; |
| 814 | for (i = 0; |
| 815 | i < sizeof(chain_protos)/sizeof(struct pprot); |
| 816 | i++) { |
| 817 | if (strcmp(s, chain_protos[i].name) == 0) { |
| 818 | proto = chain_protos[i].num; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | if (i == sizeof(chain_protos)/sizeof(struct pprot)) |
| 823 | exit_error(PARAMETER_PROBLEM, |
| 824 | "unknown protocol `%s' specified", |
| 825 | s); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return (u_int16_t)proto; |
| 830 | } |
| 831 | |
| 832 | static void |
| 833 | parse_interface(const char *arg, char *vianame, unsigned char *mask) |
| 834 | { |
| 835 | int vialen = strlen(arg); |
| 836 | unsigned int i; |
| 837 | |
| 838 | memset(mask, 0, IFNAMSIZ); |
| 839 | memset(vianame, 0, IFNAMSIZ); |
| 840 | |
| 841 | if (vialen + 1 > IFNAMSIZ) |
| 842 | exit_error(PARAMETER_PROBLEM, |
| 843 | "interface name `%s' must be shorter than IFNAMSIZ" |
| 844 | " (%i)", arg, IFNAMSIZ-1); |
| 845 | |
| 846 | strcpy(vianame, arg); |
Ozgur AKAN | 3610deb | 2004-04-07 09:36:29 +0000 | [diff] [blame] | 847 | if ((vialen == 0) || (vialen == 1 && vianame[0] == '+')) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 848 | memset(mask, 0, IFNAMSIZ); |
| 849 | else if (vianame[vialen - 1] == '+') { |
| 850 | memset(mask, 0xFF, vialen - 1); |
| 851 | memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 852 | /* Don't remove `+' here! -HW */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 853 | } else { |
| 854 | /* Include nul-terminator in match */ |
| 855 | memset(mask, 0xFF, vialen + 1); |
| 856 | memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 857 | for (i = 0; vianame[i]; i++) { |
| 858 | if (!isalnum(vianame[i]) |
| 859 | && vianame[i] != '_' |
| 860 | && vianame[i] != '.') { |
| 861 | printf("Warning: wierd character in interface" |
| 862 | " `%s' (No aliases, :, ! or *).\n", |
| 863 | vianame); |
| 864 | break; |
| 865 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* Can't be zero. */ |
| 871 | static int |
| 872 | parse_rulenumber(const char *rule) |
| 873 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 874 | unsigned int rulenum; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 875 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 876 | if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 877 | exit_error(PARAMETER_PROBLEM, |
| 878 | "Invalid rule number `%s'", rule); |
| 879 | |
| 880 | return rulenum; |
| 881 | } |
| 882 | |
| 883 | static const char * |
| 884 | parse_target(const char *targetname) |
| 885 | { |
| 886 | const char *ptr; |
| 887 | |
| 888 | if (strlen(targetname) < 1) |
| 889 | exit_error(PARAMETER_PROBLEM, |
| 890 | "Invalid target name (too short)"); |
| 891 | |
| 892 | if (strlen(targetname)+1 > sizeof(ip6t_chainlabel)) |
| 893 | exit_error(PARAMETER_PROBLEM, |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 894 | "Invalid target name `%s' (%u chars max)", |
| 895 | targetname, (unsigned int)sizeof(ip6t_chainlabel)-1); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 896 | |
| 897 | for (ptr = targetname; *ptr; ptr++) |
| 898 | if (isspace(*ptr)) |
| 899 | exit_error(PARAMETER_PROBLEM, |
| 900 | "Invalid target name `%s'", targetname); |
| 901 | return targetname; |
| 902 | } |
| 903 | |
| 904 | int |
Martin Josefsson | b105bc9 | 2004-05-26 15:54:49 +0000 | [diff] [blame] | 905 | string_to_number_ll(const char *s, unsigned long long min, unsigned long long max, |
| 906 | unsigned long long *ret) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 907 | { |
Martin Josefsson | b105bc9 | 2004-05-26 15:54:49 +0000 | [diff] [blame] | 908 | unsigned long long number; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 909 | char *end; |
| 910 | |
| 911 | /* Handle hex, octal, etc. */ |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 912 | errno = 0; |
Martin Josefsson | b105bc9 | 2004-05-26 15:54:49 +0000 | [diff] [blame] | 913 | number = strtoull(s, &end, 0); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 914 | if (*end == '\0' && end != s) { |
| 915 | /* we parsed a number, let's see if we want this */ |
Martin Josefsson | b105bc9 | 2004-05-26 15:54:49 +0000 | [diff] [blame] | 916 | if (errno != ERANGE && min <= number && (!max || number <= max)) { |
Harald Welte | ed49849 | 2001-07-23 01:24:22 +0000 | [diff] [blame] | 917 | *ret = number; |
| 918 | return 0; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 919 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 920 | } |
| 921 | return -1; |
| 922 | } |
| 923 | |
Martin Josefsson | b105bc9 | 2004-05-26 15:54:49 +0000 | [diff] [blame] | 924 | int |
| 925 | string_to_number_l(const char *s, unsigned long min, unsigned long max, |
| 926 | unsigned long *ret) |
| 927 | { |
| 928 | int result; |
| 929 | unsigned long long number; |
| 930 | |
| 931 | result = string_to_number_ll(s, min, max, &number); |
| 932 | *ret = (unsigned long)number; |
| 933 | |
| 934 | return result; |
| 935 | } |
| 936 | |
| 937 | int string_to_number(const char *s, unsigned int min, unsigned int max, |
| 938 | unsigned int *ret) |
| 939 | { |
| 940 | int result; |
| 941 | unsigned long number; |
| 942 | |
| 943 | result = string_to_number_l(s, min, max, &number); |
| 944 | *ret = (unsigned int)number; |
| 945 | |
| 946 | return result; |
| 947 | } |
| 948 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 949 | static void |
| 950 | set_option(unsigned int *options, unsigned int option, u_int8_t *invflg, |
| 951 | int invert) |
| 952 | { |
| 953 | if (*options & option) |
| 954 | exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed", |
| 955 | opt2char(option)); |
| 956 | *options |= option; |
| 957 | |
| 958 | if (invert) { |
| 959 | unsigned int i; |
| 960 | for (i = 0; 1 << i != option; i++); |
| 961 | |
| 962 | if (!inverse_for_options[i]) |
| 963 | exit_error(PARAMETER_PROBLEM, |
| 964 | "cannot have ! before -%c", |
| 965 | opt2char(option)); |
| 966 | *invflg |= inverse_for_options[i]; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | struct ip6tables_target * |
| 971 | find_target(const char *name, enum ip6t_tryload tryload) |
| 972 | { |
| 973 | struct ip6tables_target *ptr; |
| 974 | |
| 975 | /* Standard target? */ |
| 976 | if (strcmp(name, "") == 0 |
| 977 | || strcmp(name, IP6TC_LABEL_ACCEPT) == 0 |
| 978 | || strcmp(name, IP6TC_LABEL_DROP) == 0 |
| 979 | || strcmp(name, IP6TC_LABEL_QUEUE) == 0 |
| 980 | || strcmp(name, IP6TC_LABEL_RETURN) == 0) |
| 981 | name = "standard"; |
| 982 | |
| 983 | for (ptr = ip6tables_targets; ptr; ptr = ptr->next) { |
| 984 | if (strcmp(name, ptr->name) == 0) |
| 985 | break; |
| 986 | } |
| 987 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 988 | #ifndef NO_SHARED_LIBS |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 989 | if (!ptr && tryload != DONT_LOAD) { |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 990 | char path[strlen(lib_dir) + sizeof("/libip6t_.so") |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 991 | + strlen(name)]; |
Rusty Russell | 208d42e | 2004-12-20 05:29:52 +0000 | [diff] [blame] | 992 | sprintf(path, "%s/libip6t_%s.so", lib_dir, name); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 993 | if (dlopen(path, RTLD_NOW)) { |
| 994 | /* Found library. If it didn't register itself, |
| 995 | maybe they specified match as a target. */ |
| 996 | ptr = find_target(name, DONT_LOAD); |
| 997 | if (!ptr) |
| 998 | exit_error(PARAMETER_PROBLEM, |
| 999 | "Couldn't load target `%s'\n", |
| 1000 | name); |
| 1001 | } else if (tryload == LOAD_MUST_SUCCEED) |
| 1002 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1003 | "Couldn't load target `%s':%s\n", |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1004 | name, dlerror()); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1005 | } |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 1006 | #else |
| 1007 | if (ptr && !ptr->loaded) { |
| 1008 | if (tryload != DONT_LOAD) |
| 1009 | ptr->loaded = 1; |
| 1010 | else |
| 1011 | ptr = NULL; |
| 1012 | } |
Marc Boucher | 067477b | 2002-03-24 15:09:31 +0000 | [diff] [blame] | 1013 | if(!ptr && (tryload == LOAD_MUST_SUCCEED)) { |
| 1014 | exit_error(PARAMETER_PROBLEM, |
| 1015 | "Couldn't find target `%s'\n", name); |
| 1016 | } |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 1017 | #endif |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1018 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1019 | if (ptr) |
| 1020 | ptr->used = 1; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1021 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1022 | return ptr; |
| 1023 | } |
| 1024 | |
| 1025 | static struct option * |
Jan Echternach | af8fe9e | 2000-08-27 07:41:39 +0000 | [diff] [blame] | 1026 | merge_options(struct option *oldopts, const struct option *newopts, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1027 | unsigned int *option_offset) |
| 1028 | { |
| 1029 | unsigned int num_old, num_new, i; |
| 1030 | struct option *merge; |
| 1031 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame^] | 1032 | /* Release previous options merged if any */ |
| 1033 | free_opts(0); |
| 1034 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1035 | for (num_old = 0; oldopts[num_old].name; num_old++); |
| 1036 | for (num_new = 0; newopts[num_new].name; num_new++); |
| 1037 | |
| 1038 | global_option_offset += OPTION_OFFSET; |
| 1039 | *option_offset = global_option_offset; |
| 1040 | |
| 1041 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
| 1042 | memcpy(merge, oldopts, num_old * sizeof(struct option)); |
| 1043 | for (i = 0; i < num_new; i++) { |
| 1044 | merge[num_old + i] = newopts[i]; |
| 1045 | merge[num_old + i].val += *option_offset; |
| 1046 | } |
| 1047 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
| 1048 | |
| 1049 | return merge; |
| 1050 | } |
| 1051 | |
| 1052 | void |
| 1053 | register_match6(struct ip6tables_match *me) |
| 1054 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1055 | struct ip6tables_match **i; |
| 1056 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1057 | if (strcmp(me->version, program_version) != 0) { |
| 1058 | fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n", |
| 1059 | program_name, me->name, me->version, program_version); |
| 1060 | exit(1); |
| 1061 | } |
| 1062 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1063 | if (find_match(me->name, DONT_LOAD, NULL)) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1064 | fprintf(stderr, "%s: match `%s' already registered.\n", |
| 1065 | program_name, me->name); |
| 1066 | exit(1); |
| 1067 | } |
| 1068 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1069 | if (me->size != IP6T_ALIGN(me->size)) { |
| 1070 | fprintf(stderr, "%s: match `%s' has invalid size %u.\n", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1071 | program_name, me->name, (unsigned int)me->size); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1072 | exit(1); |
| 1073 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1074 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1075 | /* Append to list. */ |
| 1076 | for (i = &ip6tables_matches; *i; i = &(*i)->next); |
| 1077 | me->next = NULL; |
| 1078 | *i = me; |
| 1079 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1080 | me->m = NULL; |
| 1081 | me->mflags = 0; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | void |
| 1085 | register_target6(struct ip6tables_target *me) |
| 1086 | { |
| 1087 | if (strcmp(me->version, program_version) != 0) { |
| 1088 | fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n", |
| 1089 | program_name, me->name, me->version, program_version); |
| 1090 | exit(1); |
| 1091 | } |
| 1092 | |
| 1093 | if (find_target(me->name, DONT_LOAD)) { |
| 1094 | fprintf(stderr, "%s: target `%s' already registered.\n", |
| 1095 | program_name, me->name); |
| 1096 | exit(1); |
| 1097 | } |
| 1098 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1099 | if (me->size != IP6T_ALIGN(me->size)) { |
| 1100 | fprintf(stderr, "%s: target `%s' has invalid size %u.\n", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1101 | program_name, me->name, (unsigned int)me->size); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1102 | exit(1); |
| 1103 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1104 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1105 | /* Prepend to list. */ |
| 1106 | me->next = ip6tables_targets; |
| 1107 | ip6tables_targets = me; |
| 1108 | me->t = NULL; |
| 1109 | me->tflags = 0; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | static void |
| 1113 | print_num(u_int64_t number, unsigned int format) |
| 1114 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1115 | if (format & FMT_KILOMEGAGIGA) { |
| 1116 | if (number > 99999) { |
| 1117 | number = (number + 500) / 1000; |
| 1118 | if (number > 9999) { |
| 1119 | number = (number + 500) / 1000; |
| 1120 | if (number > 9999) { |
| 1121 | number = (number + 500) / 1000; |
| 1122 | if (number > 9999) { |
| 1123 | number = (number + 500) / 1000; |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1124 | printf(FMT("%4lluT ","%lluT "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1125 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1126 | else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1127 | } |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1128 | else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1129 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1130 | printf(FMT("%4lluK ","%lluK "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1131 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1132 | printf(FMT("%5llu ","%llu "), (unsigned long long)number); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1133 | } else |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1134 | printf(FMT("%8llu ","%llu "), (unsigned long long)number); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1137 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1138 | static void |
| 1139 | print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle) |
| 1140 | { |
| 1141 | struct ip6t_counters counters; |
| 1142 | const char *pol = ip6tc_get_policy(chain, &counters, handle); |
| 1143 | printf("Chain %s", chain); |
| 1144 | if (pol) { |
| 1145 | printf(" (policy %s", pol); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1146 | if (!(format & FMT_NOCOUNTS)) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1147 | fputc(' ', stdout); |
| 1148 | print_num(counters.pcnt, (format|FMT_NOTABLE)); |
| 1149 | fputs("packets, ", stdout); |
| 1150 | print_num(counters.bcnt, (format|FMT_NOTABLE)); |
| 1151 | fputs("bytes", stdout); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1152 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1153 | printf(")\n"); |
| 1154 | } else { |
| 1155 | unsigned int refs; |
| 1156 | if (!ip6tc_get_references(&refs, chain, handle)) |
| 1157 | printf(" (ERROR obtaining refs)\n"); |
| 1158 | else |
| 1159 | printf(" (%u references)\n", refs); |
| 1160 | } |
| 1161 | |
| 1162 | if (format & FMT_LINENUMBERS) |
| 1163 | printf(FMT("%-4s ", "%s "), "num"); |
| 1164 | if (!(format & FMT_NOCOUNTS)) { |
| 1165 | if (format & FMT_KILOMEGAGIGA) { |
| 1166 | printf(FMT("%5s ","%s "), "pkts"); |
| 1167 | printf(FMT("%5s ","%s "), "bytes"); |
| 1168 | } else { |
| 1169 | printf(FMT("%8s ","%s "), "pkts"); |
| 1170 | printf(FMT("%10s ","%s "), "bytes"); |
| 1171 | } |
| 1172 | } |
| 1173 | if (!(format & FMT_NOTARGET)) |
| 1174 | printf(FMT("%-9s ","%s "), "target"); |
| 1175 | fputs(" prot ", stdout); |
| 1176 | if (format & FMT_OPTIONS) |
| 1177 | fputs("opt", stdout); |
| 1178 | if (format & FMT_VIA) { |
| 1179 | printf(FMT(" %-6s ","%s "), "in"); |
| 1180 | printf(FMT("%-6s ","%s "), "out"); |
| 1181 | } |
| 1182 | printf(FMT(" %-19s ","%s "), "source"); |
| 1183 | printf(FMT(" %-19s "," %s "), "destination"); |
| 1184 | printf("\n"); |
| 1185 | } |
| 1186 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1187 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1188 | static int |
| 1189 | print_match(const struct ip6t_entry_match *m, |
| 1190 | const struct ip6t_ip6 *ip, |
| 1191 | int numeric) |
| 1192 | { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1193 | struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | if (match) { |
| 1196 | if (match->print) |
| 1197 | match->print(ip, m, numeric); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1198 | else |
| 1199 | printf("%s ", match->name); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1200 | } else { |
| 1201 | if (m->u.user.name[0]) |
| 1202 | printf("UNKNOWN match `%s' ", m->u.user.name); |
| 1203 | } |
| 1204 | /* Don't stop iterating. */ |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | /* e is called `fw' here for hysterical raisins */ |
| 1209 | static void |
| 1210 | print_firewall(const struct ip6t_entry *fw, |
| 1211 | const char *targname, |
| 1212 | unsigned int num, |
| 1213 | unsigned int format, |
| 1214 | const ip6tc_handle_t handle) |
| 1215 | { |
| 1216 | struct ip6tables_target *target = NULL; |
| 1217 | const struct ip6t_entry_target *t; |
| 1218 | u_int8_t flags; |
| 1219 | char buf[BUFSIZ]; |
| 1220 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1221 | if (!ip6tc_is_chain(targname, handle)) |
| 1222 | target = find_target(targname, TRY_LOAD); |
| 1223 | else |
| 1224 | target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED); |
| 1225 | |
| 1226 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 1227 | flags = fw->ipv6.flags; |
| 1228 | |
| 1229 | if (format & FMT_LINENUMBERS) |
| 1230 | printf(FMT("%-4u ", "%u "), num+1); |
| 1231 | |
| 1232 | if (!(format & FMT_NOCOUNTS)) { |
| 1233 | print_num(fw->counters.pcnt, format); |
| 1234 | print_num(fw->counters.bcnt, format); |
| 1235 | } |
| 1236 | |
| 1237 | if (!(format & FMT_NOTARGET)) |
| 1238 | printf(FMT("%-9s ", "%s "), targname); |
| 1239 | |
| 1240 | fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout); |
| 1241 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1242 | char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1243 | if (pname) |
| 1244 | printf(FMT("%-5s", "%s "), pname); |
| 1245 | else |
| 1246 | printf(FMT("%-5hu", "%hu "), fw->ipv6.proto); |
| 1247 | } |
| 1248 | |
| 1249 | if (format & FMT_OPTIONS) { |
| 1250 | if (format & FMT_NOTABLE) |
| 1251 | fputs("opt ", stdout); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1252 | fputc(' ', stdout); /* Invert flag of FRAG */ |
| 1253 | fputc(' ', stdout); /* -f */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1254 | fputc(' ', stdout); |
| 1255 | } |
| 1256 | |
| 1257 | if (format & FMT_VIA) { |
| 1258 | char iface[IFNAMSIZ+2]; |
| 1259 | |
| 1260 | if (fw->ipv6.invflags & IP6T_INV_VIA_IN) { |
| 1261 | iface[0] = '!'; |
| 1262 | iface[1] = '\0'; |
| 1263 | } |
| 1264 | else iface[0] = '\0'; |
| 1265 | |
| 1266 | if (fw->ipv6.iniface[0] != '\0') { |
| 1267 | strcat(iface, fw->ipv6.iniface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1268 | } |
| 1269 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 1270 | else strcat(iface, "any"); |
| 1271 | printf(FMT(" %-6s ","in %s "), iface); |
| 1272 | |
| 1273 | if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) { |
| 1274 | iface[0] = '!'; |
| 1275 | iface[1] = '\0'; |
| 1276 | } |
| 1277 | else iface[0] = '\0'; |
| 1278 | |
| 1279 | if (fw->ipv6.outiface[0] != '\0') { |
| 1280 | strcat(iface, fw->ipv6.outiface); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1281 | } |
| 1282 | else if (format & FMT_NUMERIC) strcat(iface, "*"); |
| 1283 | else strcat(iface, "any"); |
| 1284 | printf(FMT("%-6s ","out %s "), iface); |
| 1285 | } |
| 1286 | |
| 1287 | fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout); |
| 1288 | if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any) |
| 1289 | && !(format & FMT_NUMERIC)) |
| 1290 | printf(FMT("%-19s ","%s "), "anywhere"); |
| 1291 | else { |
| 1292 | if (format & FMT_NUMERIC) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1293 | sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1294 | else |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1295 | sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1296 | strcat(buf, mask_to_numeric(&(fw->ipv6.smsk))); |
| 1297 | printf(FMT("%-19s ","%s "), buf); |
| 1298 | } |
| 1299 | |
| 1300 | fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout); |
| 1301 | if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any) |
| 1302 | && !(format & FMT_NUMERIC)) |
| 1303 | printf(FMT("%-19s","-> %s"), "anywhere"); |
| 1304 | else { |
| 1305 | if (format & FMT_NUMERIC) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1306 | sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1307 | else |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1308 | sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1309 | strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk))); |
| 1310 | printf(FMT("%-19s","-> %s"), buf); |
| 1311 | } |
| 1312 | |
| 1313 | if (format & FMT_NOTABLE) |
| 1314 | fputs(" ", stdout); |
| 1315 | |
| 1316 | IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC); |
| 1317 | |
| 1318 | if (target) { |
| 1319 | if (target->print) |
| 1320 | /* Print the target information. */ |
| 1321 | target->print(&fw->ipv6, t, format & FMT_NUMERIC); |
| 1322 | } else if (t->u.target_size != sizeof(*t)) |
| 1323 | printf("[%u bytes of unknown target data] ", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 1324 | (unsigned int)(t->u.target_size - sizeof(*t))); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (!(format & FMT_NONEWLINE)) |
| 1327 | fputc('\n', stdout); |
| 1328 | } |
| 1329 | |
| 1330 | static void |
| 1331 | print_firewall_line(const struct ip6t_entry *fw, |
| 1332 | const ip6tc_handle_t h) |
| 1333 | { |
| 1334 | struct ip6t_entry_target *t; |
| 1335 | |
| 1336 | t = ip6t_get_target((struct ip6t_entry *)fw); |
| 1337 | print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h); |
| 1338 | } |
| 1339 | |
| 1340 | static int |
| 1341 | append_entry(const ip6t_chainlabel chain, |
| 1342 | struct ip6t_entry *fw, |
| 1343 | unsigned int nsaddrs, |
| 1344 | const struct in6_addr saddrs[], |
| 1345 | unsigned int ndaddrs, |
| 1346 | const struct in6_addr daddrs[], |
| 1347 | int verbose, |
| 1348 | ip6tc_handle_t *handle) |
| 1349 | { |
| 1350 | unsigned int i, j; |
| 1351 | int ret = 1; |
| 1352 | |
| 1353 | for (i = 0; i < nsaddrs; i++) { |
| 1354 | fw->ipv6.src = saddrs[i]; |
| 1355 | for (j = 0; j < ndaddrs; j++) { |
| 1356 | fw->ipv6.dst = daddrs[j]; |
| 1357 | if (verbose) |
| 1358 | print_firewall_line(fw, *handle); |
| 1359 | ret &= ip6tc_append_entry(chain, fw, handle); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | static int |
| 1367 | replace_entry(const ip6t_chainlabel chain, |
| 1368 | struct ip6t_entry *fw, |
| 1369 | unsigned int rulenum, |
| 1370 | const struct in6_addr *saddr, |
| 1371 | const struct in6_addr *daddr, |
| 1372 | int verbose, |
| 1373 | ip6tc_handle_t *handle) |
| 1374 | { |
| 1375 | fw->ipv6.src = *saddr; |
| 1376 | fw->ipv6.dst = *daddr; |
| 1377 | |
| 1378 | if (verbose) |
| 1379 | print_firewall_line(fw, *handle); |
| 1380 | return ip6tc_replace_entry(chain, fw, rulenum, handle); |
| 1381 | } |
| 1382 | |
| 1383 | static int |
| 1384 | insert_entry(const ip6t_chainlabel chain, |
| 1385 | struct ip6t_entry *fw, |
| 1386 | unsigned int rulenum, |
| 1387 | unsigned int nsaddrs, |
| 1388 | const struct in6_addr saddrs[], |
| 1389 | unsigned int ndaddrs, |
| 1390 | const struct in6_addr daddrs[], |
| 1391 | int verbose, |
| 1392 | ip6tc_handle_t *handle) |
| 1393 | { |
| 1394 | unsigned int i, j; |
| 1395 | int ret = 1; |
| 1396 | |
| 1397 | for (i = 0; i < nsaddrs; i++) { |
| 1398 | fw->ipv6.src = saddrs[i]; |
| 1399 | for (j = 0; j < ndaddrs; j++) { |
| 1400 | fw->ipv6.dst = daddrs[j]; |
| 1401 | if (verbose) |
| 1402 | print_firewall_line(fw, *handle); |
| 1403 | ret &= ip6tc_insert_entry(chain, fw, rulenum, handle); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
| 1410 | static unsigned char * |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1411 | make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1412 | { |
| 1413 | /* Establish mask for comparison */ |
| 1414 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1415 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1416 | unsigned char *mask, *mptr; |
| 1417 | |
| 1418 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1419 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1420 | size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1421 | |
| 1422 | mask = fw_calloc(1, size |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1423 | + IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1424 | + ip6tables_targets->size); |
| 1425 | |
| 1426 | memset(mask, 0xFF, sizeof(struct ip6t_entry)); |
| 1427 | mptr = mask + sizeof(struct ip6t_entry); |
| 1428 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1429 | for (matchp = matches; matchp; matchp = matchp->next) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1430 | memset(mptr, 0xFF, |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1431 | IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1432 | + matchp->match->userspacesize); |
| 1433 | mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1436 | memset(mptr, 0xFF, |
| 1437 | IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
| 1438 | + ip6tables_targets->userspacesize); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1439 | |
| 1440 | return mask; |
| 1441 | } |
| 1442 | |
| 1443 | static int |
| 1444 | delete_entry(const ip6t_chainlabel chain, |
| 1445 | struct ip6t_entry *fw, |
| 1446 | unsigned int nsaddrs, |
| 1447 | const struct in6_addr saddrs[], |
| 1448 | unsigned int ndaddrs, |
| 1449 | const struct in6_addr daddrs[], |
| 1450 | int verbose, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1451 | ip6tc_handle_t *handle, |
| 1452 | struct ip6tables_rule_match *matches) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1453 | { |
| 1454 | unsigned int i, j; |
| 1455 | int ret = 1; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1456 | unsigned char *mask; |
| 1457 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1458 | mask = make_delete_mask(fw, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1459 | for (i = 0; i < nsaddrs; i++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1460 | fw->ipv6.src = saddrs[i]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1461 | for (j = 0; j < ndaddrs; j++) { |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1462 | fw->ipv6.dst = daddrs[j]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1463 | if (verbose) |
| 1464 | print_firewall_line(fw, *handle); |
Philip Blundell | 57e07af | 2000-06-04 17:25:33 +0000 | [diff] [blame] | 1465 | ret &= ip6tc_delete_entry(chain, fw, mask, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1468 | free(mask); |
| 1469 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1470 | return ret; |
| 1471 | } |
| 1472 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1473 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1474 | for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), |
| 1475 | int verbose, int builtinstoo, ip6tc_handle_t *handle) |
| 1476 | { |
| 1477 | int ret = 1; |
| 1478 | const char *chain; |
| 1479 | char *chains; |
| 1480 | unsigned int i, chaincount = 0; |
| 1481 | |
| 1482 | chain = ip6tc_first_chain(handle); |
| 1483 | while (chain) { |
| 1484 | chaincount++; |
| 1485 | chain = ip6tc_next_chain(handle); |
| 1486 | } |
| 1487 | |
| 1488 | chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount); |
| 1489 | i = 0; |
| 1490 | chain = ip6tc_first_chain(handle); |
| 1491 | while (chain) { |
| 1492 | strcpy(chains + i*sizeof(ip6t_chainlabel), chain); |
| 1493 | i++; |
| 1494 | chain = ip6tc_next_chain(handle); |
| 1495 | } |
| 1496 | |
| 1497 | for (i = 0; i < chaincount; i++) { |
| 1498 | if (!builtinstoo |
| 1499 | && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel), |
Harald Welte | df1c71c | 2004-08-30 16:00:32 +0000 | [diff] [blame] | 1500 | *handle) == 1) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1501 | continue; |
| 1502 | ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle); |
| 1503 | } |
| 1504 | |
| 1505 | free(chains); |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1509 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1510 | flush_entries(const ip6t_chainlabel chain, int verbose, |
| 1511 | ip6tc_handle_t *handle) |
| 1512 | { |
| 1513 | if (!chain) |
| 1514 | return for_each_chain(flush_entries, verbose, 1, handle); |
| 1515 | |
| 1516 | if (verbose) |
| 1517 | fprintf(stdout, "Flushing chain `%s'\n", chain); |
| 1518 | return ip6tc_flush_entries(chain, handle); |
| 1519 | } |
| 1520 | |
| 1521 | static int |
| 1522 | zero_entries(const ip6t_chainlabel chain, int verbose, |
| 1523 | ip6tc_handle_t *handle) |
| 1524 | { |
| 1525 | if (!chain) |
| 1526 | return for_each_chain(zero_entries, verbose, 1, handle); |
| 1527 | |
| 1528 | if (verbose) |
| 1529 | fprintf(stdout, "Zeroing chain `%s'\n", chain); |
| 1530 | return ip6tc_zero_entries(chain, handle); |
| 1531 | } |
| 1532 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 1533 | int |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1534 | delete_chain(const ip6t_chainlabel chain, int verbose, |
| 1535 | ip6tc_handle_t *handle) |
| 1536 | { |
| 1537 | if (!chain) |
| 1538 | return for_each_chain(delete_chain, verbose, 0, handle); |
| 1539 | |
| 1540 | if (verbose) |
| 1541 | fprintf(stdout, "Deleting chain `%s'\n", chain); |
| 1542 | return ip6tc_delete_chain(chain, handle); |
| 1543 | } |
| 1544 | |
| 1545 | static int |
| 1546 | list_entries(const ip6t_chainlabel chain, int verbose, int numeric, |
| 1547 | int expanded, int linenumbers, ip6tc_handle_t *handle) |
| 1548 | { |
| 1549 | int found = 0; |
| 1550 | unsigned int format; |
| 1551 | const char *this; |
| 1552 | |
| 1553 | format = FMT_OPTIONS; |
| 1554 | if (!verbose) |
| 1555 | format |= FMT_NOCOUNTS; |
| 1556 | else |
| 1557 | format |= FMT_VIA; |
| 1558 | |
| 1559 | if (numeric) |
| 1560 | format |= FMT_NUMERIC; |
| 1561 | |
| 1562 | if (!expanded) |
| 1563 | format |= FMT_KILOMEGAGIGA; |
| 1564 | |
| 1565 | if (linenumbers) |
| 1566 | format |= FMT_LINENUMBERS; |
| 1567 | |
| 1568 | for (this = ip6tc_first_chain(handle); |
| 1569 | this; |
| 1570 | this = ip6tc_next_chain(handle)) { |
| 1571 | const struct ip6t_entry *i; |
| 1572 | unsigned int num; |
| 1573 | |
| 1574 | if (chain && strcmp(chain, this) != 0) |
| 1575 | continue; |
| 1576 | |
| 1577 | if (found) printf("\n"); |
| 1578 | |
| 1579 | print_header(format, this, handle); |
| 1580 | i = ip6tc_first_rule(this, handle); |
| 1581 | |
| 1582 | num = 0; |
| 1583 | while (i) { |
| 1584 | print_firewall(i, |
| 1585 | ip6tc_get_target(i, handle), |
| 1586 | num++, |
| 1587 | format, |
| 1588 | *handle); |
| 1589 | i = ip6tc_next_rule(i, handle); |
| 1590 | } |
| 1591 | found = 1; |
| 1592 | } |
| 1593 | |
| 1594 | errno = ENOENT; |
| 1595 | return found; |
| 1596 | } |
| 1597 | |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1598 | static char *get_modprobe(void) |
| 1599 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1600 | int procfile; |
| 1601 | char *ret; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1602 | |
Harald Welte | 10f7f14 | 2004-10-22 08:14:07 +0000 | [diff] [blame] | 1603 | #define PROCFILE_BUFSIZ 1024 |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1604 | procfile = open(PROC_SYS_MODPROBE, O_RDONLY); |
| 1605 | if (procfile < 0) |
| 1606 | return NULL; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1607 | |
Harald Welte | 10f7f14 | 2004-10-22 08:14:07 +0000 | [diff] [blame] | 1608 | ret = malloc(PROCFILE_BUFSIZ); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1609 | if (ret) { |
Harald Welte | 10f7f14 | 2004-10-22 08:14:07 +0000 | [diff] [blame] | 1610 | memset(ret, 0, PROCFILE_BUFSIZ); |
| 1611 | switch (read(procfile, ret, PROCFILE_BUFSIZ)) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1612 | case -1: goto fail; |
Harald Welte | 10f7f14 | 2004-10-22 08:14:07 +0000 | [diff] [blame] | 1613 | case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */ |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1614 | } |
| 1615 | if (ret[strlen(ret)-1]=='\n') |
| 1616 | ret[strlen(ret)-1]=0; |
| 1617 | close(procfile); |
| 1618 | return ret; |
| 1619 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1620 | fail: |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1621 | free(ret); |
| 1622 | close(procfile); |
| 1623 | return NULL; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 1626 | int ip6tables_insmod(const char *modname, const char *modprobe) |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1627 | { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1628 | char *buf = NULL; |
| 1629 | char *argv[3]; |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1630 | int status; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1631 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1632 | /* If they don't explicitly set it, read out of kernel */ |
| 1633 | if (!modprobe) { |
| 1634 | buf = get_modprobe(); |
| 1635 | if (!buf) |
| 1636 | return -1; |
| 1637 | modprobe = buf; |
| 1638 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1639 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1640 | switch (fork()) { |
| 1641 | case 0: |
| 1642 | argv[0] = (char *)modprobe; |
| 1643 | argv[1] = (char *)modname; |
| 1644 | argv[2] = NULL; |
| 1645 | execv(argv[0], argv); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1646 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1647 | /* not usually reached */ |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1648 | exit(1); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1649 | case -1: |
| 1650 | return -1; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1651 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1652 | default: /* parent */ |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1653 | wait(&status); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1654 | } |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1655 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1656 | free(buf); |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 1657 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
| 1658 | return 0; |
| 1659 | return -1; |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1662 | static struct ip6t_entry * |
| 1663 | generate_entry(const struct ip6t_entry *fw, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1664 | struct ip6tables_rule_match *matches, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1665 | struct ip6t_entry_target *target) |
| 1666 | { |
| 1667 | unsigned int size; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1668 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1669 | struct ip6t_entry *e; |
| 1670 | |
| 1671 | size = sizeof(struct ip6t_entry); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1672 | for (matchp = matches; matchp; matchp = matchp->next) |
| 1673 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1674 | |
| 1675 | e = fw_malloc(size + target->u.target_size); |
| 1676 | *e = *fw; |
| 1677 | e->target_offset = size; |
| 1678 | e->next_offset = size + target->u.target_size; |
| 1679 | |
| 1680 | size = 0; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1681 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 1682 | memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size); |
| 1683 | size += matchp->match->m->u.match_size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1684 | } |
| 1685 | memcpy(e->elems + size, target, target->u.target_size); |
| 1686 | |
| 1687 | return e; |
| 1688 | } |
| 1689 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1690 | void clear_rule_matches(struct ip6tables_rule_match **matches) |
| 1691 | { |
| 1692 | struct ip6tables_rule_match *matchp, *tmp; |
| 1693 | |
| 1694 | for (matchp = *matches; matchp;) { |
| 1695 | tmp = matchp->next; |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 1696 | if (matchp->match->m) |
| 1697 | free(matchp->match->m); |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1698 | free(matchp); |
| 1699 | matchp = tmp; |
| 1700 | } |
| 1701 | |
| 1702 | *matches = NULL; |
| 1703 | } |
| 1704 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1705 | int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle) |
| 1706 | { |
| 1707 | struct ip6t_entry fw, *e = NULL; |
| 1708 | int invert = 0; |
| 1709 | unsigned int nsaddrs = 0, ndaddrs = 0; |
| 1710 | struct in6_addr *saddrs = NULL, *daddrs = NULL; |
| 1711 | |
| 1712 | int c, verbose = 0; |
| 1713 | const char *chain = NULL; |
| 1714 | const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL; |
| 1715 | const char *policy = NULL, *newname = NULL; |
| 1716 | unsigned int rulenum = 0, options = 0, command = 0; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1717 | const char *pcnt = NULL, *bcnt = NULL; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1718 | int ret = 1; |
| 1719 | struct ip6tables_match *m; |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1720 | struct ip6tables_rule_match *matches = NULL; |
| 1721 | struct ip6tables_rule_match *matchp; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1722 | struct ip6tables_target *target = NULL; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1723 | struct ip6tables_target *t; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1724 | const char *jumpto = ""; |
| 1725 | char *protocol = NULL; |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1726 | const char *modprobe = NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 1727 | int proto_used = 0; |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 1728 | char icmp6p[] = "icmpv6"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1729 | |
| 1730 | memset(&fw, 0, sizeof(fw)); |
| 1731 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1732 | /* 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] | 1733 | * a second time */ |
| 1734 | optind = 0; |
| 1735 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1736 | /* clear mflags in case do_command gets called a second time |
| 1737 | * (we clear the global list of all matches for security)*/ |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1738 | for (m = ip6tables_matches; m; m = m->next) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1739 | m->mflags = 0; |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1740 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1741 | for (t = ip6tables_targets; t; t = t->next) { |
| 1742 | t->tflags = 0; |
| 1743 | t->used = 0; |
| 1744 | } |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1745 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1746 | /* Suppress error messages: we may add new options if we |
| 1747 | demand-load a protocol. */ |
| 1748 | opterr = 0; |
| 1749 | |
| 1750 | while ((c = getopt_long(argc, argv, |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 1751 | "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:", |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1752 | opts, NULL)) != -1) { |
| 1753 | switch (c) { |
| 1754 | /* |
| 1755 | * Command selection |
| 1756 | */ |
| 1757 | case 'A': |
| 1758 | add_command(&command, CMD_APPEND, CMD_NONE, |
| 1759 | invert); |
| 1760 | chain = optarg; |
| 1761 | break; |
| 1762 | |
| 1763 | case 'D': |
| 1764 | add_command(&command, CMD_DELETE, CMD_NONE, |
| 1765 | invert); |
| 1766 | chain = optarg; |
| 1767 | if (optind < argc && argv[optind][0] != '-' |
| 1768 | && argv[optind][0] != '!') { |
| 1769 | rulenum = parse_rulenumber(argv[optind++]); |
| 1770 | command = CMD_DELETE_NUM; |
| 1771 | } |
| 1772 | break; |
| 1773 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1774 | case 'R': |
| 1775 | add_command(&command, CMD_REPLACE, CMD_NONE, |
| 1776 | invert); |
| 1777 | chain = optarg; |
| 1778 | if (optind < argc && argv[optind][0] != '-' |
| 1779 | && argv[optind][0] != '!') |
| 1780 | rulenum = parse_rulenumber(argv[optind++]); |
| 1781 | else |
| 1782 | exit_error(PARAMETER_PROBLEM, |
| 1783 | "-%c requires a rule number", |
| 1784 | cmd2char(CMD_REPLACE)); |
| 1785 | break; |
| 1786 | |
| 1787 | case 'I': |
| 1788 | add_command(&command, CMD_INSERT, CMD_NONE, |
| 1789 | invert); |
| 1790 | chain = optarg; |
| 1791 | if (optind < argc && argv[optind][0] != '-' |
| 1792 | && argv[optind][0] != '!') |
| 1793 | rulenum = parse_rulenumber(argv[optind++]); |
| 1794 | else rulenum = 1; |
| 1795 | break; |
| 1796 | |
| 1797 | case 'L': |
| 1798 | add_command(&command, CMD_LIST, CMD_ZERO, |
| 1799 | invert); |
| 1800 | if (optarg) chain = optarg; |
| 1801 | else if (optind < argc && argv[optind][0] != '-' |
| 1802 | && argv[optind][0] != '!') |
| 1803 | chain = argv[optind++]; |
| 1804 | break; |
| 1805 | |
| 1806 | case 'F': |
| 1807 | add_command(&command, CMD_FLUSH, CMD_NONE, |
| 1808 | invert); |
| 1809 | if (optarg) chain = optarg; |
| 1810 | else if (optind < argc && argv[optind][0] != '-' |
| 1811 | && argv[optind][0] != '!') |
| 1812 | chain = argv[optind++]; |
| 1813 | break; |
| 1814 | |
| 1815 | case 'Z': |
| 1816 | add_command(&command, CMD_ZERO, CMD_LIST, |
| 1817 | invert); |
| 1818 | if (optarg) chain = optarg; |
| 1819 | else if (optind < argc && argv[optind][0] != '-' |
| 1820 | && argv[optind][0] != '!') |
| 1821 | chain = argv[optind++]; |
| 1822 | break; |
| 1823 | |
| 1824 | case 'N': |
Joszef Kadlecsik | 08f1527 | 2002-06-24 12:37:29 +0000 | [diff] [blame] | 1825 | if (optarg && *optarg == '-') |
| 1826 | exit_error(PARAMETER_PROBLEM, |
| 1827 | "chain name not allowed to start " |
| 1828 | "with `-'\n"); |
| 1829 | if (find_target(optarg, TRY_LOAD)) |
| 1830 | exit_error(PARAMETER_PROBLEM, |
| 1831 | "chain name may not clash " |
| 1832 | "with target name\n"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1833 | add_command(&command, CMD_NEW_CHAIN, CMD_NONE, |
| 1834 | invert); |
| 1835 | chain = optarg; |
| 1836 | break; |
| 1837 | |
| 1838 | case 'X': |
| 1839 | add_command(&command, CMD_DELETE_CHAIN, CMD_NONE, |
| 1840 | invert); |
| 1841 | if (optarg) chain = optarg; |
| 1842 | else if (optind < argc && argv[optind][0] != '-' |
| 1843 | && argv[optind][0] != '!') |
| 1844 | chain = argv[optind++]; |
| 1845 | break; |
| 1846 | |
| 1847 | case 'E': |
| 1848 | add_command(&command, CMD_RENAME_CHAIN, CMD_NONE, |
| 1849 | invert); |
| 1850 | chain = optarg; |
| 1851 | if (optind < argc && argv[optind][0] != '-' |
| 1852 | && argv[optind][0] != '!') |
| 1853 | newname = argv[optind++]; |
M.P.Anand Babu | c9f20d3 | 2000-06-09 09:22:38 +0000 | [diff] [blame] | 1854 | else |
| 1855 | exit_error(PARAMETER_PROBLEM, |
| 1856 | "-%c requires old-chain-name and " |
| 1857 | "new-chain-name", |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1858 | cmd2char(CMD_RENAME_CHAIN)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1859 | break; |
| 1860 | |
| 1861 | case 'P': |
| 1862 | add_command(&command, CMD_SET_POLICY, CMD_NONE, |
| 1863 | invert); |
| 1864 | chain = optarg; |
| 1865 | if (optind < argc && argv[optind][0] != '-' |
| 1866 | && argv[optind][0] != '!') |
| 1867 | policy = argv[optind++]; |
| 1868 | else |
| 1869 | exit_error(PARAMETER_PROBLEM, |
| 1870 | "-%c requires a chain and a policy", |
| 1871 | cmd2char(CMD_SET_POLICY)); |
| 1872 | break; |
| 1873 | |
| 1874 | case 'h': |
| 1875 | if (!optarg) |
| 1876 | optarg = argv[optind]; |
| 1877 | |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1878 | /* ip6tables -p icmp -h */ |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1879 | if (!matches && protocol) |
| 1880 | find_match(protocol, TRY_LOAD, &matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1881 | |
Martin Josefsson | 66aea6f | 2004-05-26 15:41:54 +0000 | [diff] [blame] | 1882 | exit_printhelp(matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1883 | |
| 1884 | /* |
| 1885 | * Option selection |
| 1886 | */ |
| 1887 | case 'p': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1888 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1889 | set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags, |
| 1890 | invert); |
| 1891 | |
| 1892 | /* Canonicalize into lower case */ |
| 1893 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 1894 | *protocol = tolower(*protocol); |
| 1895 | |
| 1896 | protocol = argv[optind-1]; |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 1897 | if ( strcmp(protocol,"ipv6-icmp") == 0) |
| 1898 | protocol = icmp6p; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1899 | fw.ipv6.proto = parse_protocol(protocol); |
| 1900 | fw.ipv6.flags |= IP6T_F_PROTO; |
| 1901 | |
| 1902 | if (fw.ipv6.proto == 0 |
| 1903 | && (fw.ipv6.invflags & IP6T_INV_PROTO)) |
| 1904 | exit_error(PARAMETER_PROBLEM, |
| 1905 | "rule would never match protocol"); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | case 's': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1909 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1910 | set_option(&options, OPT_SOURCE, &fw.ipv6.invflags, |
| 1911 | invert); |
| 1912 | shostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | case 'd': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1916 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1917 | set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags, |
| 1918 | invert); |
| 1919 | dhostnetworkmask = argv[optind-1]; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | |
| 1922 | case 'j': |
| 1923 | set_option(&options, OPT_JUMP, &fw.ipv6.invflags, |
| 1924 | invert); |
| 1925 | jumpto = parse_target(optarg); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1926 | /* TRY_LOAD (may be chain name) */ |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1927 | target = find_target(jumpto, TRY_LOAD); |
| 1928 | |
| 1929 | if (target) { |
| 1930 | size_t size; |
| 1931 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1932 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) |
| 1933 | + target->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1934 | |
| 1935 | target->t = fw_calloc(1, size); |
| 1936 | target->t->u.target_size = size; |
| 1937 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1938 | if (target->init != NULL) |
| 1939 | target->init(target->t, &fw.nfcache); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1940 | opts = merge_options(opts, target->extra_opts, &target->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1941 | } |
| 1942 | break; |
| 1943 | |
| 1944 | |
| 1945 | case 'i': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1946 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1947 | set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags, |
| 1948 | invert); |
| 1949 | parse_interface(argv[optind-1], |
| 1950 | fw.ipv6.iniface, |
| 1951 | fw.ipv6.iniface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | |
| 1954 | case 'o': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 1955 | check_inverse(optarg, &invert, &optind, argc); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1956 | set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags, |
| 1957 | invert); |
| 1958 | parse_interface(argv[optind-1], |
| 1959 | fw.ipv6.outiface, |
| 1960 | fw.ipv6.outiface_mask); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1961 | break; |
| 1962 | |
| 1963 | case 'v': |
| 1964 | if (!verbose) |
| 1965 | set_option(&options, OPT_VERBOSE, |
| 1966 | &fw.ipv6.invflags, invert); |
| 1967 | verbose++; |
| 1968 | break; |
| 1969 | |
| 1970 | case 'm': { |
| 1971 | size_t size; |
| 1972 | |
| 1973 | if (invert) |
| 1974 | exit_error(PARAMETER_PROBLEM, |
| 1975 | "unexpected ! flag before --match"); |
| 1976 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 1977 | m = find_match(optarg, LOAD_MUST_SUCCEED, &matches); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 1978 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 1979 | + m->size; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1980 | m->m = fw_calloc(1, size); |
| 1981 | m->m->u.match_size = size; |
| 1982 | strcpy(m->m->u.user.name, m->name); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 1983 | if (m->init != NULL) |
| 1984 | m->init(m->m, &fw.nfcache); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 1985 | opts = merge_options(opts, m->extra_opts, &m->option_offset); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 1986 | } |
| 1987 | break; |
| 1988 | |
| 1989 | case 'n': |
| 1990 | set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags, |
| 1991 | invert); |
| 1992 | break; |
| 1993 | |
| 1994 | case 't': |
| 1995 | if (invert) |
| 1996 | exit_error(PARAMETER_PROBLEM, |
| 1997 | "unexpected ! flag before --table"); |
| 1998 | *table = argv[optind-1]; |
| 1999 | break; |
| 2000 | |
| 2001 | case 'x': |
| 2002 | set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags, |
| 2003 | invert); |
| 2004 | break; |
| 2005 | |
| 2006 | case 'V': |
| 2007 | if (invert) |
| 2008 | printf("Not %s ;-)\n", program_version); |
| 2009 | else |
| 2010 | printf("%s v%s\n", |
| 2011 | program_name, program_version); |
| 2012 | exit(0); |
| 2013 | |
| 2014 | case '0': |
| 2015 | set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags, |
| 2016 | invert); |
| 2017 | break; |
| 2018 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2019 | case 'M': |
| 2020 | modprobe = optarg; |
| 2021 | break; |
| 2022 | |
| 2023 | case 'c': |
| 2024 | |
| 2025 | set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags, |
| 2026 | invert); |
| 2027 | pcnt = optarg; |
| 2028 | if (optind < argc && argv[optind][0] != '-' |
| 2029 | && argv[optind][0] != '!') |
| 2030 | bcnt = argv[optind++]; |
| 2031 | else |
| 2032 | exit_error(PARAMETER_PROBLEM, |
| 2033 | "-%c requires packet and byte counter", |
| 2034 | opt2char(OPT_COUNTERS)); |
| 2035 | |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 2036 | if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2037 | exit_error(PARAMETER_PROBLEM, |
| 2038 | "-%c packet counter not numeric", |
| 2039 | opt2char(OPT_COUNTERS)); |
| 2040 | |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 2041 | if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2042 | exit_error(PARAMETER_PROBLEM, |
| 2043 | "-%c byte counter not numeric", |
| 2044 | opt2char(OPT_COUNTERS)); |
| 2045 | |
| 2046 | break; |
| 2047 | |
| 2048 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2049 | case 1: /* non option */ |
| 2050 | if (optarg[0] == '!' && optarg[1] == '\0') { |
| 2051 | if (invert) |
| 2052 | exit_error(PARAMETER_PROBLEM, |
| 2053 | "multiple consecutive ! not" |
| 2054 | " allowed"); |
| 2055 | invert = TRUE; |
| 2056 | optarg[0] = '\0'; |
| 2057 | continue; |
| 2058 | } |
| 2059 | printf("Bad argument `%s'\n", optarg); |
| 2060 | exit_tryhelp(2); |
| 2061 | |
| 2062 | default: |
| 2063 | /* FIXME: This scheme doesn't allow two of the same |
| 2064 | matches --RR */ |
| 2065 | if (!target |
| 2066 | || !(target->parse(c - target->option_offset, |
| 2067 | argv, invert, |
| 2068 | &target->tflags, |
| 2069 | &fw, &target->t))) { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2070 | for (matchp = matches; matchp; matchp = matchp->next) { |
| 2071 | if (matchp->match->parse(c - matchp->match->option_offset, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2072 | argv, invert, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2073 | &matchp->match->mflags, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2074 | &fw, |
| 2075 | &fw.nfcache, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2076 | &matchp->match->m)) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2077 | break; |
| 2078 | } |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2079 | m = matchp ? matchp->match : NULL; |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2080 | |
| 2081 | /* If you listen carefully, you can |
| 2082 | actually hear this code suck. */ |
| 2083 | |
| 2084 | /* some explanations (after four different bugs |
| 2085 | * in 3 different releases): If we encountere a |
| 2086 | * parameter, that has not been parsed yet, |
| 2087 | * it's not an option of an explicitly loaded |
| 2088 | * match or a target. However, we support |
| 2089 | * implicit loading of the protocol match |
| 2090 | * extension. '-p tcp' means 'l4 proto 6' and |
| 2091 | * at the same time 'load tcp protocol match on |
| 2092 | * demand if we specify --dport'. |
| 2093 | * |
| 2094 | * To make this work, we need to make sure: |
| 2095 | * - the parameter has not been parsed by |
| 2096 | * a match (m above) |
| 2097 | * - a protocol has been specified |
| 2098 | * - the protocol extension has not been |
| 2099 | * loaded yet, or is loaded and unused |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 2100 | * [think of ip6tables-restore!] |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2101 | * - the protocol extension can be successively |
| 2102 | * loaded |
| 2103 | */ |
| 2104 | if (m == NULL |
| 2105 | && protocol |
| 2106 | && (!find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2107 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2108 | || (find_proto(protocol, DONT_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2109 | options&OPT_NUMERIC, NULL) |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2110 | && (proto_used == 0)) |
| 2111 | ) |
| 2112 | && (m = find_proto(protocol, TRY_LOAD, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2113 | options&OPT_NUMERIC, &matches))) { |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2114 | /* Try loading protocol */ |
| 2115 | size_t size; |
| 2116 | |
| 2117 | proto_used = 1; |
| 2118 | |
| 2119 | size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) |
| 2120 | + m->size; |
| 2121 | |
| 2122 | m->m = fw_calloc(1, size); |
| 2123 | m->m->u.match_size = size; |
| 2124 | strcpy(m->m->u.user.name, m->name); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 2125 | if (m->init != NULL) |
| 2126 | m->init(m->m, &fw.nfcache); |
Harald Welte | 00f5a9c | 2002-08-26 14:37:35 +0000 | [diff] [blame] | 2127 | |
| 2128 | opts = merge_options(opts, |
| 2129 | m->extra_opts, &m->option_offset); |
| 2130 | |
| 2131 | optind--; |
| 2132 | continue; |
| 2133 | } |
| 2134 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2135 | if (!m) |
| 2136 | exit_error(PARAMETER_PROBLEM, |
| 2137 | "Unknown arg `%s'", |
| 2138 | argv[optind-1]); |
| 2139 | } |
| 2140 | } |
| 2141 | invert = FALSE; |
| 2142 | } |
| 2143 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2144 | for (matchp = matches; matchp; matchp = matchp->next) |
| 2145 | matchp->match->final_check(matchp->match->mflags); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 2146 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2147 | if (target) |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2148 | target->final_check(target->tflags); |
| 2149 | |
| 2150 | /* Fix me: must put inverse options checking here --MN */ |
| 2151 | |
| 2152 | if (optind < argc) |
| 2153 | exit_error(PARAMETER_PROBLEM, |
| 2154 | "unknown arguments found on commandline"); |
| 2155 | if (!command) |
| 2156 | exit_error(PARAMETER_PROBLEM, "no command specified"); |
| 2157 | if (invert) |
| 2158 | exit_error(PARAMETER_PROBLEM, |
| 2159 | "nothing appropriate following !"); |
| 2160 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 2161 | if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) { |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2162 | if (!(options & OPT_DESTINATION)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 2163 | dhostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2164 | if (!(options & OPT_SOURCE)) |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 2165 | shostnetworkmask = "::0/0"; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | if (shostnetworkmask) |
| 2169 | parse_hostnetworkmask(shostnetworkmask, &saddrs, |
| 2170 | &(fw.ipv6.smsk), &nsaddrs); |
| 2171 | |
| 2172 | if (dhostnetworkmask) |
| 2173 | parse_hostnetworkmask(dhostnetworkmask, &daddrs, |
| 2174 | &(fw.ipv6.dmsk), &ndaddrs); |
| 2175 | |
| 2176 | if ((nsaddrs > 1 || ndaddrs > 1) && |
| 2177 | (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP))) |
| 2178 | exit_error(PARAMETER_PROBLEM, "! not allowed with multiple" |
| 2179 | " source or destination IP addresses"); |
| 2180 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2181 | if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1)) |
| 2182 | exit_error(PARAMETER_PROBLEM, "Replacement rule does not " |
| 2183 | "specify a unique address"); |
| 2184 | |
| 2185 | generic_opt_check(command, options); |
| 2186 | |
| 2187 | if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN) |
| 2188 | exit_error(PARAMETER_PROBLEM, |
| 2189 | "chain name `%s' too long (must be under %i chars)", |
| 2190 | chain, IP6T_FUNCTION_MAXNAMELEN); |
| 2191 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2192 | /* only allocate handle if we weren't called with a handle */ |
Martin Josefsson | 8371e15 | 2003-05-05 19:33:40 +0000 | [diff] [blame] | 2193 | if (!*handle) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2194 | *handle = ip6tc_init(*table); |
András Kis-Szabó | 3aa6287 | 2001-05-03 01:01:41 +0000 | [diff] [blame] | 2195 | |
Rusty Russell | 8beb049 | 2004-12-22 00:37:10 +0000 | [diff] [blame] | 2196 | /* try to insmod the module if iptc_init failed */ |
| 2197 | if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1) |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2198 | *handle = ip6tc_init(*table); |
Fabrice MARIE | 8a5eb6d | 2001-05-05 21:37:47 +0000 | [diff] [blame] | 2199 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2200 | if (!*handle) |
| 2201 | exit_error(VERSION_PROBLEM, |
| 2202 | "can't initialize ip6tables table `%s': %s", |
| 2203 | *table, ip6tc_strerror(errno)); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2204 | |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 2205 | if (command == CMD_APPEND |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2206 | || command == CMD_DELETE |
| 2207 | || command == CMD_INSERT |
| 2208 | || command == CMD_REPLACE) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2209 | if (strcmp(chain, "PREROUTING") == 0 |
| 2210 | || strcmp(chain, "INPUT") == 0) { |
| 2211 | /* -o not valid with incoming packets. */ |
| 2212 | if (options & OPT_VIANAMEOUT) |
| 2213 | exit_error(PARAMETER_PROBLEM, |
| 2214 | "Can't use -%c with %s\n", |
| 2215 | opt2char(OPT_VIANAMEOUT), |
| 2216 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2217 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2218 | |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2219 | if (strcmp(chain, "POSTROUTING") == 0 |
| 2220 | || strcmp(chain, "OUTPUT") == 0) { |
| 2221 | /* -i not valid with outgoing packets */ |
| 2222 | if (options & OPT_VIANAMEIN) |
| 2223 | exit_error(PARAMETER_PROBLEM, |
| 2224 | "Can't use -%c with %s\n", |
| 2225 | opt2char(OPT_VIANAMEIN), |
| 2226 | chain); |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2227 | } |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2228 | |
| 2229 | if (target && ip6tc_is_chain(jumpto, *handle)) { |
| 2230 | printf("Warning: using chain %s, not extension\n", |
| 2231 | jumpto); |
| 2232 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2233 | if (target->t) |
| 2234 | free(target->t); |
| 2235 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2236 | target = NULL; |
| 2237 | } |
| 2238 | |
| 2239 | /* If they didn't specify a target, or it's a chain |
| 2240 | name, use standard. */ |
| 2241 | if (!target |
| 2242 | && (strlen(jumpto) == 0 |
| 2243 | || ip6tc_is_chain(jumpto, *handle))) { |
| 2244 | size_t size; |
| 2245 | |
| 2246 | target = find_target(IP6T_STANDARD_TARGET, |
| 2247 | LOAD_MUST_SUCCEED); |
| 2248 | |
| 2249 | size = sizeof(struct ip6t_entry_target) |
| 2250 | + target->size; |
| 2251 | target->t = fw_calloc(1, size); |
| 2252 | target->t->u.target_size = size; |
| 2253 | strcpy(target->t->u.user.name, jumpto); |
Jonas Berlin | 1b91e59 | 2005-04-01 06:58:38 +0000 | [diff] [blame] | 2254 | if (target->init != NULL) |
| 2255 | target->init(target->t, &fw.nfcache); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | if (!target) { |
Harald Welte | 43ac191 | 2002-03-03 09:43:07 +0000 | [diff] [blame] | 2259 | /* it is no chain, and we can't load a plugin. |
| 2260 | * We cannot know if the plugin is corrupt, non |
| 2261 | * existant OR if the user just misspelled a |
| 2262 | * chain. */ |
| 2263 | find_target(jumpto, LOAD_MUST_SUCCEED); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2264 | } else { |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2265 | e = generate_entry(&fw, matches, target->t); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2266 | free(target->t); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | switch (command) { |
| 2271 | case CMD_APPEND: |
| 2272 | ret = append_entry(chain, e, |
| 2273 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 2274 | options&OPT_VERBOSE, |
| 2275 | handle); |
| 2276 | break; |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2277 | case CMD_DELETE: |
| 2278 | ret = delete_entry(chain, e, |
| 2279 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 2280 | options&OPT_VERBOSE, |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2281 | handle, matches); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2282 | break; |
| 2283 | case CMD_DELETE_NUM: |
| 2284 | ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle); |
| 2285 | break; |
| 2286 | case CMD_REPLACE: |
| 2287 | ret = replace_entry(chain, e, rulenum - 1, |
| 2288 | saddrs, daddrs, options&OPT_VERBOSE, |
| 2289 | handle); |
| 2290 | break; |
| 2291 | case CMD_INSERT: |
| 2292 | ret = insert_entry(chain, e, rulenum - 1, |
| 2293 | nsaddrs, saddrs, ndaddrs, daddrs, |
| 2294 | options&OPT_VERBOSE, |
| 2295 | handle); |
| 2296 | break; |
| 2297 | case CMD_LIST: |
| 2298 | ret = list_entries(chain, |
| 2299 | options&OPT_VERBOSE, |
| 2300 | options&OPT_NUMERIC, |
| 2301 | options&OPT_EXPANDED, |
| 2302 | options&OPT_LINENUMBERS, |
| 2303 | handle); |
| 2304 | break; |
| 2305 | case CMD_FLUSH: |
| 2306 | ret = flush_entries(chain, options&OPT_VERBOSE, handle); |
| 2307 | break; |
| 2308 | case CMD_ZERO: |
| 2309 | ret = zero_entries(chain, options&OPT_VERBOSE, handle); |
| 2310 | break; |
| 2311 | case CMD_LIST|CMD_ZERO: |
| 2312 | ret = list_entries(chain, |
| 2313 | options&OPT_VERBOSE, |
| 2314 | options&OPT_NUMERIC, |
| 2315 | options&OPT_EXPANDED, |
| 2316 | options&OPT_LINENUMBERS, |
| 2317 | handle); |
| 2318 | if (ret) |
| 2319 | ret = zero_entries(chain, |
| 2320 | options&OPT_VERBOSE, handle); |
| 2321 | break; |
| 2322 | case CMD_NEW_CHAIN: |
| 2323 | ret = ip6tc_create_chain(chain, handle); |
| 2324 | break; |
| 2325 | case CMD_DELETE_CHAIN: |
| 2326 | ret = delete_chain(chain, options&OPT_VERBOSE, handle); |
| 2327 | break; |
| 2328 | case CMD_RENAME_CHAIN: |
| 2329 | ret = ip6tc_rename_chain(chain, newname, handle); |
| 2330 | break; |
| 2331 | case CMD_SET_POLICY: |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 2332 | ret = ip6tc_set_policy(chain, policy, NULL, handle); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2333 | break; |
| 2334 | default: |
| 2335 | /* We should never reach this... */ |
| 2336 | exit_tryhelp(2); |
| 2337 | } |
| 2338 | |
| 2339 | if (verbose > 1) |
| 2340 | dump_entries6(*handle); |
| 2341 | |
Martin Josefsson | 69ac0e0 | 2004-02-02 20:02:10 +0000 | [diff] [blame] | 2342 | clear_rule_matches(&matches); |
| 2343 | |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2344 | if (e != NULL) { |
| 2345 | free(e); |
| 2346 | e = NULL; |
| 2347 | } |
| 2348 | |
| 2349 | for (c = 0; c < nsaddrs; c++) |
| 2350 | free(&saddrs[c]); |
| 2351 | |
| 2352 | for (c = 0; c < ndaddrs; c++) |
| 2353 | free(&daddrs[c]); |
| 2354 | |
Pablo Neira | dfdcd64 | 2005-05-29 19:05:23 +0000 | [diff] [blame^] | 2355 | free_opts(1); |
Martin Josefsson | 4dd5fed | 2004-05-18 18:09:43 +0000 | [diff] [blame] | 2356 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 2357 | return ret; |
| 2358 | } |