Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Code to save the iptables state, in human readable-form. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 2 | /* Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and |
| 3 | * Harald Welte <laforge@gnumonks.org> |
| 4 | */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 5 | #include <getopt.h> |
| 6 | #include <sys/errno.h> |
| 7 | #include <stdio.h> |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 11 | #include <dlfcn.h> |
| 12 | #include <time.h> |
Rusty Russell | b1f69be | 2000-08-27 07:42:54 +0000 | [diff] [blame] | 13 | #include "libiptc/libiptc.h" |
| 14 | #include "iptables.h" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 16 | static int binary = 0, counters = 0; |
| 17 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 18 | static struct option options[] = { |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 19 | { "binary", 0, 0, 'b' }, |
| 20 | { "counters", 0, 0, 'c' }, |
| 21 | { "dump", 0, 0, 'd' }, |
| 22 | { "table", 1, 0, 't' }, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 23 | { 0 } |
| 24 | }; |
| 25 | |
| 26 | #define IP_PARTS_NATIVE(n) \ |
| 27 | (unsigned int)((n)>>24)&0xFF, \ |
| 28 | (unsigned int)((n)>>16)&0xFF, \ |
| 29 | (unsigned int)((n)>>8)&0xFF, \ |
| 30 | (unsigned int)((n)&0xFF) |
| 31 | |
| 32 | #define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n)) |
| 33 | |
| 34 | /* This assumes that mask is contiguous, and byte-bounded. */ |
| 35 | static void |
| 36 | print_iface(char letter, const char *iface, const unsigned char *mask, |
| 37 | int invert) |
| 38 | { |
| 39 | unsigned int i; |
| 40 | |
| 41 | if (mask[0] == 0) |
| 42 | return; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 43 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 44 | printf("-%c %s", letter, invert ? "! " : ""); |
| 45 | |
| 46 | for (i = 0; i < IFNAMSIZ; i++) { |
| 47 | if (mask[i] != 0) { |
| 48 | if (iface[i] != '\0') |
| 49 | printf("%c", iface[i]); |
| 50 | } else { |
| 51 | if (iface[i] != '\0') |
| 52 | printf("+"); |
| 53 | break; |
| 54 | } |
| 55 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 56 | |
| 57 | printf(" "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* These are hardcoded backups in iptables.c, so they are safe */ |
| 61 | struct pprot { |
| 62 | char *name; |
| 63 | u_int8_t num; |
| 64 | }; |
| 65 | |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 66 | /* FIXME: why don't we use /etc/protocols ? */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 67 | static const struct pprot chain_protos[] = { |
| 68 | { "tcp", IPPROTO_TCP }, |
| 69 | { "udp", IPPROTO_UDP }, |
| 70 | { "icmp", IPPROTO_ICMP }, |
András Kis-Szabó | 764316a | 2001-02-26 17:31:20 +0000 | [diff] [blame] | 71 | { "esp", IPPROTO_ESP }, |
| 72 | { "ah", IPPROTO_AH }, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static void print_proto(u_int16_t proto, int invert) |
| 76 | { |
| 77 | if (proto) { |
| 78 | unsigned int i; |
| 79 | const char *invertstr = invert ? "! " : ""; |
| 80 | |
| 81 | for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++) |
| 82 | if (chain_protos[i].num == proto) { |
| 83 | printf("-p %s%s ", |
| 84 | invertstr, chain_protos[i].name); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | printf("-p %s%u ", invertstr, proto); |
| 89 | } |
| 90 | } |
| 91 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 92 | #if 0 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 93 | static int non_zero(const void *ptr, size_t size) |
| 94 | { |
| 95 | unsigned int i; |
| 96 | |
| 97 | for (i = 0; i < size; i++) |
| 98 | if (((char *)ptr)[i]) |
| 99 | return 0; |
| 100 | |
| 101 | return 1; |
| 102 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |
Harald Welte | 32db524 | 2001-01-23 22:46:22 +0000 | [diff] [blame] | 105 | static int print_match(const struct ipt_entry_match *e, |
| 106 | const struct ipt_ip *ip) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 107 | { |
| 108 | struct iptables_match *match |
| 109 | = find_match(e->u.user.name, TRY_LOAD); |
| 110 | |
| 111 | if (match) { |
| 112 | printf("-m %s ", e->u.user.name); |
Harald Welte | e813779 | 2001-01-24 01:32:51 +0000 | [diff] [blame] | 113 | |
| 114 | /* some matches don't provide a save function */ |
| 115 | if (match->save) |
| 116 | match->save(ip, e); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 117 | } else { |
| 118 | if (e->u.match_size) { |
| 119 | fprintf(stderr, |
| 120 | "Can't find library for match `%s'\n", |
| 121 | e->u.user.name); |
| 122 | exit(1); |
| 123 | } |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* print a given ip including mask if neccessary */ |
| 129 | static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert) |
| 130 | { |
| 131 | if (!mask && !ip) |
| 132 | return; |
| 133 | |
| 134 | printf("%s %s%u.%u.%u.%u", |
| 135 | prefix, |
| 136 | invert ? "! " : "", |
| 137 | IP_PARTS(ip)); |
| 138 | |
| 139 | if (mask != 0xffffffff) |
| 140 | printf("/%u.%u.%u.%u ", IP_PARTS(mask)); |
| 141 | else |
| 142 | printf(" "); |
| 143 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 144 | |
| 145 | /* We want this to be readable, so only print out neccessary fields. |
| 146 | * Because that's the kind of world I want to live in. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 147 | static void print_rule(const struct ipt_entry *e, |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 148 | iptc_handle_t *h, const char *chain, int counters) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 149 | { |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 150 | struct ipt_entry_target *t; |
| 151 | |
| 152 | /* print counters */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 153 | if (counters) |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 154 | printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 156 | /* print chain name */ |
| 157 | printf("-A %s ", chain); |
| 158 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 159 | /* Print IP part. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 160 | print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr, |
| 161 | e->ip.invflags & IPT_INV_SRCIP); |
| 162 | |
| 163 | print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr, |
Harald Welte | fa7625a | 2001-01-26 03:28:18 +0000 | [diff] [blame] | 164 | e->ip.invflags & IPT_INV_DSTIP); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 165 | |
| 166 | print_iface('i', e->ip.iniface, e->ip.iniface_mask, |
| 167 | e->ip.invflags & IPT_INV_VIA_IN); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 168 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | print_iface('o', e->ip.outiface, e->ip.outiface_mask, |
| 170 | e->ip.invflags & IPT_INV_VIA_OUT); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 171 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 172 | print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO); |
| 173 | |
| 174 | if (e->ip.flags & IPT_F_FRAG) |
| 175 | printf("%s-f ", |
| 176 | e->ip.invflags & IPT_INV_FRAG ? "! " : ""); |
| 177 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 178 | /* Print matchinfo part */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 179 | if (e->target_offset) { |
Harald Welte | 32db524 | 2001-01-23 22:46:22 +0000 | [diff] [blame] | 180 | IPT_MATCH_ITERATE(e, print_match, &e->ip); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 183 | /* Print target name */ |
| 184 | printf("-j %s ", iptc_get_target(e, h)); |
| 185 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 186 | /* Print targinfo part */ |
Rusty Russell | 082ba02 | 2001-01-07 06:54:51 +0000 | [diff] [blame] | 187 | t = ipt_get_target((struct ipt_entry *)e); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 188 | if (t->u.user.name[0]) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 189 | struct iptables_target *target |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 190 | = find_target(t->u.user.name, TRY_LOAD); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 191 | |
| 192 | if (target) |
Harald Welte | 32db524 | 2001-01-23 22:46:22 +0000 | [diff] [blame] | 193 | target->save(&e->ip, t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 194 | else { |
| 195 | /* If some bits are non-zero, it implies we *need* |
| 196 | to understand it */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 197 | if (t->u.target_size) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 198 | fprintf(stderr, |
| 199 | "Can't find library for target `%s'\n", |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 200 | t->u.user.name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 201 | exit(1); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | printf("\n"); |
| 206 | } |
| 207 | |
| 208 | /* Debugging prototype. */ |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 209 | static int for_each_table(int (*func)(const char *tablename)) |
| 210 | { |
| 211 | int ret = 1; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 212 | FILE *procfile = NULL; |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 213 | char tablename[IPT_TABLE_MAXNAMELEN+1]; |
| 214 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 215 | procfile = fopen("/proc/net/ip_tables_names", "r"); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 216 | if (!procfile) |
| 217 | return 0; |
| 218 | |
| 219 | while (fgets(tablename, sizeof(tablename), procfile)) { |
| 220 | if (tablename[strlen(tablename) - 1] != '\n') |
| 221 | exit_error(OTHER_PROBLEM, |
| 222 | "Badly formed tablename `%s'\n", |
| 223 | tablename); |
| 224 | tablename[strlen(tablename) - 1] = '\0'; |
| 225 | ret &= func(tablename); |
| 226 | } |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 232 | static int do_output(const char *tablename) |
| 233 | { |
| 234 | iptc_handle_t h; |
| 235 | const char *chain = NULL; |
| 236 | |
| 237 | if (!tablename) |
| 238 | return for_each_table(&do_output); |
| 239 | |
| 240 | h = iptc_init(tablename); |
| 241 | if (!h) |
| 242 | exit_error(OTHER_PROBLEM, "Can't initialize: %s\n", |
| 243 | iptc_strerror(errno)); |
| 244 | |
| 245 | if (!binary) { |
| 246 | time_t now = time(NULL); |
| 247 | |
| 248 | printf("# Generated by iptables-save v%s on %s", |
| 249 | NETFILTER_VERSION, ctime(&now)); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 250 | printf("*%s\n", tablename); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 251 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 252 | /* Dump out chain names first, |
| 253 | * thereby preventing dependency conflicts */ |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 254 | for (chain = iptc_first_chain(&h); |
| 255 | chain; |
| 256 | chain = iptc_next_chain(&h)) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 257 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 258 | printf(":%s ", chain); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 259 | if (iptc_builtin(chain, h)) { |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 260 | struct ipt_counters count; |
| 261 | printf("%s ", |
| 262 | iptc_get_policy(chain, &count, &h)); |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 263 | printf("[%llu:%llu]\n", count.pcnt, count.bcnt); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 264 | } else { |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 265 | printf("- [0:0]\n"); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 266 | } |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 267 | } |
| 268 | |
| 269 | |
| 270 | for (chain = iptc_first_chain(&h); |
| 271 | chain; |
| 272 | chain = iptc_next_chain(&h)) { |
| 273 | const struct ipt_entry *e; |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 274 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 275 | /* Dump out rules */ |
| 276 | e = iptc_first_rule(chain, &h); |
| 277 | while(e) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame^] | 278 | print_rule(e, &h, chain, counters); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 279 | e = iptc_next_rule(e, &h); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | now = time(NULL); |
| 284 | printf("COMMIT\n"); |
| 285 | printf("# Completed on %s", ctime(&now)); |
| 286 | } else { |
| 287 | /* Binary, huh? OK. */ |
| 288 | exit_error(OTHER_PROBLEM, "Binary NYI\n"); |
| 289 | } |
| 290 | |
| 291 | return 1; |
| 292 | } |
| 293 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 294 | /* Format: |
| 295 | * :Chain name POLICY packets bytes |
| 296 | * rule |
| 297 | */ |
| 298 | int main(int argc, char *argv[]) |
| 299 | { |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 300 | const char *tablename = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 301 | int c; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 302 | |
| 303 | program_name = "iptables-save"; |
| 304 | program_version = NETFILTER_VERSION; |
| 305 | |
| 306 | while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) { |
| 307 | switch (c) { |
| 308 | case 'b': |
| 309 | binary = 1; |
| 310 | break; |
| 311 | |
| 312 | case 'c': |
| 313 | counters = 1; |
| 314 | break; |
| 315 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 316 | case 't': |
| 317 | /* Select specific table. */ |
| 318 | tablename = optarg; |
| 319 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 320 | case 'd': |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 321 | do_output(tablename); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 322 | exit(0); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (optind < argc) { |
| 327 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 328 | exit(1); |
| 329 | } |
| 330 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 331 | return !do_output(tablename); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 332 | } |