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