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