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