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