Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Code to save the iptables state, in human readable-form. */ |
Rusty Russell | 6df8368 | 2001-07-10 13:41:49 +0000 | [diff] [blame] | 2 | /* Authors: Paul 'Rusty' Russell <rusty@rustcorp.com.au> and |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 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 { |
Harald Welte | de1578f | 2001-05-23 23:07:33 +0000 | [diff] [blame] | 51 | if (iface[i] != '\0') |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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; |
Harald Welte | ace8a01 | 2001-07-05 06:29:10 +0000 | [diff] [blame] | 151 | const char *target_name; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 152 | |
| 153 | /* print counters */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 154 | if (counters) |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 155 | printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 156 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 157 | /* print chain name */ |
| 158 | printf("-A %s ", chain); |
| 159 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 160 | /* Print IP part. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 161 | print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr, |
| 162 | e->ip.invflags & IPT_INV_SRCIP); |
| 163 | |
| 164 | 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] | 165 | e->ip.invflags & IPT_INV_DSTIP); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 166 | |
| 167 | print_iface('i', e->ip.iniface, e->ip.iniface_mask, |
| 168 | e->ip.invflags & IPT_INV_VIA_IN); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 169 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 170 | print_iface('o', e->ip.outiface, e->ip.outiface_mask, |
| 171 | e->ip.invflags & IPT_INV_VIA_OUT); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 172 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 173 | print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO); |
| 174 | |
| 175 | if (e->ip.flags & IPT_F_FRAG) |
| 176 | printf("%s-f ", |
| 177 | e->ip.invflags & IPT_INV_FRAG ? "! " : ""); |
| 178 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 179 | /* Print matchinfo part */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 180 | if (e->target_offset) { |
Harald Welte | 32db524 | 2001-01-23 22:46:22 +0000 | [diff] [blame] | 181 | IPT_MATCH_ITERATE(e, print_match, &e->ip); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 184 | /* Print target name */ |
Harald Welte | 6f83cf0 | 2001-05-24 23:22:28 +0000 | [diff] [blame] | 185 | target_name = iptc_get_target(e, h); |
| 186 | if (target_name && (*target_name != '\0')) |
| 187 | printf("-j %s ", target_name); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 188 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 189 | /* Print targinfo part */ |
Rusty Russell | 082ba02 | 2001-01-07 06:54:51 +0000 | [diff] [blame] | 190 | t = ipt_get_target((struct ipt_entry *)e); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 191 | if (t->u.user.name[0]) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 192 | struct iptables_target *target |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 193 | = find_target(t->u.user.name, TRY_LOAD); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 194 | |
Harald Welte | 31098ad | 2001-10-16 09:40:13 +0000 | [diff] [blame^] | 195 | if (!target) { |
| 196 | fprintf(stderr, "Can't find library for target `%s'\n", |
| 197 | t->u.user.name); |
| 198 | exit(1); |
| 199 | } |
| 200 | |
| 201 | if (target->save) |
Harald Welte | 32db524 | 2001-01-23 22:46:22 +0000 | [diff] [blame] | 202 | target->save(&e->ip, t); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 203 | else { |
Harald Welte | 31098ad | 2001-10-16 09:40:13 +0000 | [diff] [blame^] | 204 | /* If the target size is greater than ipt_entry_target |
| 205 | * there is something to be saved, we just don't know |
| 206 | * how to print it */ |
| 207 | if (t->u.target_size != |
| 208 | sizeof(struct ipt_entry_target)) { |
| 209 | fprintf(stderr, "Target `%s' is missing " |
| 210 | "save function\n", |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 211 | t->u.user.name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 212 | exit(1); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | printf("\n"); |
| 217 | } |
| 218 | |
| 219 | /* Debugging prototype. */ |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 220 | static int for_each_table(int (*func)(const char *tablename)) |
| 221 | { |
| 222 | int ret = 1; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 223 | FILE *procfile = NULL; |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 224 | char tablename[IPT_TABLE_MAXNAMELEN+1]; |
| 225 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 226 | procfile = fopen("/proc/net/ip_tables_names", "r"); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 227 | if (!procfile) |
| 228 | return 0; |
| 229 | |
| 230 | while (fgets(tablename, sizeof(tablename), procfile)) { |
| 231 | if (tablename[strlen(tablename) - 1] != '\n') |
| 232 | exit_error(OTHER_PROBLEM, |
| 233 | "Badly formed tablename `%s'\n", |
| 234 | tablename); |
| 235 | tablename[strlen(tablename) - 1] = '\0'; |
| 236 | ret &= func(tablename); |
| 237 | } |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 243 | static int do_output(const char *tablename) |
| 244 | { |
| 245 | iptc_handle_t h; |
| 246 | const char *chain = NULL; |
| 247 | |
| 248 | if (!tablename) |
| 249 | return for_each_table(&do_output); |
| 250 | |
| 251 | h = iptc_init(tablename); |
| 252 | if (!h) |
| 253 | exit_error(OTHER_PROBLEM, "Can't initialize: %s\n", |
| 254 | iptc_strerror(errno)); |
| 255 | |
| 256 | if (!binary) { |
| 257 | time_t now = time(NULL); |
| 258 | |
| 259 | printf("# Generated by iptables-save v%s on %s", |
| 260 | NETFILTER_VERSION, ctime(&now)); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 261 | printf("*%s\n", tablename); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 262 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 263 | /* Dump out chain names first, |
| 264 | * thereby preventing dependency conflicts */ |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 265 | for (chain = iptc_first_chain(&h); |
| 266 | chain; |
| 267 | chain = iptc_next_chain(&h)) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 268 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 269 | printf(":%s ", chain); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 270 | if (iptc_builtin(chain, h)) { |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 271 | struct ipt_counters count; |
| 272 | printf("%s ", |
| 273 | iptc_get_policy(chain, &count, &h)); |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 274 | printf("[%llu:%llu]\n", count.pcnt, count.bcnt); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 275 | } else { |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 276 | printf("- [0:0]\n"); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 277 | } |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | for (chain = iptc_first_chain(&h); |
| 282 | chain; |
| 283 | chain = iptc_next_chain(&h)) { |
| 284 | const struct ipt_entry *e; |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 285 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 286 | /* Dump out rules */ |
| 287 | e = iptc_first_rule(chain, &h); |
| 288 | while(e) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 289 | print_rule(e, &h, chain, counters); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 290 | e = iptc_next_rule(e, &h); |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | now = time(NULL); |
| 295 | printf("COMMIT\n"); |
| 296 | printf("# Completed on %s", ctime(&now)); |
| 297 | } else { |
| 298 | /* Binary, huh? OK. */ |
| 299 | exit_error(OTHER_PROBLEM, "Binary NYI\n"); |
| 300 | } |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 305 | /* Format: |
| 306 | * :Chain name POLICY packets bytes |
| 307 | * rule |
| 308 | */ |
| 309 | int main(int argc, char *argv[]) |
| 310 | { |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 311 | const char *tablename = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 312 | int c; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 313 | |
| 314 | program_name = "iptables-save"; |
| 315 | program_version = NETFILTER_VERSION; |
| 316 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 317 | #ifdef NO_SHARED_LIBS |
| 318 | init_extensions(); |
| 319 | #endif |
| 320 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 321 | while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) { |
| 322 | switch (c) { |
| 323 | case 'b': |
| 324 | binary = 1; |
| 325 | break; |
| 326 | |
| 327 | case 'c': |
| 328 | counters = 1; |
| 329 | break; |
| 330 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 331 | case 't': |
| 332 | /* Select specific table. */ |
| 333 | tablename = optarg; |
| 334 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 335 | case 'd': |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 336 | do_output(tablename); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 337 | exit(0); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (optind < argc) { |
| 342 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 343 | exit(1); |
| 344 | } |
| 345 | |
Rusty Russell | a8f033e | 2000-07-30 01:43:01 +0000 | [diff] [blame] | 346 | return !do_output(tablename); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 347 | } |