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