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