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