András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 1 | /* Code to save the ip6tables state, in human readable-form. */ |
| 2 | /* Author: Andras Kis-Szabo <kisza@sch.bme.hu> |
| 3 | * Original code: iptables-save |
| 4 | * Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and |
| 5 | * Harald Welte <laforge@gnumonks.org> |
| 6 | */ |
| 7 | #include <getopt.h> |
| 8 | #include <sys/errno.h> |
| 9 | #include <stdio.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <dlfcn.h> |
| 14 | #include <time.h> |
| 15 | #include <netdb.h> |
| 16 | #include <arpa/inet.h> |
| 17 | #include "libiptc/libip6tc.h" |
| 18 | #include "ip6tables.h" |
| 19 | |
| 20 | #ifndef IP6T_LIB_DIR |
| 21 | #define IP6T_LIB_DIR "/usr/local/lib/iptables" |
| 22 | #endif |
| 23 | |
| 24 | static int binary = 0, counters = 0; |
| 25 | |
| 26 | static struct option options[] = { |
| 27 | { "binary", 0, 0, 'b' }, |
| 28 | { "counters", 0, 0, 'c' }, |
| 29 | { "dump", 0, 0, 'd' }, |
| 30 | { "table", 1, 0, 't' }, |
| 31 | { 0 } |
| 32 | }; |
| 33 | |
| 34 | extern struct ip6tables_match *find_match(const char *name, enum ip6t_tryload tryload); |
| 35 | extern struct ip6tables_target *find_target(const char *name, enum ip6t_tryload tryload); |
| 36 | |
| 37 | /* This assumes that mask is contiguous, and byte-bounded. */ |
| 38 | static void |
| 39 | print_iface(char letter, const char *iface, const unsigned char *mask, |
| 40 | int invert) |
| 41 | { |
| 42 | unsigned int i; |
| 43 | |
| 44 | if (mask[0] == 0) |
| 45 | return; |
| 46 | |
| 47 | printf("-%c %s", letter, invert ? "! " : ""); |
| 48 | |
| 49 | for (i = 0; i < IFNAMSIZ; i++) { |
| 50 | if (mask[i] != 0) { |
| 51 | if (iface[i] != '\0') |
| 52 | printf("%c", iface[i]); |
| 53 | } else { |
Harald Welte | 6a4542a | 2001-05-12 04:38:31 +0000 | [diff] [blame] | 54 | if (iface[i] == '\0') |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 55 | printf("+"); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | printf(" "); |
| 61 | } |
| 62 | |
| 63 | /* These are hardcoded backups in ip6tables.c, so they are safe */ |
| 64 | struct pprot { |
| 65 | char *name; |
| 66 | u_int8_t num; |
| 67 | }; |
| 68 | |
| 69 | static const struct pprot chain_protos[] = { |
| 70 | { "tcp", IPPROTO_TCP }, |
| 71 | { "udp", IPPROTO_UDP }, |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 72 | { "icmpv6", IPPROTO_ICMPV6 }, |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 73 | { "esp", IPPROTO_ESP }, |
| 74 | { "ah", IPPROTO_AH }, |
| 75 | }; |
| 76 | |
| 77 | /* The ip6tables looks up the /etc/protocols. */ |
| 78 | static void print_proto(u_int16_t proto, int invert) |
| 79 | { |
| 80 | if (proto) { |
| 81 | unsigned int i; |
| 82 | const char *invertstr = invert ? "! " : ""; |
| 83 | |
| 84 | struct protoent *pent = getprotobynumber(proto); |
| 85 | if (pent) { |
| 86 | printf("-p %s%s ", |
| 87 | invertstr, pent->p_name); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++) |
| 92 | if (chain_protos[i].num == proto) { |
| 93 | printf("-p %s%s ", |
| 94 | invertstr, chain_protos[i].name); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | printf("-p %s%u ", invertstr, proto); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static int print_match(const struct ip6t_entry_match *e, |
| 103 | const struct ip6t_ip6 *ip) |
| 104 | { |
| 105 | struct ip6tables_match *match |
| 106 | = find_match(e->u.user.name, TRY_LOAD); |
| 107 | |
| 108 | if (match) { |
| 109 | printf("-m %s ", e->u.user.name); |
| 110 | |
| 111 | /* some matches don't provide a save function */ |
| 112 | if (match->save) |
| 113 | match->save(ip, e); |
| 114 | } else { |
| 115 | if (e->u.match_size) { |
| 116 | fprintf(stderr, |
| 117 | "Can't find library for match `%s'\n", |
| 118 | e->u.user.name); |
| 119 | exit(1); |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* print a given ip including mask if neccessary */ |
| 126 | static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert) |
| 127 | { |
| 128 | char buf[51]; |
| 129 | int l = ipv6_prefix_length(mask); |
| 130 | |
| 131 | if (!mask && !ip) |
| 132 | return; |
| 133 | |
| 134 | printf("%s %s%s/", |
| 135 | prefix, |
| 136 | invert ? "! " : "", |
| 137 | inet_ntop(AF_INET6, ip, buf, sizeof buf)); |
| 138 | |
| 139 | if (l == -1) |
| 140 | printf("%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf)); |
| 141 | else |
| 142 | printf("%d ", l); |
| 143 | |
| 144 | #if 0 |
| 145 | if (mask != 0xffffffff) |
| 146 | printf("/%u.%u.%u.%u ", IP_PARTS(mask)); |
| 147 | else |
| 148 | printf(" "); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | /* We want this to be readable, so only print out neccessary fields. |
| 153 | * Because that's the kind of world I want to live in. */ |
| 154 | static void print_rule(const struct ip6t_entry *e, |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 155 | ip6tc_handle_t *h, const char *chain, int counters) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 156 | { |
| 157 | struct ip6t_entry_target *t; |
Harald Welte | ace8a01 | 2001-07-05 06:29:10 +0000 | [diff] [blame] | 158 | const char *target_name; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 159 | |
| 160 | /* print counters */ |
| 161 | if (counters) |
| 162 | printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt); |
| 163 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 164 | /* print chain name */ |
| 165 | printf("-A %s ", chain); |
| 166 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 167 | /* Print IP part. */ |
| 168 | print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk), |
| 169 | e->ipv6.invflags & IP6T_INV_SRCIP); |
| 170 | |
| 171 | print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk), |
| 172 | e->ipv6.invflags & IP6T_INV_DSTIP); |
| 173 | |
| 174 | print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask, |
| 175 | e->ipv6.invflags & IP6T_INV_VIA_IN); |
| 176 | |
| 177 | print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask, |
| 178 | e->ipv6.invflags & IP6T_INV_VIA_OUT); |
| 179 | |
| 180 | print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO); |
| 181 | |
| 182 | #if 0 |
| 183 | // not definied in ipv6 |
| 184 | // FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? |
| 185 | if (e->ipv6.flags & IPT_F_FRAG) |
| 186 | printf("%s-f ", |
| 187 | e->ipv6.invflags & IP6T_INV_FRAG ? "! " : ""); |
| 188 | #endif |
| 189 | |
| 190 | // TODO: i've got some problem with the code - under understanding ;) |
| 191 | // How can I set this? |
| 192 | if (e->ipv6.flags & IP6T_F_TOS) |
| 193 | printf("%s-? %d ", |
| 194 | e->ipv6.invflags & IP6T_INV_TOS ? "! " : "", |
| 195 | e->ipv6.tos); |
| 196 | |
| 197 | /* Print matchinfo part */ |
| 198 | if (e->target_offset) { |
| 199 | IP6T_MATCH_ITERATE(e, print_match, &e->ipv6); |
| 200 | } |
| 201 | |
| 202 | /* Print target name */ |
Harald Welte | ace8a01 | 2001-07-05 06:29:10 +0000 | [diff] [blame] | 203 | target_name = ip6tc_get_target(e, h); |
| 204 | if (target_name && *target_name != '\0') |
| 205 | printf("-j %s ", ip6tc_get_target(e, h)); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 206 | |
| 207 | /* Print targinfo part */ |
| 208 | t = ip6t_get_target((struct ip6t_entry *)e); |
| 209 | if (t->u.user.name[0]) { |
| 210 | struct ip6tables_target *target |
| 211 | = find_target(t->u.user.name, TRY_LOAD); |
| 212 | |
| 213 | if (target) |
| 214 | target->save(&e->ipv6, t); |
| 215 | else { |
| 216 | /* If some bits are non-zero, it implies we *need* |
| 217 | to understand it */ |
| 218 | if (t->u.target_size) { |
| 219 | fprintf(stderr, |
| 220 | "Can't find library for target `%s'\n", |
| 221 | t->u.user.name); |
| 222 | exit(1); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | printf("\n"); |
| 227 | } |
| 228 | |
| 229 | /* Debugging prototype. */ |
| 230 | static int for_each_table(int (*func)(const char *tablename)) |
| 231 | { |
| 232 | int ret = 1; |
| 233 | FILE *procfile = NULL; |
| 234 | char tablename[IP6T_TABLE_MAXNAMELEN+1]; |
| 235 | |
| 236 | procfile = fopen("/proc/net/ip6_tables_names", "r"); |
| 237 | if (!procfile) |
| 238 | return 0; |
| 239 | |
| 240 | while (fgets(tablename, sizeof(tablename), procfile)) { |
| 241 | if (tablename[strlen(tablename) - 1] != '\n') |
| 242 | exit_error(OTHER_PROBLEM, |
| 243 | "Badly formed tablename `%s'\n", |
| 244 | tablename); |
| 245 | tablename[strlen(tablename) - 1] = '\0'; |
| 246 | ret &= func(tablename); |
| 247 | } |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static int do_output(const char *tablename) |
| 254 | { |
| 255 | ip6tc_handle_t h; |
| 256 | const char *chain = NULL; |
| 257 | |
| 258 | if (!tablename) |
| 259 | return for_each_table(&do_output); |
| 260 | |
| 261 | h = ip6tc_init(tablename); |
| 262 | if (!h) |
| 263 | exit_error(OTHER_PROBLEM, "Can't initialize: %s\n", |
| 264 | ip6tc_strerror(errno)); |
| 265 | |
| 266 | if (!binary) { |
| 267 | time_t now = time(NULL); |
| 268 | |
| 269 | printf("# Generated by ip6tables-save v%s on %s", |
| 270 | NETFILTER_VERSION, ctime(&now)); |
| 271 | printf("*%s\n", tablename); |
| 272 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 273 | /* Dump out chain names first, |
| 274 | * thereby preventing dependency conflicts */ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 275 | for (chain = ip6tc_first_chain(&h); |
| 276 | chain; |
| 277 | chain = ip6tc_next_chain(&h)) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 278 | |
| 279 | printf(":%s ", chain); |
| 280 | if (ip6tc_builtin(chain, h)) { |
| 281 | struct ip6t_counters count; |
| 282 | printf("%s ", |
| 283 | ip6tc_get_policy(chain, &count, &h)); |
| 284 | printf("[%llu:%llu]\n", count.pcnt, count.bcnt); |
| 285 | } else { |
| 286 | printf("- [0:0]\n"); |
| 287 | } |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | for (chain = ip6tc_first_chain(&h); |
| 291 | chain; |
| 292 | chain = ip6tc_next_chain(&h)) { |
| 293 | const struct ip6t_entry *e; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 294 | |
| 295 | /* Dump out rules */ |
| 296 | e = ip6tc_first_rule(chain, &h); |
| 297 | while(e) { |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 298 | print_rule(e, &h, chain, counters); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 299 | e = ip6tc_next_rule(e, &h); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | now = time(NULL); |
| 304 | printf("COMMIT\n"); |
| 305 | printf("# Completed on %s", ctime(&now)); |
| 306 | } else { |
| 307 | /* Binary, huh? OK. */ |
| 308 | exit_error(OTHER_PROBLEM, "Binary NYI\n"); |
| 309 | } |
| 310 | |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | /* Format: |
| 315 | * :Chain name POLICY packets bytes |
| 316 | * rule |
| 317 | */ |
| 318 | int main(int argc, char *argv[]) |
| 319 | { |
| 320 | const char *tablename = NULL; |
| 321 | int c; |
| 322 | |
| 323 | program_name = "ip6tables-save"; |
| 324 | program_version = NETFILTER_VERSION; |
| 325 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 326 | #ifdef NO_SHARED_LIBS |
| 327 | init_extensions(); |
| 328 | #endif |
| 329 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 330 | while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) { |
| 331 | switch (c) { |
| 332 | case 'b': |
| 333 | binary = 1; |
| 334 | break; |
| 335 | |
| 336 | case 'c': |
| 337 | counters = 1; |
| 338 | break; |
| 339 | |
| 340 | case 't': |
| 341 | /* Select specific table. */ |
| 342 | tablename = optarg; |
| 343 | break; |
| 344 | |
| 345 | case 'd': |
| 346 | do_output(tablename); |
| 347 | exit(0); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (optind < argc) { |
| 352 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 353 | exit(1); |
| 354 | } |
| 355 | |
| 356 | return !do_output(tablename); |
| 357 | } |